Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { jsonSchemaToZod } from '@composio/json-schema-to-zod'; |
| 2 | import { Option, Schema } from 'effect'; |
| 3 | import { describe, expect, it } from 'vitest'; |
| 4 | import { jsonSchemaToEffectSchema, type JsonSchemaValidationIssue } from '../src/index'; |
| 5 | import { acceptedFor, loadObjectCases } from './fixtures/corpus'; |
| 6 | |
| 7 | type JsonSchema = Record<string, unknown>; |
| 8 | |
| 9 | const effectAccepts = (schema: JsonSchema, input: unknown): boolean => |
| 10 | Option.isSome( |
| 11 | Schema.decodeUnknownOption(jsonSchemaToEffectSchema(schema), { errors: 'all' })(input) |
| 12 | ); |
| 13 | |
| 14 | const zodAccepts = (schema: JsonSchema, input: unknown): boolean => |
| 15 | jsonSchemaToZod(schema).safeParse(input).success; |
| 16 | |
| 17 | const expectParity = (schema: JsonSchema, input: unknown, expected: boolean): void => { |
| 18 | expect(effectAccepts(schema, input)).toBe(expected); |
| 19 | expect(zodAccepts(schema, input)).toBe(expected); |
| 20 | }; |
| 21 | |
| 22 | describe('jsonSchemaToEffectSchema', () => { |
| 23 | it('matches the previous validator for object, required, and additional-property checks', () => { |
| 24 | const strictSchema = { |
| 25 | type: 'object', |
| 26 | required: ['name'], |
| 27 | properties: { |
| 28 | name: { type: 'string' }, |
| 29 | metadata: { |
| 30 | type: 'object', |
| 31 | properties: { count: { type: 'integer' } }, |
| 32 | }, |
| 33 | }, |
| 34 | } satisfies JsonSchema; |
| 35 | |
| 36 | expectParity(strictSchema, { name: 'Ada', metadata: { count: 1 } }, true); |
| 37 | expectParity(strictSchema, { name: 'Ada', extra: true }, false); |
| 38 | expectParity(strictSchema, { name: 'Ada', metadata: { count: 1.5 } }, false); |
| 39 | expectParity(strictSchema, { metadata: { count: 1 } }, false); |
| 40 | |
| 41 | expectParity({ type: 'object', additionalProperties: true }, { arbitrary: 'value' }, true); |
| 42 | }); |
| 43 | |
| 44 | it('normalizes the OpenAPI and Composio extensions accepted by the previous validator', () => { |
| 45 | expectParity({ type: 'string', nullable: true }, null, true); |
| 46 | expectParity({ type: 'string', nullable: true }, 42, false); |
| 47 | expectParity({ type: 'string', min: 2, max: 4 }, 'abc', true); |
| 48 | expectParity({ type: 'string', min: 2, max: 4 }, 'a', false); |
| 49 | expectParity({ type: 'array', min: 1, max: 2, items: { type: 'number' } }, [1], true); |
| 50 | expectParity({ type: 'array', min: 1, max: 2, items: { type: 'number' } }, [], false); |
| 51 | expectParity({ type: 'number', format: 'int64' }, 42, true); |
| 52 | expectParity({ type: 'number', format: 'int64' }, 4.2, false); |
| 53 | expectParity({ type: 'string', format: 'binary' }, 'aGVsbG8=', true); |
| 54 | expectParity({ type: 'string', format: 'binary' }, 'hello world', true); |
| 55 | expectParity({ type: 'string', format: 'byte' }, 'not base64', true); |
| 56 | expectParity({ type: 'string', contentEncoding: 'base16' }, '0f', true); |
| 57 | expectParity({ type: 'string', contentEncoding: 'base64' }, 'aGVsbG8gd29ybGQ=', true); |
| 58 | expectParity({ type: 'string', contentEncoding: 'base64' }, 'not base64', false); |
| 59 | expectParity({ type: 'string', contentEncoding: 'BASE64' }, 'not base64', false); |
| 60 | expectParity( |
| 61 | { type: 'string', format: 'binary', contentEncoding: 'base64' }, |
| 62 | 'not base64', |
| 63 | false |
| 64 | ); |
| 65 | expectParity({ type: 'string', format: 'ip' }, '127.0.0.1', true); |
| 66 | expectParity({ type: 'string', format: 'ip' }, 'not-an-ip', false); |
| 67 | }); |
| 68 | |
| 69 | it('honors the boolean exclusive bound under an explicit Draft 4', () => { |
| 70 | const schema = { type: 'number', minimum: 1, exclusiveMinimum: true } satisfies JsonSchema; |
| 71 | const accepts = (input: unknown, draft?: '4'): boolean => |
| 72 | Option.isSome(Schema.decodeUnknownOption(jsonSchemaToEffectSchema(schema, { draft }))(input)); |
| 73 | |
| 74 | expect(accepts(1, '4')).toBe(false); |
| 75 | expect(accepts(2, '4')).toBe(true); |
| 76 | expect(accepts(1)).toBe(false); |
| 77 | expect(accepts(2)).toBe(true); |
| 78 | }); |
| 79 | |
| 80 | it('supports JSON Schema composition without generated code', () => { |
| 81 | const schema = { |
| 82 | type: 'object', |
| 83 | additionalProperties: false, |
| 84 | required: ['value'], |
| 85 | properties: { |
| 86 | value: { |
| 87 | anyOf: [ |
| 88 | { type: 'string', minLength: 1 }, |
| 89 | { type: 'integer', minimum: 1 }, |
| 90 | ], |
| 91 | }, |
| 92 | }, |
| 93 | } satisfies JsonSchema; |
| 94 | |
| 95 | expect(effectAccepts(schema, { value: 'ready' })).toBe(true); |
| 96 | expect(effectAccepts(schema, { value: 2 })).toBe(true); |
| 97 | expect(effectAccepts(schema, { value: 0 })).toBe(false); |
| 98 | expect(effectAccepts(schema, { value: false })).toBe(false); |
| 99 | }); |
| 100 | |
| 101 | it('reports all failures with field paths and groups unknown keys', () => { |
| 102 | const schema = { |
| 103 | type: 'object', |
| 104 | additionalProperties: false, |
| 105 | required: ['email', 'profile'], |
| 106 | properties: { |
| 107 | email: { type: 'string' }, |
| 108 | profile: { |
| 109 | type: 'object', |
| 110 | additionalProperties: false, |
| 111 | required: ['age'], |
| 112 | properties: { age: { type: 'integer' } }, |
| 113 | }, |
| 114 | }, |
| 115 | } satisfies JsonSchema; |
| 116 | let captured: ReadonlyArray<JsonSchemaValidationIssue> = []; |
| 117 | const effectSchema = jsonSchemaToEffectSchema(schema, { |
| 118 | formatIssues: issues => { |
| 119 | captured = issues; |
| 120 | return issues.map(issue => issue.message); |
| 121 | }, |
| 122 | }); |
| 123 | |
| 124 | const result = Schema.decodeUnknownOption(effectSchema, { errors: 'all' })({ |
| 125 | email: 42, |
| 126 | profile: { age: 'old', extra: true }, |
| 127 | typo: true, |
| 128 | }); |
| 129 | |
| 130 | expect(Option.isNone(result)).toBe(true); |
| 131 | expect(captured).toEqual( |
| 132 | expect.arrayContaining([ |
| 133 | expect.objectContaining({ code: 'type', path: ['email'] }), |
| 134 | expect.objectContaining({ code: 'type', path: ['profile', 'age'] }), |
| 135 | expect.objectContaining({ code: 'unrecognized_keys', keys: ['extra'], path: ['profile'] }), |
| 136 | expect.objectContaining({ code: 'unrecognized_keys', keys: ['typo'], path: [] }), |
| 137 | ]) |
| 138 | ); |
| 139 | }); |
| 140 | }); |
| 141 | |
| 142 | describe('shared cross-SDK object corpus', () => { |
| 143 | for (const testCase of loadObjectCases()) { |
| 144 | describe(testCase.id, () => { |
| 145 | for (const [index, instance] of testCase.instances.entries()) { |
| 146 | const expected = acceptedFor(instance, 'effect'); |
| 147 | |
| 148 | it(`instance ${index} is ${expected ? 'accepted' : 'rejected'}`, () => { |
| 149 | const result = Schema.decodeUnknownOption( |
| 150 | jsonSchemaToEffectSchema(testCase.schema as JsonSchema), |
| 151 | { errors: 'all' } |
| 152 | )(instance.input); |
| 153 | |
| 154 | expect(Option.isSome(result)).toBe(expected); |
| 155 | if (Option.isSome(result) && instance.effect && 'output' in instance.effect) { |
| 156 | // Effect decoding is validation-only: a successful decode returns the |
| 157 | // input value unchanged, with no default materialization. |
| 158 | expect(result.value).toEqual(instance.effect.output); |
| 159 | } |
| 160 | }); |
| 161 | } |
| 162 | }); |
| 163 | } |
| 164 | }); |
| 165 | |
| 166 | describe('schema defects fail at construction', () => { |
| 167 | // `@cfworker/json-schema` resolves references and compiles regexes inside |
| 168 | // `validate`, so each of these used to reach the caller once per call, as an |
| 169 | // input validation failure. The consumer wraps construction in its own |
| 170 | // `Effect.try` and reports a compile failure against the cached schema path, |
| 171 | // which is the accurate diagnosis for all of them. |
| 172 | const defects: ReadonlyArray<readonly [string, JsonSchema, RegExp]> = [ |
| 173 | [ |
| 174 | 'an invalid patternProperties regular expression', |
| 175 | { type: 'object', patternProperties: { '[': { type: 'string' } } }, |
| 176 | /Invalid patternProperties regular expression/, |
| 177 | ], |
| 178 | [ |
| 179 | 'an invalid patternProperties regular expression nested under a property', |
| 180 | { |
| 181 | type: 'object', |
| 182 | properties: { a: { type: 'object', patternProperties: { '(': { type: 'string' } } } }, |
| 183 | }, |
| 184 | /Invalid patternProperties regular expression/, |
| 185 | ], |
| 186 | [ |
| 187 | 'an invalid patternProperties regular expression inside array items', |
| 188 | { |
| 189 | type: 'object', |
| 190 | properties: { |
| 191 | a: { type: 'array', items: { type: 'object', patternProperties: { '(?<': {} } } }, |
| 192 | }, |
| 193 | }, |
| 194 | /Invalid patternProperties regular expression/, |
| 195 | ], |
| 196 | [ |
| 197 | 'an unresolved local $ref in a patternProperties subschema', |
| 198 | { type: 'object', patternProperties: { '^s_': { $ref: '#/$defs/Missing' } } }, |
| 199 | /Unresolved \$ref .* in a dynamic-key schema/, |
| 200 | ], |
| 201 | [ |
| 202 | 'an external $ref in a patternProperties subschema', |
| 203 | { type: 'object', patternProperties: { '^s_': { $ref: 'https://example.com/s.json' } } }, |
| 204 | /Unresolved \$ref .* in a dynamic-key schema/, |
| 205 | ], |
| 206 | [ |
| 207 | 'an unresolved local $ref in a schema-valued additionalProperties', |
| 208 | { type: 'object', additionalProperties: { $ref: '#/$defs/Missing' } }, |
| 209 | /Unresolved \$ref .* in a dynamic-key schema/, |
| 210 | ], |
| 211 | [ |
| 212 | 'a dynamic-key reference whose target carries a dangling reference', |
| 213 | { |
| 214 | type: 'object', |
| 215 | additionalProperties: { $ref: '#/$defs/Hop' }, |
| 216 | $defs: { Hop: { type: 'object', properties: { a: { $ref: '#/$defs/Missing' } } } }, |
| 217 | }, |
| 218 | /Unresolved \$ref .* in a dynamic-key schema/, |
| 219 | ], |
| 220 | [ |
| 221 | 'a duplicate schema URI', |
| 222 | { |
| 223 | $id: 'https://example.com/a', |
| 224 | type: 'object', |
| 225 | $defs: { x: { $id: 'https://example.com/a', type: 'string' } }, |
| 226 | }, |
| 227 | /Duplicate schema URI/, |
| 228 | ], |
| 229 | ]; |
| 230 | |
| 231 | // Deliberately outside the scope above: matching the Python SDK, which only |
| 232 | // rejects dynamic-key schemas while wrapping a tool. Widening to every `$ref` |
| 233 | // would reject tool schemas whose dangling reference sits in a branch the |
| 234 | // interpreter never descends into, and which validate today. |
| 235 | const toleratedOutsideDynamicKeys: ReadonlyArray<readonly [string, JsonSchema]> = [ |
| 236 | [ |
| 237 | 'an unresolved $ref under a declared property', |
| 238 | { type: 'object', properties: { a: { $ref: '#/$defs/Missing' } } }, |
| 239 | ], |
| 240 | [ |
| 241 | 'an unresolved $ref inside array items', |
| 242 | { type: 'object', properties: { a: { type: 'array', items: { $ref: '#/$defs/Gone' } } } }, |
| 243 | ], |
| 244 | [ |
| 245 | 'an invalid pattern regular expression', |
| 246 | { type: 'object', properties: { a: { type: 'string', pattern: '[' } } }, |
| 247 | ], |
| 248 | ]; |
| 249 | |
| 250 | for (const [label, schema] of toleratedOutsideDynamicKeys) { |
| 251 | it(`leaves ${label} to the interpreter`, () => { |
| 252 | expect(() => jsonSchemaToEffectSchema(schema)).not.toThrow(); |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | for (const [label, schema, message] of defects) { |
| 257 | it(`rejects ${label}`, () => { |
| 258 | expect(() => jsonSchemaToEffectSchema(schema)).toThrow(message); |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | it('resolves a local $ref that does exist', () => { |
| 263 | const schema = { |
| 264 | type: 'object', |
| 265 | properties: { a: { $ref: '#/$defs/Name' } }, |
| 266 | required: ['a'], |
| 267 | $defs: { Name: { type: 'string' } }, |
| 268 | } satisfies JsonSchema; |
| 269 | |
| 270 | expect(() => jsonSchemaToEffectSchema(schema)).not.toThrow(); |
| 271 | expect(effectAccepts(schema, { a: 'Ada' })).toBe(true); |
| 272 | expect(effectAccepts(schema, { a: 1 })).toBe(false); |
| 273 | }); |
| 274 | |
| 275 | it('accepts a self-referential $ref without re-entering it', () => { |
| 276 | const schema = { |
| 277 | type: 'object', |
| 278 | properties: { next: { $ref: '#' } }, |
| 279 | } satisfies JsonSchema; |
| 280 | |
| 281 | expect(() => jsonSchemaToEffectSchema(schema)).not.toThrow(); |
| 282 | expect(effectAccepts(schema, { next: { next: {} } })).toBe(true); |
| 283 | }); |
| 284 | }); |
| 285 | |
| 286 | describe('input the interpreter cannot represent stays an input failure', () => { |
| 287 | // The one throw in `validate` that depends on the instance rather than the |
| 288 | // schema. It must stay inside the filter: throwing here would escape |
| 289 | // `Schema.decodeUnknown` as a defect instead of a typed parse error. |
| 290 | const unrepresentable: ReadonlyArray<readonly [string, unknown]> = [ |
| 291 | ['a bigint', BigInt(1)], |
| 292 | ['a function', () => 1], |
| 293 | ['a symbol', Symbol('x')], |
| 294 | ['undefined', undefined], |
| 295 | ]; |
| 296 | |
| 297 | for (const [label, input] of unrepresentable) { |
| 298 | it(`rejects ${label} without throwing`, () => { |
| 299 | const schema = jsonSchemaToEffectSchema({ type: 'object' }); |
| 300 | expect(() => Schema.decodeUnknownOption(schema)(input)).not.toThrow(); |
| 301 | expect(Option.isSome(Schema.decodeUnknownOption(schema)(input))).toBe(false); |
| 302 | }); |
| 303 | } |
| 304 | }); |