@composio/json-schema-to-zod 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,7 +11,7 @@ npm install @composio/json-schema-to-zod
11
11
  ### Simple example
12
12
 
13
13
  ```typescript
14
- import { jsonSchemaToZod } from 'json-schema-to-zod';
14
+ import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
15
15
 
16
16
  const jsonSchema = {
17
17
  type: 'object',
@@ -22,12 +22,40 @@ const jsonSchema = {
22
22
  },
23
23
  };
24
24
 
25
- const zodSchema = jsonSchemaToZod(myObject);
25
+ const zodSchema = jsonSchemaToZod(jsonSchema);
26
26
  ```
27
27
 
28
+ The root entry builds Zod v3 schemas (`zod/v3`), which every supported zod release ships.
29
+
30
+ ### Object-level keywords and `jsonSchemaToZodShape`
31
+
32
+ `jsonSchemaToZodShape` converts each entry of `properties` on its own, so assertions that apply to the object as a whole have no place in the shape and are not enforced: `minProperties`, `maxProperties`, `propertyNames`, `uniqueItems`, or a `required` key with no declared property. Keywords on a property (`{ uniqueItems: true, type: 'array' }` as a property schema) are still enforced. When the object-level assertions must hold, use `jsonSchemaToZod`, or check them yourself on the `z.object(shape)` you build.
33
+
34
+ ### Zod v4 schemas
35
+
36
+ The `/v4` entry builds classic Zod v4 schemas (`zod/v4`) with the same function names, so a caller switches by changing the import path:
37
+
38
+ ```typescript
39
+ import { jsonSchemaToZod, jsonSchemaToZodShape } from '@composio/json-schema-to-zod/v4';
40
+
41
+ const zodSchema = jsonSchemaToZod(jsonSchema);
42
+ const shape = jsonSchemaToZodShape(jsonSchema);
43
+ ```
44
+
45
+ Both builders accept and reject the same values. Beyond that, the v4 builder forwards the JSON Schema annotations a serializer cannot derive from the schema's checks, so `z.toJSONSchema()` and the frameworks built on it see them again:
46
+
47
+ - `format`, verbatim, including values such as `binary`, `int64`, and `time`;
48
+ - `contentEncoding` and `contentMediaType`, verbatim;
49
+ - `type: "integer"`, since the integer check is a refinement that accepts every integral number, as the root builder and JSON Schema do;
50
+ - the description, title, and examples text the root builder also writes.
51
+
52
+ Pass `{ withoutAnnotations: true }` to leave the annotations out; `{ withoutDescribes: true }` leaves out the description, as it does for the root entry.
53
+
54
+ `/v4` requires zod `>= 4.1.13`: importing it under an older release throws at load time. 4.1.13 is the first release whose `zod/v4` shares its metadata registry across copies through `globalThis`. That check covers only the zod copy that builds the schema. If the code that serializes the schema loads its own copy of zod below 4.1.13 (a framework that depends on zod directly rather than as a peer), that copy reads a separate registry and the descriptions and annotations are lost. Keep every zod copy in the process at 4.1.13 or later.
55
+
28
56
  ### Overriding a parser
29
57
 
30
- You can pass a function to the `overrideParser` option, which represents a function that receives the current schema node and the reference object, and should return a zod object when it wants to replace a default output. If the default output should be used for the node just return undefined.
58
+ You can pass a function to the `parserOverride` option, which represents a function that receives the current schema node and the reference object, and should return a zod object when it wants to replace a default output. If the default output should be used for the node just return undefined.
31
59
 
32
60
  ## Acknowledgements
33
61
 
@@ -0,0 +1,214 @@
1
+ import { ZodTypeAny } from "zod/v3";
2
+ import { RE2JS } from "re2js";
3
+ //#region ../../../node_modules/.pnpm/@types+json-schema@7.0.15/node_modules/@types/json-schema/index.d.ts
4
+ // ==================================================================================================
5
+ // JSON Schema Draft 07
6
+ // ==================================================================================================
7
+ // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
8
+ // --------------------------------------------------------------------------------------------------
9
+ /**
10
+ * Primitive type
11
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
12
+ */
13
+ type JSONSchema7TypeName = "string" //
14
+ | "number" | "integer" | "boolean" | "object" | "array" | "null";
15
+ /**
16
+ * Primitive type
17
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
18
+ */
19
+ type JSONSchema7Type = string //
20
+ | number | boolean | JSONSchema7Object | JSONSchema7Array | null;
21
+ // Workaround for infinite type recursion
22
+ interface JSONSchema7Object {
23
+ [key: string]: JSONSchema7Type;
24
+ }
25
+ // Workaround for infinite type recursion
26
+ // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
27
+ interface JSONSchema7Array extends Array<JSONSchema7Type> {}
28
+ /**
29
+ * Meta schema
30
+ *
31
+ * Recommended values:
32
+ * - 'http://json-schema.org/schema#'
33
+ * - 'http://json-schema.org/hyper-schema#'
34
+ * - 'http://json-schema.org/draft-07/schema#'
35
+ * - 'http://json-schema.org/draft-07/hyper-schema#'
36
+ *
37
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
38
+ */
39
+ type JSONSchema7Version = string;
40
+ /**
41
+ * JSON Schema v7
42
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
43
+ */
44
+ type JSONSchema7Definition = JSONSchema7 | boolean;
45
+ interface JSONSchema7 {
46
+ $id?: string | undefined;
47
+ $ref?: string | undefined;
48
+ $schema?: JSONSchema7Version | undefined;
49
+ $comment?: string | undefined;
50
+ /**
51
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
52
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
53
+ */
54
+ $defs?: {
55
+ [key: string]: JSONSchema7Definition;
56
+ } | undefined;
57
+ /**
58
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
59
+ */
60
+ type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
61
+ enum?: JSONSchema7Type[] | undefined;
62
+ const?: JSONSchema7Type | undefined;
63
+ /**
64
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
65
+ */
66
+ multipleOf?: number | undefined;
67
+ maximum?: number | undefined;
68
+ exclusiveMaximum?: number | undefined;
69
+ minimum?: number | undefined;
70
+ exclusiveMinimum?: number | undefined;
71
+ /**
72
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
73
+ */
74
+ maxLength?: number | undefined;
75
+ minLength?: number | undefined;
76
+ pattern?: string | undefined;
77
+ /**
78
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
79
+ */
80
+ items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
81
+ additionalItems?: JSONSchema7Definition | undefined;
82
+ maxItems?: number | undefined;
83
+ minItems?: number | undefined;
84
+ uniqueItems?: boolean | undefined;
85
+ contains?: JSONSchema7Definition | undefined;
86
+ /**
87
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
88
+ */
89
+ maxProperties?: number | undefined;
90
+ minProperties?: number | undefined;
91
+ required?: string[] | undefined;
92
+ properties?: {
93
+ [key: string]: JSONSchema7Definition;
94
+ } | undefined;
95
+ patternProperties?: {
96
+ [key: string]: JSONSchema7Definition;
97
+ } | undefined;
98
+ additionalProperties?: JSONSchema7Definition | undefined;
99
+ dependencies?: {
100
+ [key: string]: JSONSchema7Definition | string[];
101
+ } | undefined;
102
+ propertyNames?: JSONSchema7Definition | undefined;
103
+ /**
104
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
105
+ */
106
+ if?: JSONSchema7Definition | undefined;
107
+ then?: JSONSchema7Definition | undefined;
108
+ else?: JSONSchema7Definition | undefined;
109
+ /**
110
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
111
+ */
112
+ allOf?: JSONSchema7Definition[] | undefined;
113
+ anyOf?: JSONSchema7Definition[] | undefined;
114
+ oneOf?: JSONSchema7Definition[] | undefined;
115
+ not?: JSONSchema7Definition | undefined;
116
+ /**
117
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
118
+ */
119
+ format?: string | undefined;
120
+ /**
121
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
122
+ */
123
+ contentMediaType?: string | undefined;
124
+ contentEncoding?: string | undefined;
125
+ /**
126
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
127
+ */
128
+ definitions?: {
129
+ [key: string]: JSONSchema7Definition;
130
+ } | undefined;
131
+ /**
132
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
133
+ */
134
+ title?: string | undefined;
135
+ description?: string | undefined;
136
+ default?: JSONSchema7Type | undefined;
137
+ readOnly?: boolean | undefined;
138
+ writeOnly?: boolean | undefined;
139
+ examples?: JSONSchema7Type | undefined;
140
+ }
141
+ //#endregion
142
+ //#region src/context.d.ts
143
+ type ParserOverrideOf<S> = (schema: JsonSchemaObject, refs: RefsOf<S>) => S | undefined;
144
+ type OptionsOf<S> = {
145
+ withoutDefaults?: boolean;
146
+ withoutDescribes?: boolean;
147
+ parserOverride?: ParserOverrideOf<S>;
148
+ depth?: number;
149
+ };
150
+ type RefsOf<S> = OptionsOf<S> & {
151
+ path: Array<string | number>;
152
+ seen: Map<object | boolean, {
153
+ n: number;
154
+ r: S | undefined;
155
+ }>;
156
+ /** The document that local `$ref` pointers resolve against. */
157
+ root?: JsonSchema;
158
+ };
159
+ //#endregion
160
+ //#region src/types.d.ts
161
+ type Serializable = {
162
+ [key: string]: Serializable;
163
+ } | Serializable[] | string | number | boolean | null;
164
+ type JsonSchema = JsonSchemaObject | boolean;
165
+ type JsonSchemaDefinitions = Record<string, JsonSchema>;
166
+ interface JsonSchemaObject extends Omit<JSONSchema7, '$defs' | 'items' | 'additionalItems' | 'contains' | 'properties' | 'patternProperties' | 'additionalProperties' | 'dependencies' | 'propertyNames' | 'if' | 'then' | 'else' | 'allOf' | 'anyOf' | 'oneOf' | 'not' | 'definitions' | 'exclusiveMinimum' | 'exclusiveMaximum'> {
167
+ min?: number;
168
+ max?: number;
169
+ example?: Serializable;
170
+ /** Draft 4/OpenAPI boolean spellings ride beside the Draft 7 numeric ones. */
171
+ exclusiveMinimum?: number | boolean;
172
+ exclusiveMaximum?: number | boolean;
173
+ $defs?: JsonSchemaDefinitions;
174
+ definitions?: JsonSchemaDefinitions;
175
+ items?: JsonSchema | JsonSchema[];
176
+ additionalItems?: JsonSchema;
177
+ contains?: JsonSchema;
178
+ properties?: JsonSchemaDefinitions;
179
+ patternProperties?: JsonSchemaDefinitions;
180
+ additionalProperties?: JsonSchema;
181
+ dependencies?: Record<string, JsonSchema | string[]>;
182
+ propertyNames?: JsonSchema;
183
+ if?: JsonSchema;
184
+ then?: JsonSchema;
185
+ else?: JsonSchema;
186
+ allOf?: JsonSchema[];
187
+ anyOf?: JsonSchema[];
188
+ oneOf?: JsonSchema[];
189
+ not?: JsonSchema;
190
+ }
191
+ type ParserSelector = (schema: JsonSchemaObject, refs: Refs) => ZodTypeAny;
192
+ type ParserOverride = ParserOverrideOf<ZodTypeAny>;
193
+ type JsonSchemaToZodOptions = OptionsOf<ZodTypeAny>;
194
+ type Refs = RefsOf<ZodTypeAny>;
195
+ //#endregion
196
+ //#region src/utils/compile-pattern.d.ts
197
+ type InvalidPatternReason = 'syntax' | 'too-long' | 'unsupported';
198
+ /**
199
+ * Raised at conversion time when a schema `pattern` cannot be turned into a
200
+ * `RegExp`. Conversion fails eagerly, in line with how this package and
201
+ * `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
202
+ * schema is the tool author's bug, so it surfaces once at construction rather
203
+ * than on every validation as if the caller's input were wrong.
204
+ */
205
+ declare class InvalidPatternError extends Error {
206
+ readonly keyword: 'pattern' | 'patternProperties';
207
+ readonly pattern: string;
208
+ readonly path: ReadonlyArray<string | number>;
209
+ readonly reason: InvalidPatternReason;
210
+ readonly name = "InvalidPatternError";
211
+ constructor(keyword: 'pattern' | 'patternProperties', pattern: string, path: ReadonlyArray<string | number>, reason: InvalidPatternReason, detail: string, options?: ErrorOptions);
212
+ }
213
+ //#endregion
214
+ export { JsonSchemaToZodOptions as a, Refs as c, ParserOverrideOf as d, RefsOf as f, JsonSchemaObject as i, Serializable as l, InvalidPatternReason as n, ParserOverride as o, JsonSchema as r, ParserSelector as s, InvalidPatternError as t, OptionsOf as u };