@composio/json-schema-to-zod 0.3.4 → 0.4.1
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 +31 -3
- package/dist/compile-pattern-D-zr7Yui.d.mts +206 -0
- package/dist/convert-Cte4lEaK.mjs +1768 -0
- package/dist/index.d.mts +4 -209
- package/dist/index.mjs +103 -1731
- package/dist/v4.d.mts +14 -0
- package/dist/v4.mjs +246 -0
- package/package.json +16 -7
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(
|
|
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 `
|
|
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,206 @@
|
|
|
1
|
+
import { ZodTypeAny } from "zod/v3";
|
|
2
|
+
import "re2js";
|
|
3
|
+
// ==================================================================================================
|
|
4
|
+
// JSON Schema Draft 07
|
|
5
|
+
// ==================================================================================================
|
|
6
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
7
|
+
// --------------------------------------------------------------------------------------------------
|
|
8
|
+
/**
|
|
9
|
+
* Primitive type
|
|
10
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
11
|
+
*/
|
|
12
|
+
type JSONSchema7TypeName = "string" //
|
|
13
|
+
| "number" | "integer" | "boolean" | "object" | "array" | "null";
|
|
14
|
+
/**
|
|
15
|
+
* Primitive type
|
|
16
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
17
|
+
*/
|
|
18
|
+
type JSONSchema7Type = string //
|
|
19
|
+
| number | boolean | JSONSchema7Object | JSONSchema7Array | null;
|
|
20
|
+
// Workaround for infinite type recursion
|
|
21
|
+
interface JSONSchema7Object {
|
|
22
|
+
[key: string]: JSONSchema7Type;
|
|
23
|
+
}
|
|
24
|
+
// Workaround for infinite type recursion
|
|
25
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
26
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
27
|
+
/**
|
|
28
|
+
* Meta schema
|
|
29
|
+
*
|
|
30
|
+
* Recommended values:
|
|
31
|
+
* - 'http://json-schema.org/schema#'
|
|
32
|
+
* - 'http://json-schema.org/hyper-schema#'
|
|
33
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
|
34
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
35
|
+
*
|
|
36
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
37
|
+
*/
|
|
38
|
+
type JSONSchema7Version = string;
|
|
39
|
+
/**
|
|
40
|
+
* JSON Schema v7
|
|
41
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
42
|
+
*/
|
|
43
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
44
|
+
interface JSONSchema7 {
|
|
45
|
+
$id?: string | undefined;
|
|
46
|
+
$ref?: string | undefined;
|
|
47
|
+
$schema?: JSONSchema7Version | undefined;
|
|
48
|
+
$comment?: string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
51
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
52
|
+
*/
|
|
53
|
+
$defs?: {
|
|
54
|
+
[key: string]: JSONSchema7Definition;
|
|
55
|
+
} | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
58
|
+
*/
|
|
59
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
60
|
+
enum?: JSONSchema7Type[] | undefined;
|
|
61
|
+
const?: JSONSchema7Type | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
64
|
+
*/
|
|
65
|
+
multipleOf?: number | undefined;
|
|
66
|
+
maximum?: number | undefined;
|
|
67
|
+
exclusiveMaximum?: number | undefined;
|
|
68
|
+
minimum?: number | undefined;
|
|
69
|
+
exclusiveMinimum?: number | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
72
|
+
*/
|
|
73
|
+
maxLength?: number | undefined;
|
|
74
|
+
minLength?: number | undefined;
|
|
75
|
+
pattern?: string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
78
|
+
*/
|
|
79
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
80
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
|
81
|
+
maxItems?: number | undefined;
|
|
82
|
+
minItems?: number | undefined;
|
|
83
|
+
uniqueItems?: boolean | undefined;
|
|
84
|
+
contains?: JSONSchema7Definition | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
87
|
+
*/
|
|
88
|
+
maxProperties?: number | undefined;
|
|
89
|
+
minProperties?: number | undefined;
|
|
90
|
+
required?: string[] | undefined;
|
|
91
|
+
properties?: {
|
|
92
|
+
[key: string]: JSONSchema7Definition;
|
|
93
|
+
} | undefined;
|
|
94
|
+
patternProperties?: {
|
|
95
|
+
[key: string]: JSONSchema7Definition;
|
|
96
|
+
} | undefined;
|
|
97
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
|
98
|
+
dependencies?: {
|
|
99
|
+
[key: string]: JSONSchema7Definition | string[];
|
|
100
|
+
} | undefined;
|
|
101
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
104
|
+
*/
|
|
105
|
+
if?: JSONSchema7Definition | undefined;
|
|
106
|
+
then?: JSONSchema7Definition | undefined;
|
|
107
|
+
else?: JSONSchema7Definition | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
110
|
+
*/
|
|
111
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
|
112
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
|
113
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
|
114
|
+
not?: JSONSchema7Definition | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
117
|
+
*/
|
|
118
|
+
format?: string | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
121
|
+
*/
|
|
122
|
+
contentMediaType?: string | undefined;
|
|
123
|
+
contentEncoding?: string | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
126
|
+
*/
|
|
127
|
+
definitions?: {
|
|
128
|
+
[key: string]: JSONSchema7Definition;
|
|
129
|
+
} | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
132
|
+
*/
|
|
133
|
+
title?: string | undefined;
|
|
134
|
+
description?: string | undefined;
|
|
135
|
+
default?: JSONSchema7Type | undefined;
|
|
136
|
+
readOnly?: boolean | undefined;
|
|
137
|
+
writeOnly?: boolean | undefined;
|
|
138
|
+
examples?: JSONSchema7Type | undefined;
|
|
139
|
+
}
|
|
140
|
+
type ParserOverrideOf<S> = (schema: JsonSchemaObject, refs: RefsOf<S>) => S | undefined;
|
|
141
|
+
type OptionsOf<S> = {
|
|
142
|
+
withoutDefaults?: boolean;
|
|
143
|
+
withoutDescribes?: boolean;
|
|
144
|
+
parserOverride?: ParserOverrideOf<S>;
|
|
145
|
+
depth?: number;
|
|
146
|
+
};
|
|
147
|
+
type RefsOf<S> = OptionsOf<S> & {
|
|
148
|
+
path: Array<string | number>;
|
|
149
|
+
seen: Map<object | boolean, {
|
|
150
|
+
n: number;
|
|
151
|
+
r: S | undefined;
|
|
152
|
+
}>;
|
|
153
|
+
/** The document that local `$ref` pointers resolve against. */
|
|
154
|
+
root?: JsonSchema;
|
|
155
|
+
};
|
|
156
|
+
type Serializable = {
|
|
157
|
+
[key: string]: Serializable;
|
|
158
|
+
} | Serializable[] | string | number | boolean | null;
|
|
159
|
+
type JsonSchema = JsonSchemaObject | boolean;
|
|
160
|
+
type JsonSchemaDefinitions = Record<string, JsonSchema>;
|
|
161
|
+
interface JsonSchemaObject extends Omit<JSONSchema7, '$defs' | 'items' | 'additionalItems' | 'contains' | 'properties' | 'patternProperties' | 'additionalProperties' | 'dependencies' | 'propertyNames' | 'if' | 'then' | 'else' | 'allOf' | 'anyOf' | 'oneOf' | 'not' | 'definitions' | 'exclusiveMinimum' | 'exclusiveMaximum'> {
|
|
162
|
+
min?: number;
|
|
163
|
+
max?: number;
|
|
164
|
+
example?: Serializable;
|
|
165
|
+
/** Draft 4/OpenAPI boolean spellings ride beside the Draft 7 numeric ones. */
|
|
166
|
+
exclusiveMinimum?: number | boolean;
|
|
167
|
+
exclusiveMaximum?: number | boolean;
|
|
168
|
+
$defs?: JsonSchemaDefinitions;
|
|
169
|
+
definitions?: JsonSchemaDefinitions;
|
|
170
|
+
items?: JsonSchema | JsonSchema[];
|
|
171
|
+
additionalItems?: JsonSchema;
|
|
172
|
+
contains?: JsonSchema;
|
|
173
|
+
properties?: JsonSchemaDefinitions;
|
|
174
|
+
patternProperties?: JsonSchemaDefinitions;
|
|
175
|
+
additionalProperties?: JsonSchema;
|
|
176
|
+
dependencies?: Record<string, JsonSchema | string[]>;
|
|
177
|
+
propertyNames?: JsonSchema;
|
|
178
|
+
if?: JsonSchema;
|
|
179
|
+
then?: JsonSchema;
|
|
180
|
+
else?: JsonSchema;
|
|
181
|
+
allOf?: JsonSchema[];
|
|
182
|
+
anyOf?: JsonSchema[];
|
|
183
|
+
oneOf?: JsonSchema[];
|
|
184
|
+
not?: JsonSchema;
|
|
185
|
+
}
|
|
186
|
+
type ParserSelector = (schema: JsonSchemaObject, refs: Refs) => ZodTypeAny;
|
|
187
|
+
type ParserOverride = ParserOverrideOf<ZodTypeAny>;
|
|
188
|
+
type JsonSchemaToZodOptions = OptionsOf<ZodTypeAny>;
|
|
189
|
+
type Refs = RefsOf<ZodTypeAny>;
|
|
190
|
+
type InvalidPatternReason = 'syntax' | 'too-long' | 'unsupported';
|
|
191
|
+
/**
|
|
192
|
+
* Raised at conversion time when a schema `pattern` cannot be turned into a
|
|
193
|
+
* `RegExp`. Conversion fails eagerly, in line with how this package and
|
|
194
|
+
* `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
|
|
195
|
+
* schema is the tool author's bug, so it surfaces once at construction rather
|
|
196
|
+
* than on every validation as if the caller's input were wrong.
|
|
197
|
+
*/
|
|
198
|
+
declare class InvalidPatternError extends Error {
|
|
199
|
+
readonly keyword: 'pattern' | 'patternProperties';
|
|
200
|
+
readonly pattern: string;
|
|
201
|
+
readonly path: ReadonlyArray<string | number>;
|
|
202
|
+
readonly reason: InvalidPatternReason;
|
|
203
|
+
readonly name = "InvalidPatternError";
|
|
204
|
+
constructor(keyword: 'pattern' | 'patternProperties', pattern: string, path: ReadonlyArray<string | number>, reason: InvalidPatternReason, detail: string, options?: ErrorOptions);
|
|
205
|
+
}
|
|
206
|
+
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 };
|