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 { describe, expect, it } from 'vitest'; |
| 2 | import { version } from 'zod/v4/core'; |
| 3 | |
| 4 | import type { ZodBackend } from '../src/backend/types'; |
| 5 | import { zodV3Backend } from '../src/backend/zod-v3'; |
| 6 | import { zodV4Backend } from '../src/backend/zod-v4'; |
| 7 | import { meetsZodV4Floor } from '../src/backend/zod-v4-floor'; |
| 8 | import type { ParseContext } from '../src/context'; |
| 9 | import { parseObjectShape } from '../src/parsers/parse-object-shape'; |
| 10 | import type { JsonSchemaObject } from '../src/types'; |
| 11 | |
| 12 | /** |
| 13 | * Counts how many whole-schema guards a shape parse attaches. A permissive |
| 14 | * property ($ref, typeless constraints) gets its guard from the permissive |
| 15 | * branch of `selectParser`, so the shape parser must not attach the identical |
| 16 | * guard a second time; a whole-schema property that parses normally still |
| 17 | * needs the property-level guard, and a plain property needs none. |
| 18 | */ |
| 19 | const countGuardAttachments = <S>(backend: ZodBackend<S>, schema: JsonSchemaObject): number => { |
| 20 | let attachments = 0; |
| 21 | const counting: ZodBackend<S> = { |
| 22 | ...backend, |
| 23 | attachGuard: (parsed, guard) => { |
| 24 | attachments += 1; |
| 25 | return backend.attachGuard(parsed, guard); |
| 26 | }, |
| 27 | }; |
| 28 | const ctx: ParseContext<S> = { |
| 29 | path: [], |
| 30 | seen: new Map(), |
| 31 | root: schema, |
| 32 | backend: counting, |
| 33 | }; |
| 34 | parseObjectShape(schema, ctx); |
| 35 | return attachments; |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Whether a typeless `{ not: { type: 'string' } }` property, replaced by a |
| 40 | * `parserOverride` with an unconstrained schema, accepts `value`. The override |
| 41 | * skips the parser that would guard the property, so the shape parser must |
| 42 | * still guard it. |
| 43 | */ |
| 44 | const overriddenPropertyAccepts = <S extends { safeParse(value: unknown): { success: boolean } }>( |
| 45 | backend: ZodBackend<S>, |
| 46 | value: unknown |
| 47 | ): boolean => { |
| 48 | const schema: JsonSchemaObject = { properties: { p: { not: { type: 'string' } } } }; |
| 49 | const shape = parseObjectShape(schema, { |
| 50 | path: [], |
| 51 | seen: new Map(), |
| 52 | root: schema, |
| 53 | backend, |
| 54 | parserOverride: node => ('not' in node ? backend.any() : undefined), |
| 55 | }); |
| 56 | return shape.p.safeParse(value).success; |
| 57 | }; |
| 58 | |
| 59 | const backends: ReadonlyArray<{ |
| 60 | name: string; |
| 61 | attachmentsOf: (schema: JsonSchemaObject) => number; |
| 62 | overrideAccepts: (value: unknown) => boolean; |
| 63 | }> = [ |
| 64 | { |
| 65 | name: 'zod-v3', |
| 66 | attachmentsOf: schema => countGuardAttachments(zodV3Backend, schema), |
| 67 | overrideAccepts: value => overriddenPropertyAccepts(zodV3Backend, value), |
| 68 | }, |
| 69 | // The `/v4` backend cannot load below the floor (an embedded zod 4.0.0 core |
| 70 | // inside zod 3.25.x), so it joins only where the installed copy meets it. |
| 71 | ...(meetsZodV4Floor(version) |
| 72 | ? [ |
| 73 | { |
| 74 | name: 'zod-v4', |
| 75 | attachmentsOf: (schema: JsonSchemaObject) => countGuardAttachments(zodV4Backend, schema), |
| 76 | overrideAccepts: (value: unknown) => overriddenPropertyAccepts(zodV4Backend, value), |
| 77 | }, |
| 78 | ] |
| 79 | : []), |
| 80 | ]; |
| 81 | |
| 82 | describe.each(backends)('parseObjectShape guard attachments ($name)', ({ attachmentsOf }) => { |
| 83 | it('attaches the guard once for a permissive $ref property', () => { |
| 84 | const attachments = attachmentsOf({ |
| 85 | properties: { p: { $ref: '#/$defs/x' } }, |
| 86 | $defs: { x: { type: 'string' } }, |
| 87 | }); |
| 88 | expect(attachments).toBe(1); |
| 89 | }); |
| 90 | |
| 91 | it('attaches the guard once for a typeless permissive property', () => { |
| 92 | const attachments = attachmentsOf({ |
| 93 | properties: { p: { properties: { a: { type: 'string' } } } }, |
| 94 | }); |
| 95 | expect(attachments).toBe(1); |
| 96 | }); |
| 97 | |
| 98 | it('attaches the guard once for a whole-schema property that parses normally', () => { |
| 99 | const attachments = attachmentsOf({ |
| 100 | properties: { p: { type: 'array', uniqueItems: true } }, |
| 101 | }); |
| 102 | expect(attachments).toBe(1); |
| 103 | }); |
| 104 | |
| 105 | it('attaches no guard for a plain property', () => { |
| 106 | const attachments = attachmentsOf({ properties: { p: { type: 'string' } } }); |
| 107 | expect(attachments).toBe(0); |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | describe.each(backends)('parseObjectShape with a parserOverride ($name)', ({ overrideAccepts }) => { |
| 112 | it('keeps the guard on a permissive property the override replaces', () => { |
| 113 | expect(overrideAccepts('text')).toBe(false); |
| 114 | expect(overrideAccepts(1)).toBe(true); |
| 115 | }); |
| 116 | }); |