Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 06:00 UTC
| 1 | import Ajv from 'ajv'; |
| 2 | import { format } from '@cfworker/json-schema'; |
| 3 | import { describe, expect, it } from 'vitest'; |
| 4 | |
| 5 | import type { JsonSchema } from '../src/types'; |
| 6 | import { builders } from './support/builders'; |
| 7 | import { testFor } from './support/divergences'; |
| 8 | |
| 9 | type RejectionCase = { |
| 10 | readonly name: string; |
| 11 | readonly schema: JsonSchema; |
| 12 | readonly value: unknown; |
| 13 | }; |
| 14 | |
| 15 | type AcceptanceCase = RejectionCase; |
| 16 | |
| 17 | const ajv = new Ajv({ strict: false }); |
| 18 | |
| 19 | const rejectionCases: ReadonlyArray<RejectionCase> = [ |
| 20 | { |
| 21 | name: 'typeless scalar dispatch must not discard object assertions', |
| 22 | schema: { minLength: 2, required: ['x'] }, |
| 23 | value: {}, |
| 24 | }, |
| 25 | { |
| 26 | name: 'anyOf must compose with adjacent assertions', |
| 27 | schema: { |
| 28 | type: 'string', |
| 29 | minLength: 2, |
| 30 | anyOf: [{ const: 'a' }, { const: 'abc' }], |
| 31 | }, |
| 32 | value: 'a', |
| 33 | }, |
| 34 | { |
| 35 | name: 'allOf must compose with adjacent assertions', |
| 36 | schema: { |
| 37 | type: 'string', |
| 38 | maxLength: 1, |
| 39 | allOf: [{ pattern: '^ab' }], |
| 40 | }, |
| 41 | value: 'ab', |
| 42 | }, |
| 43 | { |
| 44 | name: 'oneOf must compose with adjacent assertions', |
| 45 | schema: { |
| 46 | type: 'number', |
| 47 | minimum: 10, |
| 48 | oneOf: [{ maximum: 5 }, { minimum: 0 }], |
| 49 | }, |
| 50 | value: 7, |
| 51 | }, |
| 52 | { |
| 53 | name: 'not must compose with adjacent assertions', |
| 54 | schema: { type: 'string', minLength: 2, not: { const: 'x' } }, |
| 55 | value: 'a', |
| 56 | }, |
| 57 | { |
| 58 | name: 'if and then are a complete conditional without else', |
| 59 | schema: { if: { type: 'string' }, then: { minLength: 2 } }, |
| 60 | value: 'a', |
| 61 | }, |
| 62 | { |
| 63 | name: 'conditionals must compose with adjacent primitive assertions', |
| 64 | schema: { |
| 65 | type: 'number', |
| 66 | if: { minimum: 0 }, |
| 67 | then: { maximum: 10 }, |
| 68 | else: { minimum: -10 }, |
| 69 | }, |
| 70 | value: 20, |
| 71 | }, |
| 72 | { |
| 73 | name: 'contains must be enforced for typed arrays', |
| 74 | schema: { type: 'array', contains: { const: 1 } }, |
| 75 | value: [2], |
| 76 | }, |
| 77 | { |
| 78 | name: 'uniqueItems must be enforced for typed arrays', |
| 79 | schema: { type: 'array', uniqueItems: true }, |
| 80 | value: [1, 1], |
| 81 | }, |
| 82 | { |
| 83 | name: 'propertyNames must be enforced for typed objects', |
| 84 | schema: { type: 'object', propertyNames: { pattern: '^x' } }, |
| 85 | value: { bad: 1 }, |
| 86 | }, |
| 87 | { |
| 88 | name: 'required must be enforced for property-less typed objects', |
| 89 | schema: { type: 'object', required: ['x'] }, |
| 90 | value: {}, |
| 91 | }, |
| 92 | { |
| 93 | name: 'required properties must be checked before defaults materialize', |
| 94 | schema: { |
| 95 | type: 'object', |
| 96 | properties: { x: { type: 'string', default: 'generated' } }, |
| 97 | required: ['x'], |
| 98 | }, |
| 99 | value: {}, |
| 100 | }, |
| 101 | { |
| 102 | name: 'const must be enforced for typed objects', |
| 103 | schema: { type: 'object', const: { x: 1 } }, |
| 104 | value: { x: 2 }, |
| 105 | }, |
| 106 | { |
| 107 | name: 'enum must be enforced for typed arrays', |
| 108 | schema: { type: 'array', enum: [[1, 2]] }, |
| 109 | value: [2], |
| 110 | }, |
| 111 | { |
| 112 | name: 'local references must constrain direct converter callers', |
| 113 | schema: { |
| 114 | $defs: { positive: { type: 'number', minimum: 1 } }, |
| 115 | $ref: '#/$defs/positive', |
| 116 | }, |
| 117 | value: 0, |
| 118 | }, |
| 119 | ]; |
| 120 | |
| 121 | const acceptanceCases: ReadonlyArray<AcceptanceCase> = [ |
| 122 | { |
| 123 | name: 'typeless properties must accept non-object instances', |
| 124 | schema: { properties: { x: { type: 'string' } } }, |
| 125 | value: 42, |
| 126 | }, |
| 127 | { |
| 128 | name: 'typeless required must accept non-object instances', |
| 129 | schema: { required: ['x'] }, |
| 130 | value: 42, |
| 131 | }, |
| 132 | { |
| 133 | name: 'typeless items must accept non-array instances', |
| 134 | schema: { items: { type: 'string' } }, |
| 135 | value: 42, |
| 136 | }, |
| 137 | { |
| 138 | name: 'typeless array bounds must accept non-array instances', |
| 139 | schema: { minItems: 2 }, |
| 140 | value: { not: 'an array' }, |
| 141 | }, |
| 142 | { |
| 143 | name: 'typeless propertyNames must accept non-object instances', |
| 144 | schema: { propertyNames: { pattern: '^x' } }, |
| 145 | value: 42, |
| 146 | }, |
| 147 | { |
| 148 | name: 'typeless dependencies must accept non-object instances', |
| 149 | schema: { dependencies: { a: ['b'] } }, |
| 150 | value: 42, |
| 151 | }, |
| 152 | { |
| 153 | name: 'typeless not can accept a different instance type', |
| 154 | schema: { not: { type: 'string' } }, |
| 155 | value: 42, |
| 156 | }, |
| 157 | { |
| 158 | name: 'typeless conditionals leave nonmatching instance types untouched', |
| 159 | schema: { if: { type: 'string' }, then: { minLength: 2 } }, |
| 160 | value: 42, |
| 161 | }, |
| 162 | { |
| 163 | name: 'a direct local ref accepts values admitted by its target', |
| 164 | schema: { |
| 165 | $defs: { positive: { type: 'number', minimum: 1 } }, |
| 166 | $ref: '#/$defs/positive', |
| 167 | }, |
| 168 | value: 2, |
| 169 | }, |
| 170 | { |
| 171 | name: 'const accepts the matching typed object', |
| 172 | schema: { type: 'object', const: { x: 1 } }, |
| 173 | value: { x: 1 }, |
| 174 | }, |
| 175 | { |
| 176 | name: 'sibling allOf object branches accept each other declared keys', |
| 177 | schema: { |
| 178 | allOf: [ |
| 179 | { type: 'object', properties: { a: { type: 'string' } }, required: ['a'] }, |
| 180 | { type: 'object', properties: { b: { type: 'string' } }, required: ['b'] }, |
| 181 | ], |
| 182 | }, |
| 183 | value: { a: 'ready', b: 'set' }, |
| 184 | }, |
| 185 | { |
| 186 | name: 'nested typeless object assertions accept non-object property values', |
| 187 | schema: { |
| 188 | type: 'object', |
| 189 | properties: { value: { properties: { x: { type: 'string' } } } }, |
| 190 | required: ['value'], |
| 191 | }, |
| 192 | value: { value: 42 }, |
| 193 | }, |
| 194 | ]; |
| 195 | |
| 196 | describe.each(builders)('whole-schema semantic regressions ($name)', builder => { |
| 197 | for (const { name, schema, value } of rejectionCases) { |
| 198 | testFor(builder, name)(name, () => { |
| 199 | expect(ajv.compile(schema)(value), 'Draft 7 oracle must reject the fixture').toBe(false); |
| 200 | expect(builder.convert(schema).safeParse(value).success).toBe(false); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | for (const { name, schema, value } of acceptanceCases) { |
| 205 | testFor(builder, name)(name, () => { |
| 206 | expect(ajv.compile(schema)(value), 'Draft 7 oracle must accept the fixture').toBe(true); |
| 207 | expect(builder.convert(schema).safeParse(value).success).toBe(true); |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | it('resolves local references from a raw shape against the enclosing document', () => { |
| 212 | const schema: JsonSchema = { |
| 213 | type: 'object', |
| 214 | properties: { name: { $ref: '#/$defs/name' } }, |
| 215 | required: ['name'], |
| 216 | $defs: { name: { type: 'string' } }, |
| 217 | }; |
| 218 | const shape = builder.objectFromShape(builder.convertShape(schema)); |
| 219 | |
| 220 | expect(shape.safeParse({ name: 'Ada' }).success).toBe(true); |
| 221 | expect(shape.safeParse({ name: 1 }).success).toBe(false); |
| 222 | }); |
| 223 | |
| 224 | it('keeps sibling defaults when a nested reference widens only its own property', () => { |
| 225 | const schema: JsonSchema = { |
| 226 | type: 'object', |
| 227 | properties: { |
| 228 | name: { $ref: '#/$defs/n' }, |
| 229 | enabled: { type: 'boolean', default: true }, |
| 230 | }, |
| 231 | $defs: { n: { type: 'string' } }, |
| 232 | }; |
| 233 | const parsed = builder.convert(schema); |
| 234 | |
| 235 | expect(parsed.parse({ name: 'Ada' })).toEqual({ name: 'Ada', enabled: true }); |
| 236 | expect(parsed.safeParse({ name: 1 }).success).toBe(false); |
| 237 | }); |
| 238 | |
| 239 | it('keeps allOf branch defaults and stays strict over the union of declared keys', () => { |
| 240 | const schema: JsonSchema = { |
| 241 | allOf: [ |
| 242 | { type: 'object', properties: { a: { type: 'string' } }, required: ['a'] }, |
| 243 | { type: 'object', properties: { b: { type: 'string', default: 'set' } } }, |
| 244 | ], |
| 245 | }; |
| 246 | const parsed = builder.convert(schema); |
| 247 | |
| 248 | expect(parsed.parse({ a: 'ready' })).toEqual({ a: 'ready', b: 'set' }); |
| 249 | expect(parsed.safeParse({ a: 'ready', c: 1 }).success).toBe(false); |
| 250 | }); |
| 251 | |
| 252 | it('normalizes Draft 4 exclusive bounds before the whole-schema guard runs', () => { |
| 253 | const schema = { |
| 254 | type: 'object', |
| 255 | propertyNames: { pattern: '^[a-z]+$' }, |
| 256 | properties: { v: { type: 'number', minimum: 0, exclusiveMinimum: true } }, |
| 257 | } as unknown as JsonSchema; |
| 258 | const parsed = builder.convert(schema); |
| 259 | |
| 260 | expect(parsed.safeParse({ v: 0.5 }).success).toBe(true); |
| 261 | expect(parsed.safeParse({ v: 0 }).success).toBe(false); |
| 262 | }); |
| 263 | |
| 264 | it('keeps Draft 4 exclusive flags and adjacent Draft 7 keywords active together', () => { |
| 265 | const schema = { |
| 266 | type: 'array', |
| 267 | minimum: 0, |
| 268 | exclusiveMinimum: true, |
| 269 | contains: { const: 1 }, |
| 270 | } as unknown as JsonSchema; |
| 271 | |
| 272 | // Ajv intentionally cannot serve as the oracle for the Draft 4 boolean |
| 273 | // bound. This value isolates `contains`, which remains a Draft 7 assertion |
| 274 | // even when an OpenAPI 3.0 exporter emits the older bound spelling nearby. |
| 275 | expect(builder.convert(schema).safeParse([2]).success).toBe(false); |
| 276 | }); |
| 277 | |
| 278 | it('accepts every valid prefix of a closed Draft 7 tuple', () => { |
| 279 | const schema: JsonSchema = { |
| 280 | type: 'array', |
| 281 | items: [{ type: 'string' }, { type: 'integer' }], |
| 282 | additionalItems: false, |
| 283 | }; |
| 284 | const parsed = builder.convert(schema); |
| 285 | |
| 286 | for (const value of [[], ['ready'], ['ready', 2]]) { |
| 287 | expect(ajv.compile(schema)(value), 'Draft 7 oracle must accept the fixture').toBe(true); |
| 288 | expect(parsed.safeParse(value).success).toBe(true); |
| 289 | } |
| 290 | for (const value of [ |
| 291 | [2, 'ready'], |
| 292 | ['ready', 2, true], |
| 293 | ]) { |
| 294 | expect(ajv.compile(schema)(value), 'Draft 7 oracle must reject the fixture').toBe(false); |
| 295 | expect(parsed.safeParse(value).success).toBe(false); |
| 296 | } |
| 297 | }); |
| 298 | |
| 299 | it('allows unconstrained trailing values when additionalItems is omitted', () => { |
| 300 | const schema: JsonSchema = { |
| 301 | type: 'array', |
| 302 | items: [{ type: 'string' }, { type: 'integer' }], |
| 303 | }; |
| 304 | const value = ['ready', 2, { extra: true }]; |
| 305 | |
| 306 | expect(ajv.compile(schema)(value), 'Draft 7 oracle must accept the fixture').toBe(true); |
| 307 | expect(builder.convert(schema).safeParse(value).success).toBe(true); |
| 308 | }); |
| 309 | |
| 310 | it('validates trailing tuple values against schema-valued additionalItems', () => { |
| 311 | const schema: JsonSchema = { |
| 312 | type: 'array', |
| 313 | items: [{ type: 'string' }], |
| 314 | additionalItems: { type: 'integer' }, |
| 315 | }; |
| 316 | const parsed = builder.convert(schema); |
| 317 | |
| 318 | expect(parsed.safeParse(['ready', 1, 2]).success).toBe(true); |
| 319 | expect(parsed.safeParse(['ready', 'wrong']).success).toBe(false); |
| 320 | }); |
| 321 | |
| 322 | it('does not round tiny nonzero numbers into integer multiples', () => { |
| 323 | const schema: JsonSchema = { type: 'number', multipleOf: 3 }; |
| 324 | const value = 2 ** -1022; |
| 325 | |
| 326 | expect(ajv.compile(schema)(value), 'Draft 7 oracle must reject the fixture').toBe(false); |
| 327 | expect(builder.convert(schema).safeParse(value).success).toBe(false); |
| 328 | }); |
| 329 | |
| 330 | it('does not treat a subnormal as an integer multiple', () => { |
| 331 | const schema: JsonSchema = { type: 'number', multipleOf: 5 }; |
| 332 | const value = -5e-324; |
| 333 | |
| 334 | // Ajv divides in floating point and `-5e-324 / 5` underflows to `-0`, so the |
| 335 | // oracle accepts; the property suite skips this case for that reason. |
| 336 | expect(ajv.compile(schema)(value), 'the oracle underflows on this fixture').toBe(true); |
| 337 | expect(builder.convert(schema).safeParse(value).success).toBe(false); |
| 338 | }); |
| 339 | |
| 340 | it('keeps a guarded object root a ZodObject with its shape', () => { |
| 341 | const schema: JsonSchema = { |
| 342 | type: 'object', |
| 343 | properties: { |
| 344 | q: { type: 'string' }, |
| 345 | limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] }, |
| 346 | }, |
| 347 | required: ['q'], |
| 348 | }; |
| 349 | const parsed = builder.convert(schema); |
| 350 | |
| 351 | expect(builder.kind(parsed)).toBe('object'); |
| 352 | expect(Object.keys(builder.shape(parsed))).toEqual(['q', 'limit']); |
| 353 | expect(parsed.safeParse({ q: 'cats', limit: null }).success).toBe(true); |
| 354 | expect(parsed.safeParse({ q: 'cats', limit: 'ten' }).success).toBe(false); |
| 355 | expect(parsed.describe('Search').safeParse({ q: 'cats', limit: 'ten' }).success).toBe(false); |
| 356 | }); |
| 357 | |
| 358 | it('accepts patterns with identity escapes that Unicode mode refuses', () => { |
| 359 | const schema: JsonSchema = { |
| 360 | type: 'object', |
| 361 | properties: { |
| 362 | slug: { type: 'string', pattern: '^[a-z\\_]+$' }, |
| 363 | ref: { $ref: '#/$defs/handle' }, |
| 364 | limit: { anyOf: [{ type: 'integer' }, { type: 'null' }] }, |
| 365 | }, |
| 366 | $defs: { handle: { type: 'string', pattern: '^\\@[a-z\\-]+$' } }, |
| 367 | }; |
| 368 | const parsed = builder.convert(schema); |
| 369 | |
| 370 | expect(parsed.safeParse({ slug: 'a_b', ref: '@a-b' }).success).toBe(true); |
| 371 | expect(parsed.safeParse({ slug: 'A' }).success).toBe(false); |
| 372 | expect(parsed.safeParse({ ref: 'a-b' }).success).toBe(false); |
| 373 | }); |
| 374 | |
| 375 | it.each([ |
| 376 | { name: 'a legacy identity escape', pattern: '^\\k_$', valid: 'k_', invalid: 'wrong' }, |
| 377 | { name: 'a quantified lookahead', pattern: '^(?=a){2}ab$', valid: 'ab', invalid: 'b' }, |
| 378 | ])('enforces $name in a pattern only the guard sees', ({ pattern, valid, invalid }) => { |
| 379 | const schema: JsonSchema = { |
| 380 | type: 'object', |
| 381 | properties: { handle: { $ref: '#/$defs/handle' } }, |
| 382 | $defs: { handle: { type: 'string', pattern } }, |
| 383 | }; |
| 384 | const parsed = builder.convert(schema); |
| 385 | |
| 386 | expect(parsed.safeParse({ handle: valid }).success).toBe(true); |
| 387 | expect(parsed.safeParse({ handle: invalid }).success).toBe(false); |
| 388 | }); |
| 389 | |
| 390 | it('leaves no pattern formats in the interpreter format table', () => { |
| 391 | const schema: JsonSchema = { |
| 392 | type: 'object', |
| 393 | properties: { handle: { $ref: '#/$defs/handle' } }, |
| 394 | $defs: { handle: { type: 'string', pattern: '^leak-check-\\d+$' } }, |
| 395 | }; |
| 396 | const parsed = builder.convert(schema); |
| 397 | |
| 398 | expect(parsed.safeParse({ handle: 'leak-check-1' }).success).toBe(true); |
| 399 | expect(parsed.safeParse({ handle: 'wrong' }).success).toBe(false); |
| 400 | expect(Object.keys(format).filter(name => name.includes('leak-check'))).toEqual([]); |
| 401 | }); |
| 402 | |
| 403 | it('enforces every patternProperties key, including keys that differ only in escapes', () => { |
| 404 | const schema: JsonSchema = { |
| 405 | patternProperties: { |
| 406 | '^a\\_b$': { type: 'integer', minimum: 10 }, |
| 407 | '^a_b$': { type: 'integer' }, |
| 408 | '^\\k_$': { type: 'string' }, |
| 409 | }, |
| 410 | }; |
| 411 | const parsed = builder.convert(schema); |
| 412 | |
| 413 | expect(parsed.safeParse({ a_b: 12, k_: 'ok' }).success).toBe(true); |
| 414 | expect(parsed.safeParse({ a_b: 2 }).success).toBe(false); |
| 415 | expect(parsed.safeParse({ k_: 1 }).success).toBe(false); |
| 416 | }); |
| 417 | |
| 418 | it('resolves references nested under a patternProperties key that needs a rewrite', () => { |
| 419 | const schema: JsonSchema = { |
| 420 | type: 'object', |
| 421 | patternProperties: { '^x\\_': { $ref: '#/$defs/label' } }, |
| 422 | $defs: { label: { type: 'string' } }, |
| 423 | }; |
| 424 | const parsed = builder.convert(schema); |
| 425 | |
| 426 | expect(parsed.safeParse({ x_1: 'ok' }).success).toBe(true); |
| 427 | expect(parsed.safeParse({ x_1: 1 }).success).toBe(false); |
| 428 | }); |
| 429 | |
| 430 | it('does not mistake a definition named patternProperties for the keyword', () => { |
| 431 | const schema = { |
| 432 | type: 'object', |
| 433 | properties: { v: { $ref: `#/$defs/patternProperties/${encodeURI('^a\\_b$')}` } }, |
| 434 | $defs: { patternProperties: { '^a\\_b$': { type: 'string', minLength: 2 } } }, |
| 435 | } as unknown as JsonSchema; |
| 436 | const parsed = builder.convert(schema); |
| 437 | |
| 438 | expect(parsed.safeParse({ v: 'ok' }).success).toBe(true); |
| 439 | expect(parsed.safeParse({ v: 'x' }).success).toBe(false); |
| 440 | }); |
| 441 | |
| 442 | it('compares decimal multiples by their JSON number spelling', () => { |
| 443 | const schema: JsonSchema = { type: 'number', multipleOf: 0.1 }; |
| 444 | expect(builder.convert(schema).safeParse(0.3).success).toBe(true); |
| 445 | }); |
| 446 | }); |