Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 119 lines 4.22 KB
Raw
1 import { describe, expect, it } from 'vitest';
2
3 import { InvalidPatternError } from '../src/index';
4 import { MAX_PATTERN_LENGTH, compilePattern } from '../src/utils/compile-pattern';
5 import { builders } from './support/builders';
6
7 const invalidPatternAt = (build: () => unknown): InvalidPatternError => {
8 try {
9 build();
10 } catch (error) {
11 expect(error).toBeInstanceOf(InvalidPatternError);
12 return error as InvalidPatternError;
13 }
14 throw new Error('expected conversion to fail');
15 };
16
17 describe('compilePattern', () => {
18 it('compiles a valid pattern into an equivalent RegExp', () => {
19 const regex = compilePattern('pattern', '^[a-z]+$', { path: [] });
20 expect(regex.test('abc')).toBe(true);
21 expect(regex.test('ABC')).toBe(false);
22 });
23
24 it('turns a RegExp SyntaxError into an InvalidPatternError that keeps the cause', () => {
25 const error = invalidPatternAt(() =>
26 compilePattern('pattern', '(', { path: ['properties', 'name'] })
27 );
28 expect(error.reason).toBe('syntax');
29 expect(error.path).toEqual(['properties', 'name']);
30 expect(error.cause).toBeInstanceOf(SyntaxError);
31 expect(error.message).toContain('at properties.name');
32 });
33
34 it('rejects patterns above the length cap', () => {
35 const error = invalidPatternAt(() =>
36 compilePattern('pattern', 'a'.repeat(MAX_PATTERN_LENGTH + 1), { path: [] })
37 );
38 expect(error.reason).toBe('too-long');
39 expect(error.message).toContain('<root>');
40 });
41
42 it('appends the patternProperties key to the reported path', () => {
43 const error = invalidPatternAt(() =>
44 compilePattern('patternProperties', '[', { path: ['properties', 'tags'] })
45 );
46 expect(error.reason).toBe('syntax');
47 expect(error.path).toEqual(['properties', 'tags', 'patternProperties', '[']);
48 });
49 });
50
51 describe.each(builders)('jsonSchemaToZod pattern guard ($name)', builder => {
52 it('reports a malformed property pattern with its path instead of a raw SyntaxError', () => {
53 const error = invalidPatternAt(() =>
54 builder.convert({
55 type: 'object',
56 properties: { name: { type: 'string', pattern: '(' } },
57 })
58 );
59 expect(error).not.toBeInstanceOf(SyntaxError);
60 expect(error.keyword).toBe('pattern');
61 expect(error.pattern).toBe('(');
62 expect(error.message).toContain('at properties.name');
63 });
64
65 it('reports a typeless pattern with the path of the node carrying it', () => {
66 const error = invalidPatternAt(() =>
67 builder.convert({
68 type: 'object',
69 properties: { slug: { pattern: '[' } },
70 })
71 );
72 expect(error.path).toEqual(['properties', 'slug']);
73 });
74
75 it('reports a malformed patternProperties key with its path', () => {
76 const error = invalidPatternAt(() =>
77 builder.convert({
78 type: 'object',
79 properties: {
80 labels: {
81 type: 'object',
82 patternProperties: { '^x-(': { type: 'string' } },
83 },
84 },
85 })
86 );
87 expect(error.keyword).toBe('patternProperties');
88 expect(error.path).toEqual(['properties', 'labels', 'patternProperties', '^x-(']);
89 expect(error.message).toContain('at properties.labels.patternProperties.^x-(');
90 });
91
92 it('keeps compiling quantified groups that backtrack linearly', () => {
93 const schema = builder.convert({
94 type: 'object',
95 properties: { version: { type: 'string', pattern: '^(\\d+\\.)*\\d+$' } },
96 required: ['version'],
97 });
98
99 expect(schema.safeParse({ version: '1.2.3' }).success).toBe(true);
100 expect(schema.safeParse({ version: '42' }).success).toBe(true);
101 expect(schema.safeParse({ version: '1.2.' }).success).toBe(false);
102 expect(schema.safeParse({ version: 'v1' }).success).toBe(false);
103 });
104
105 it('still enforces valid patterns', () => {
106 const schema = builder.convert({
107 type: 'object',
108 properties: {
109 id: { type: 'string', pattern: '^[a-z]{3}-\\d+$' },
110 meta: { type: 'object', patternProperties: { '^x-': { type: 'number' } } },
111 },
112 required: ['id'],
113 });
114
115 expect(schema.safeParse({ id: 'abc-42', meta: { 'x-rate': 1 } }).success).toBe(true);
116 expect(schema.safeParse({ id: 'ABC-42' }).success).toBe(false);
117 expect(schema.safeParse({ id: 'abc-42', meta: { 'x-rate': 'one' } }).success).toBe(false);
118 });
119 });