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 169 lines 5.6 KB
Raw
1 import { parseAllOf } from './parse-all-of';
2 import { parseAnyOf } from './parse-any-of';
3 import { parseArray } from './parse-array';
4 import { parseBoolean } from './parse-boolean';
5 import { parseConst } from './parse-const';
6 import { parseDefault } from './parse-default';
7 import { parseEnum } from './parse-enum';
8 import { parseIfThenElse } from './parse-if-then-else';
9 import { parseMultipleType } from './parse-multiple-type';
10 import { parseNot } from './parse-not';
11 import { parseNull } from './parse-null';
12 import { parseNullable } from './parse-nullable';
13 import { parseNumber } from './parse-number';
14 import { parseObject } from './parse-object';
15 import { parseOneOf } from './parse-one-of';
16 import { parseString } from './parse-string';
17 import {
18 hasTypelessScalarConstraints,
19 parseTypelessConstraints,
20 } from './parse-typeless-constraints';
21 import { collectAnnotations, describeText } from '../annotations';
22 import type { ParseContext } from '../context';
23 import type { JsonSchemaObject, JsonSchema } from '../types';
24 import { its } from '../utils/its';
25 import {
26 createWholeSchemaGuard,
27 guardSchemaAt,
28 requiresPermissiveMaterialization,
29 } from '../whole-schema-validation';
30
31 const addDecorations = <S>(jsonSchema: JsonSchemaObject, zodSchema: S, ctx: ParseContext<S>): S => {
32 const description = ctx.withoutDescribes ? undefined : describeText(jsonSchema);
33 const annotations = ctx.withoutAnnotations ? undefined : collectAnnotations(jsonSchema);
34
35 if (description === undefined && annotations === undefined) {
36 return zodSchema;
37 }
38
39 return ctx.backend.decorate(zodSchema, { description, annotations });
40 };
41
42 const addDefaults = <S>(jsonSchema: JsonSchemaObject, zodSchema: S, ctx: ParseContext<S>): S => {
43 if (jsonSchema.default !== undefined) {
44 // Don't apply null defaults to non-nullable types within anyOf/oneOf contexts
45 if (jsonSchema.default === null) {
46 // Check if we're in an anyOf/oneOf context by examining the path
47 const inAnyOfOrOneOf = ctx.path.some(segment => segment === 'anyOf' || segment === 'oneOf');
48
49 // If we're in anyOf/oneOf and this is a non-nullable type, skip the default
50 if (
51 inAnyOfOrOneOf &&
52 jsonSchema.type &&
53 jsonSchema.type !== 'null' &&
54 // @ts-expect-error: `nullable` is not publicly available in Zod 4
55 !jsonSchema.nullable
56 ) {
57 return zodSchema;
58 }
59 }
60
61 return ctx.backend.withDefault(zodSchema, jsonSchema.default);
62 }
63
64 return zodSchema;
65 };
66
67 const addAnnotations = <S>(jsonSchema: JsonSchemaObject, zodSchema: S, ctx: ParseContext<S>): S => {
68 if (jsonSchema.readOnly) {
69 return ctx.backend.readonly(zodSchema);
70 }
71
72 return zodSchema;
73 };
74
75 const selectParser = <S>(schema: JsonSchemaObject, ctx: ParseContext<S>): S => {
76 if (requiresPermissiveMaterialization(schema)) {
77 // Widen only this node, and let it guard itself so combinators such as
78 // `oneOf` still discriminate between branches that were widened.
79 return ctx.backend.attachGuard(
80 ctx.backend.any(),
81 createWholeSchemaGuard(guardSchemaAt(schema, ctx))
82 );
83 } else if (its.a.nullable(schema)) {
84 return parseNullable(schema, ctx);
85 } else if (its.an.enum(schema)) {
86 // `enum`/`const` intersect the declared type, so they must win over the
87 // object and array parsers or a literal object/array value is never checked.
88 return parseEnum(schema, ctx);
89 } else if (its.a.const(schema)) {
90 return parseConst(schema, ctx);
91 } else if (its.an.object(schema)) {
92 return parseObject(schema, ctx);
93 } else if (its.an.array(schema)) {
94 return parseArray(schema, ctx);
95 } else if (its.an.anyOf(schema)) {
96 return parseAnyOf(schema, ctx);
97 } else if (its.an.allOf(schema)) {
98 return parseAllOf(schema, ctx);
99 } else if (its.a.oneOf(schema)) {
100 return parseOneOf(schema, ctx);
101 } else if (its.a.not(schema)) {
102 return parseNot(schema, ctx);
103 } else if (hasTypelessScalarConstraints(schema)) {
104 return parseTypelessConstraints(schema, ctx);
105 } else if (its.a.multipleType(schema)) {
106 return parseMultipleType(schema, ctx);
107 } else if (its.a.primitive(schema, 'string')) {
108 return parseString(schema, ctx);
109 } else if (its.a.primitive(schema, 'number') || its.a.primitive(schema, 'integer')) {
110 return parseNumber(schema, ctx.backend);
111 } else if (its.a.primitive(schema, 'boolean')) {
112 return parseBoolean(schema, ctx);
113 } else if (its.a.primitive(schema, 'null')) {
114 return parseNull(schema, ctx);
115 } else if (its.a.conditional(schema)) {
116 return parseIfThenElse(schema, ctx);
117 } else {
118 return parseDefault(schema, ctx);
119 }
120 };
121
122 export const parseSchema = <S>(
123 jsonSchema: JsonSchema,
124 ctx: ParseContext<S>,
125 blockMeta?: boolean
126 ): S => {
127 const { backend } = ctx;
128 if (typeof jsonSchema !== 'object') return jsonSchema ? backend.any() : backend.never();
129
130 if (ctx.parserOverride) {
131 const custom = ctx.parserOverride(jsonSchema, ctx);
132
133 if (backend.isSchema(custom)) {
134 return custom;
135 }
136 }
137
138 let seen = ctx.seen.get(jsonSchema);
139
140 if (seen) {
141 if (seen.r !== undefined) {
142 return seen.r;
143 }
144
145 if (ctx.depth === undefined || seen.n >= ctx.depth) {
146 return backend.any();
147 }
148
149 seen.n += 1;
150 } else {
151 seen = { r: undefined, n: 0 };
152 ctx.seen.set(jsonSchema, seen);
153 }
154
155 let parsedZodSchema = selectParser(jsonSchema, ctx);
156 if (!blockMeta) {
157 parsedZodSchema = addDecorations(jsonSchema, parsedZodSchema, ctx);
158
159 if (!ctx.withoutDefaults) {
160 parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, ctx);
161 }
162
163 parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema, ctx);
164 }
165
166 seen.r = parsedZodSchema;
167
168 return parsedZodSchema;
169 };