@temporary-name/json-schema 1.9.3-alpha.4275e976ddda4d8be107c2cfde9899bdea9a337d → 1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e
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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/zod4/index.d.mts +304 -0
- package/dist/zod4/index.d.ts +304 -0
- package/dist/zod4/index.mjs +487 -0
- package/package.json +13 -10
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as Draft07 from '@temporary-name/interop/json-schema-typed/draft-07';
|
|
2
2
|
import * as Draft2019 from '@temporary-name/interop/json-schema-typed/draft-2019-09';
|
|
3
3
|
import * as Draft2020 from '@temporary-name/interop/json-schema-typed/draft-2020-12';
|
|
4
|
-
import { ConditionalSchemaConverter } from '@temporary-name/openapi';
|
|
5
4
|
import { Context } from '@temporary-name/server';
|
|
5
|
+
import { ConditionalSchemaConverter } from '@temporary-name/server/openapi';
|
|
6
6
|
import { StandardHandlerPlugin, StandardHandlerOptions } from '@temporary-name/server/standard';
|
|
7
7
|
|
|
8
8
|
type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as Draft07 from '@temporary-name/interop/json-schema-typed/draft-07';
|
|
2
2
|
import * as Draft2019 from '@temporary-name/interop/json-schema-typed/draft-2019-09';
|
|
3
3
|
import * as Draft2020 from '@temporary-name/interop/json-schema-typed/draft-2020-12';
|
|
4
|
-
import { ConditionalSchemaConverter } from '@temporary-name/openapi';
|
|
5
4
|
import { Context } from '@temporary-name/server';
|
|
5
|
+
import { ConditionalSchemaConverter } from '@temporary-name/server/openapi';
|
|
6
6
|
import { StandardHandlerPlugin, StandardHandlerOptions } from '@temporary-name/server/standard';
|
|
7
7
|
|
|
8
8
|
type JsonSchema = Draft2020.JSONSchema | Draft2019.JSONSchema | Draft07.JSONSchema;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isObject, toArray, guard } from '@temporary-name/shared';
|
|
2
|
-
import { CompositeSchemaConverter } from '@temporary-name/openapi';
|
|
2
|
+
import { CompositeSchemaConverter } from '@temporary-name/server/openapi';
|
|
3
3
|
|
|
4
4
|
var JsonSchemaXNativeType = /* @__PURE__ */ ((JsonSchemaXNativeType2) => {
|
|
5
5
|
JsonSchemaXNativeType2["BigInt"] = "bigint";
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { JSONSchema, SchemaConvertOptions, ConditionalSchemaConverter } from '@temporary-name/server/openapi';
|
|
2
|
+
import { Interceptor } from '@temporary-name/shared';
|
|
3
|
+
import { AnySchema } from '@temporary-name/zod';
|
|
4
|
+
import * as zod_v4_core from 'zod/v4/core';
|
|
5
|
+
import { $ZodType, $input, $output } from 'zod/v4/core';
|
|
6
|
+
|
|
7
|
+
interface ZodToJsonSchemaConverterOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Max depth of lazy type.
|
|
10
|
+
*
|
|
11
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
|
12
|
+
*
|
|
13
|
+
* @default 2
|
|
14
|
+
*/
|
|
15
|
+
maxLazyDepth?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Max depth of nested types.
|
|
18
|
+
*
|
|
19
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
|
20
|
+
*
|
|
21
|
+
* @default 10
|
|
22
|
+
*/
|
|
23
|
+
maxStructureDepth?: number;
|
|
24
|
+
/**
|
|
25
|
+
* The schema to be used to represent the any | unknown type.
|
|
26
|
+
*
|
|
27
|
+
* @default { }
|
|
28
|
+
*/
|
|
29
|
+
anyJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
30
|
+
/**
|
|
31
|
+
* The schema to be used when the Zod schema is unsupported.
|
|
32
|
+
*
|
|
33
|
+
* @default { not: {} }
|
|
34
|
+
*/
|
|
35
|
+
unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
36
|
+
/**
|
|
37
|
+
* The schema to be used to represent the undefined type.
|
|
38
|
+
*
|
|
39
|
+
* @default { not: {} }
|
|
40
|
+
*/
|
|
41
|
+
undefinedJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
42
|
+
interceptors?: Interceptor<{
|
|
43
|
+
schema: $ZodType;
|
|
44
|
+
options: SchemaConvertOptions;
|
|
45
|
+
lazyDepth: number;
|
|
46
|
+
isHandledCustomJSONSchema: boolean;
|
|
47
|
+
}, [
|
|
48
|
+
required: boolean,
|
|
49
|
+
jsonSchema: Exclude<JSONSchema, boolean>
|
|
50
|
+
]>[];
|
|
51
|
+
}
|
|
52
|
+
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
|
53
|
+
#private;
|
|
54
|
+
private readonly maxLazyDepth;
|
|
55
|
+
private readonly maxStructureDepth;
|
|
56
|
+
private readonly anyJsonSchema;
|
|
57
|
+
private readonly unsupportedJsonSchema;
|
|
58
|
+
private readonly undefinedJsonSchema;
|
|
59
|
+
private readonly interceptors;
|
|
60
|
+
constructor(options?: ZodToJsonSchemaConverterOptions);
|
|
61
|
+
condition(schema: AnySchema | undefined): boolean;
|
|
62
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Zod registry for customizing generated JSON schema, can use both for .input and .output
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { JSON_SCHEMA_REGISTRY } from '@temporary-name/zod/zod4'
|
|
71
|
+
*
|
|
72
|
+
* const user = z.object({
|
|
73
|
+
* name: z.string(),
|
|
74
|
+
* age: z.number(),
|
|
75
|
+
* })
|
|
76
|
+
*
|
|
77
|
+
* JSON_SCHEMA_REGISTRY.add(user, {
|
|
78
|
+
* examples: [{ name: 'John', age: 20 }],
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare const JSON_SCHEMA_REGISTRY: zod_v4_core.$ZodRegistry<{
|
|
83
|
+
$anchor?: string;
|
|
84
|
+
$comment?: string;
|
|
85
|
+
$defs?: Record<string, JSONSchema>;
|
|
86
|
+
$dynamicAnchor?: string;
|
|
87
|
+
$dynamicRef?: string;
|
|
88
|
+
$id?: string;
|
|
89
|
+
$ref?: string;
|
|
90
|
+
$schema?: string;
|
|
91
|
+
$vocabulary?: Record<string, string>;
|
|
92
|
+
additionalItems?: JSONSchema;
|
|
93
|
+
additionalProperties?: JSONSchema;
|
|
94
|
+
allOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
95
|
+
anyOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
96
|
+
const?: typeof $input | typeof $output | undefined;
|
|
97
|
+
contains?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
98
|
+
contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
|
|
99
|
+
contentMediaType?: string;
|
|
100
|
+
contentSchema?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
101
|
+
default?: typeof $input | typeof $output | undefined;
|
|
102
|
+
definitions?: Record<string, JSONSchema>;
|
|
103
|
+
dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
|
|
104
|
+
dependentRequired?: Record<string, string[] | readonly string[]>;
|
|
105
|
+
dependentSchemas?: Record<string, JSONSchema>;
|
|
106
|
+
deprecated?: boolean;
|
|
107
|
+
description?: string;
|
|
108
|
+
else?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
109
|
+
enum?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
|
|
110
|
+
examples?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
|
|
111
|
+
exclusiveMaximum?: number;
|
|
112
|
+
exclusiveMinimum?: number;
|
|
113
|
+
format?: string;
|
|
114
|
+
if?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
115
|
+
items?: JSONSchema;
|
|
116
|
+
maxContains?: number;
|
|
117
|
+
maximum?: number;
|
|
118
|
+
maxItems?: number;
|
|
119
|
+
maxLength?: number;
|
|
120
|
+
maxProperties?: number;
|
|
121
|
+
minContains?: number;
|
|
122
|
+
minimum?: number;
|
|
123
|
+
minItems?: number;
|
|
124
|
+
minLength?: number;
|
|
125
|
+
minProperties?: number;
|
|
126
|
+
multipleOf?: number;
|
|
127
|
+
not?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
128
|
+
oneOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
129
|
+
pattern?: string;
|
|
130
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
131
|
+
prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
|
|
132
|
+
properties?: Record<string, JSONSchema>;
|
|
133
|
+
propertyNames?: JSONSchema;
|
|
134
|
+
readOnly?: boolean;
|
|
135
|
+
required?: string[] | readonly string[];
|
|
136
|
+
then?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
137
|
+
title?: string;
|
|
138
|
+
type?: JSONSchema.TypeValue | undefined;
|
|
139
|
+
unevaluatedItems?: JSONSchema;
|
|
140
|
+
unevaluatedProperties?: JSONSchema;
|
|
141
|
+
uniqueItems?: boolean;
|
|
142
|
+
writeOnly?: boolean;
|
|
143
|
+
}, zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>>;
|
|
144
|
+
/**
|
|
145
|
+
* Zod registry for customizing generated JSON schema, only useful for .input
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { JSON_SCHEMA_INPUT_REGISTRY } from '@temporary-name/zod/zod4'
|
|
150
|
+
*
|
|
151
|
+
* const user = z.object({
|
|
152
|
+
* name: z.string(),
|
|
153
|
+
* age: z.string().transform(v => Number(v)),
|
|
154
|
+
* })
|
|
155
|
+
*
|
|
156
|
+
* JSON_SCHEMA_REGISTRY.add(user, {
|
|
157
|
+
* examples: [{ name: 'John', age: "20" }],
|
|
158
|
+
* })
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
declare const JSON_SCHEMA_INPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
|
|
162
|
+
$anchor?: string;
|
|
163
|
+
$comment?: string;
|
|
164
|
+
$defs?: Record<string, JSONSchema>;
|
|
165
|
+
$dynamicAnchor?: string;
|
|
166
|
+
$dynamicRef?: string;
|
|
167
|
+
$id?: string;
|
|
168
|
+
$ref?: string;
|
|
169
|
+
$schema?: string;
|
|
170
|
+
$vocabulary?: Record<string, string>;
|
|
171
|
+
additionalItems?: JSONSchema;
|
|
172
|
+
additionalProperties?: JSONSchema;
|
|
173
|
+
allOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
|
|
174
|
+
anyOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
|
|
175
|
+
const?: typeof $input | undefined;
|
|
176
|
+
contains?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
177
|
+
contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
|
|
178
|
+
contentMediaType?: string;
|
|
179
|
+
contentSchema?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
180
|
+
default?: typeof $input | undefined;
|
|
181
|
+
definitions?: Record<string, JSONSchema>;
|
|
182
|
+
dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
|
|
183
|
+
dependentRequired?: Record<string, string[] | readonly string[]>;
|
|
184
|
+
dependentSchemas?: Record<string, JSONSchema>;
|
|
185
|
+
deprecated?: boolean;
|
|
186
|
+
description?: string;
|
|
187
|
+
else?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
188
|
+
enum?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
|
|
189
|
+
examples?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
|
|
190
|
+
exclusiveMaximum?: number;
|
|
191
|
+
exclusiveMinimum?: number;
|
|
192
|
+
format?: string;
|
|
193
|
+
if?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
194
|
+
items?: JSONSchema;
|
|
195
|
+
maxContains?: number;
|
|
196
|
+
maximum?: number;
|
|
197
|
+
maxItems?: number;
|
|
198
|
+
maxLength?: number;
|
|
199
|
+
maxProperties?: number;
|
|
200
|
+
minContains?: number;
|
|
201
|
+
minimum?: number;
|
|
202
|
+
minItems?: number;
|
|
203
|
+
minLength?: number;
|
|
204
|
+
minProperties?: number;
|
|
205
|
+
multipleOf?: number;
|
|
206
|
+
not?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
207
|
+
oneOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
|
|
208
|
+
pattern?: string;
|
|
209
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
210
|
+
prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
|
|
211
|
+
properties?: Record<string, JSONSchema>;
|
|
212
|
+
propertyNames?: JSONSchema;
|
|
213
|
+
readOnly?: boolean;
|
|
214
|
+
required?: string[] | readonly string[];
|
|
215
|
+
then?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
216
|
+
title?: string;
|
|
217
|
+
type?: JSONSchema.TypeValue | undefined;
|
|
218
|
+
unevaluatedItems?: JSONSchema;
|
|
219
|
+
unevaluatedProperties?: JSONSchema;
|
|
220
|
+
uniqueItems?: boolean;
|
|
221
|
+
writeOnly?: boolean;
|
|
222
|
+
}, zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>>;
|
|
223
|
+
/**
|
|
224
|
+
* Zod registry for customizing generated JSON schema, only useful for .input
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* import { JSON_SCHEMA_OUTPUT_REGISTRY } from '@temporary-name/zod/zod4'
|
|
229
|
+
*
|
|
230
|
+
* const user = z.object({
|
|
231
|
+
* name: z.string(),
|
|
232
|
+
* age: z.string().transform(v => Number(v)),
|
|
233
|
+
* })
|
|
234
|
+
*
|
|
235
|
+
* JSON_SCHEMA_OUTPUT_REGISTRY.add(user, {
|
|
236
|
+
* examples: [{ name: 'John', age: 20 }],
|
|
237
|
+
* })
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
declare const JSON_SCHEMA_OUTPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
|
|
241
|
+
$anchor?: string;
|
|
242
|
+
$comment?: string;
|
|
243
|
+
$defs?: Record<string, JSONSchema>;
|
|
244
|
+
$dynamicAnchor?: string;
|
|
245
|
+
$dynamicRef?: string;
|
|
246
|
+
$id?: string;
|
|
247
|
+
$ref?: string;
|
|
248
|
+
$schema?: string;
|
|
249
|
+
$vocabulary?: Record<string, string>;
|
|
250
|
+
additionalItems?: JSONSchema;
|
|
251
|
+
additionalProperties?: JSONSchema;
|
|
252
|
+
allOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
253
|
+
anyOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
254
|
+
const?: typeof $output | undefined;
|
|
255
|
+
contains?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
256
|
+
contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
|
|
257
|
+
contentMediaType?: string;
|
|
258
|
+
contentSchema?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
259
|
+
default?: typeof $output | undefined;
|
|
260
|
+
definitions?: Record<string, JSONSchema>;
|
|
261
|
+
dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
|
|
262
|
+
dependentRequired?: Record<string, string[] | readonly string[]>;
|
|
263
|
+
dependentSchemas?: Record<string, JSONSchema>;
|
|
264
|
+
deprecated?: boolean;
|
|
265
|
+
description?: string;
|
|
266
|
+
else?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
267
|
+
enum?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
|
|
268
|
+
examples?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
|
|
269
|
+
exclusiveMaximum?: number;
|
|
270
|
+
exclusiveMinimum?: number;
|
|
271
|
+
format?: string;
|
|
272
|
+
if?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
273
|
+
items?: JSONSchema;
|
|
274
|
+
maxContains?: number;
|
|
275
|
+
maximum?: number;
|
|
276
|
+
maxItems?: number;
|
|
277
|
+
maxLength?: number;
|
|
278
|
+
maxProperties?: number;
|
|
279
|
+
minContains?: number;
|
|
280
|
+
minimum?: number;
|
|
281
|
+
minItems?: number;
|
|
282
|
+
minLength?: number;
|
|
283
|
+
minProperties?: number;
|
|
284
|
+
multipleOf?: number;
|
|
285
|
+
not?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
286
|
+
oneOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
287
|
+
pattern?: string;
|
|
288
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
289
|
+
prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
|
|
290
|
+
properties?: Record<string, JSONSchema>;
|
|
291
|
+
propertyNames?: JSONSchema;
|
|
292
|
+
readOnly?: boolean;
|
|
293
|
+
required?: string[] | readonly string[];
|
|
294
|
+
then?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
295
|
+
title?: string;
|
|
296
|
+
type?: JSONSchema.TypeValue | undefined;
|
|
297
|
+
unevaluatedItems?: JSONSchema;
|
|
298
|
+
unevaluatedProperties?: JSONSchema;
|
|
299
|
+
uniqueItems?: boolean;
|
|
300
|
+
writeOnly?: boolean;
|
|
301
|
+
}, zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>>;
|
|
302
|
+
|
|
303
|
+
export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY, ZodToJsonSchemaConverter };
|
|
304
|
+
export type { ZodToJsonSchemaConverterOptions };
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { JSONSchema, SchemaConvertOptions, ConditionalSchemaConverter } from '@temporary-name/server/openapi';
|
|
2
|
+
import { Interceptor } from '@temporary-name/shared';
|
|
3
|
+
import { AnySchema } from '@temporary-name/zod';
|
|
4
|
+
import * as zod_v4_core from 'zod/v4/core';
|
|
5
|
+
import { $ZodType, $input, $output } from 'zod/v4/core';
|
|
6
|
+
|
|
7
|
+
interface ZodToJsonSchemaConverterOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Max depth of lazy type.
|
|
10
|
+
*
|
|
11
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
|
12
|
+
*
|
|
13
|
+
* @default 2
|
|
14
|
+
*/
|
|
15
|
+
maxLazyDepth?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Max depth of nested types.
|
|
18
|
+
*
|
|
19
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
|
20
|
+
*
|
|
21
|
+
* @default 10
|
|
22
|
+
*/
|
|
23
|
+
maxStructureDepth?: number;
|
|
24
|
+
/**
|
|
25
|
+
* The schema to be used to represent the any | unknown type.
|
|
26
|
+
*
|
|
27
|
+
* @default { }
|
|
28
|
+
*/
|
|
29
|
+
anyJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
30
|
+
/**
|
|
31
|
+
* The schema to be used when the Zod schema is unsupported.
|
|
32
|
+
*
|
|
33
|
+
* @default { not: {} }
|
|
34
|
+
*/
|
|
35
|
+
unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
36
|
+
/**
|
|
37
|
+
* The schema to be used to represent the undefined type.
|
|
38
|
+
*
|
|
39
|
+
* @default { not: {} }
|
|
40
|
+
*/
|
|
41
|
+
undefinedJsonSchema?: Exclude<JSONSchema, boolean>;
|
|
42
|
+
interceptors?: Interceptor<{
|
|
43
|
+
schema: $ZodType;
|
|
44
|
+
options: SchemaConvertOptions;
|
|
45
|
+
lazyDepth: number;
|
|
46
|
+
isHandledCustomJSONSchema: boolean;
|
|
47
|
+
}, [
|
|
48
|
+
required: boolean,
|
|
49
|
+
jsonSchema: Exclude<JSONSchema, boolean>
|
|
50
|
+
]>[];
|
|
51
|
+
}
|
|
52
|
+
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
|
53
|
+
#private;
|
|
54
|
+
private readonly maxLazyDepth;
|
|
55
|
+
private readonly maxStructureDepth;
|
|
56
|
+
private readonly anyJsonSchema;
|
|
57
|
+
private readonly unsupportedJsonSchema;
|
|
58
|
+
private readonly undefinedJsonSchema;
|
|
59
|
+
private readonly interceptors;
|
|
60
|
+
constructor(options?: ZodToJsonSchemaConverterOptions);
|
|
61
|
+
condition(schema: AnySchema | undefined): boolean;
|
|
62
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Zod registry for customizing generated JSON schema, can use both for .input and .output
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { JSON_SCHEMA_REGISTRY } from '@temporary-name/zod/zod4'
|
|
71
|
+
*
|
|
72
|
+
* const user = z.object({
|
|
73
|
+
* name: z.string(),
|
|
74
|
+
* age: z.number(),
|
|
75
|
+
* })
|
|
76
|
+
*
|
|
77
|
+
* JSON_SCHEMA_REGISTRY.add(user, {
|
|
78
|
+
* examples: [{ name: 'John', age: 20 }],
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare const JSON_SCHEMA_REGISTRY: zod_v4_core.$ZodRegistry<{
|
|
83
|
+
$anchor?: string;
|
|
84
|
+
$comment?: string;
|
|
85
|
+
$defs?: Record<string, JSONSchema>;
|
|
86
|
+
$dynamicAnchor?: string;
|
|
87
|
+
$dynamicRef?: string;
|
|
88
|
+
$id?: string;
|
|
89
|
+
$ref?: string;
|
|
90
|
+
$schema?: string;
|
|
91
|
+
$vocabulary?: Record<string, string>;
|
|
92
|
+
additionalItems?: JSONSchema;
|
|
93
|
+
additionalProperties?: JSONSchema;
|
|
94
|
+
allOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
95
|
+
anyOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
96
|
+
const?: typeof $input | typeof $output | undefined;
|
|
97
|
+
contains?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
98
|
+
contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
|
|
99
|
+
contentMediaType?: string;
|
|
100
|
+
contentSchema?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
101
|
+
default?: typeof $input | typeof $output | undefined;
|
|
102
|
+
definitions?: Record<string, JSONSchema>;
|
|
103
|
+
dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
|
|
104
|
+
dependentRequired?: Record<string, string[] | readonly string[]>;
|
|
105
|
+
dependentSchemas?: Record<string, JSONSchema>;
|
|
106
|
+
deprecated?: boolean;
|
|
107
|
+
description?: string;
|
|
108
|
+
else?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
109
|
+
enum?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
|
|
110
|
+
examples?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
|
|
111
|
+
exclusiveMaximum?: number;
|
|
112
|
+
exclusiveMinimum?: number;
|
|
113
|
+
format?: string;
|
|
114
|
+
if?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
115
|
+
items?: JSONSchema;
|
|
116
|
+
maxContains?: number;
|
|
117
|
+
maximum?: number;
|
|
118
|
+
maxItems?: number;
|
|
119
|
+
maxLength?: number;
|
|
120
|
+
maxProperties?: number;
|
|
121
|
+
minContains?: number;
|
|
122
|
+
minimum?: number;
|
|
123
|
+
minItems?: number;
|
|
124
|
+
minLength?: number;
|
|
125
|
+
minProperties?: number;
|
|
126
|
+
multipleOf?: number;
|
|
127
|
+
not?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
128
|
+
oneOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
129
|
+
pattern?: string;
|
|
130
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
131
|
+
prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
|
|
132
|
+
properties?: Record<string, JSONSchema>;
|
|
133
|
+
propertyNames?: JSONSchema;
|
|
134
|
+
readOnly?: boolean;
|
|
135
|
+
required?: string[] | readonly string[];
|
|
136
|
+
then?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
|
|
137
|
+
title?: string;
|
|
138
|
+
type?: JSONSchema.TypeValue | undefined;
|
|
139
|
+
unevaluatedItems?: JSONSchema;
|
|
140
|
+
unevaluatedProperties?: JSONSchema;
|
|
141
|
+
uniqueItems?: boolean;
|
|
142
|
+
writeOnly?: boolean;
|
|
143
|
+
}, zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>>;
|
|
144
|
+
/**
|
|
145
|
+
* Zod registry for customizing generated JSON schema, only useful for .input
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { JSON_SCHEMA_INPUT_REGISTRY } from '@temporary-name/zod/zod4'
|
|
150
|
+
*
|
|
151
|
+
* const user = z.object({
|
|
152
|
+
* name: z.string(),
|
|
153
|
+
* age: z.string().transform(v => Number(v)),
|
|
154
|
+
* })
|
|
155
|
+
*
|
|
156
|
+
* JSON_SCHEMA_REGISTRY.add(user, {
|
|
157
|
+
* examples: [{ name: 'John', age: "20" }],
|
|
158
|
+
* })
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
declare const JSON_SCHEMA_INPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
|
|
162
|
+
$anchor?: string;
|
|
163
|
+
$comment?: string;
|
|
164
|
+
$defs?: Record<string, JSONSchema>;
|
|
165
|
+
$dynamicAnchor?: string;
|
|
166
|
+
$dynamicRef?: string;
|
|
167
|
+
$id?: string;
|
|
168
|
+
$ref?: string;
|
|
169
|
+
$schema?: string;
|
|
170
|
+
$vocabulary?: Record<string, string>;
|
|
171
|
+
additionalItems?: JSONSchema;
|
|
172
|
+
additionalProperties?: JSONSchema;
|
|
173
|
+
allOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
|
|
174
|
+
anyOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
|
|
175
|
+
const?: typeof $input | undefined;
|
|
176
|
+
contains?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
177
|
+
contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
|
|
178
|
+
contentMediaType?: string;
|
|
179
|
+
contentSchema?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
180
|
+
default?: typeof $input | undefined;
|
|
181
|
+
definitions?: Record<string, JSONSchema>;
|
|
182
|
+
dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
|
|
183
|
+
dependentRequired?: Record<string, string[] | readonly string[]>;
|
|
184
|
+
dependentSchemas?: Record<string, JSONSchema>;
|
|
185
|
+
deprecated?: boolean;
|
|
186
|
+
description?: string;
|
|
187
|
+
else?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
188
|
+
enum?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
|
|
189
|
+
examples?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
|
|
190
|
+
exclusiveMaximum?: number;
|
|
191
|
+
exclusiveMinimum?: number;
|
|
192
|
+
format?: string;
|
|
193
|
+
if?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
194
|
+
items?: JSONSchema;
|
|
195
|
+
maxContains?: number;
|
|
196
|
+
maximum?: number;
|
|
197
|
+
maxItems?: number;
|
|
198
|
+
maxLength?: number;
|
|
199
|
+
maxProperties?: number;
|
|
200
|
+
minContains?: number;
|
|
201
|
+
minimum?: number;
|
|
202
|
+
minItems?: number;
|
|
203
|
+
minLength?: number;
|
|
204
|
+
minProperties?: number;
|
|
205
|
+
multipleOf?: number;
|
|
206
|
+
not?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
207
|
+
oneOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
|
|
208
|
+
pattern?: string;
|
|
209
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
210
|
+
prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
|
|
211
|
+
properties?: Record<string, JSONSchema>;
|
|
212
|
+
propertyNames?: JSONSchema;
|
|
213
|
+
readOnly?: boolean;
|
|
214
|
+
required?: string[] | readonly string[];
|
|
215
|
+
then?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
|
|
216
|
+
title?: string;
|
|
217
|
+
type?: JSONSchema.TypeValue | undefined;
|
|
218
|
+
unevaluatedItems?: JSONSchema;
|
|
219
|
+
unevaluatedProperties?: JSONSchema;
|
|
220
|
+
uniqueItems?: boolean;
|
|
221
|
+
writeOnly?: boolean;
|
|
222
|
+
}, zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>>;
|
|
223
|
+
/**
|
|
224
|
+
* Zod registry for customizing generated JSON schema, only useful for .input
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* import { JSON_SCHEMA_OUTPUT_REGISTRY } from '@temporary-name/zod/zod4'
|
|
229
|
+
*
|
|
230
|
+
* const user = z.object({
|
|
231
|
+
* name: z.string(),
|
|
232
|
+
* age: z.string().transform(v => Number(v)),
|
|
233
|
+
* })
|
|
234
|
+
*
|
|
235
|
+
* JSON_SCHEMA_OUTPUT_REGISTRY.add(user, {
|
|
236
|
+
* examples: [{ name: 'John', age: 20 }],
|
|
237
|
+
* })
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
declare const JSON_SCHEMA_OUTPUT_REGISTRY: zod_v4_core.$ZodRegistry<{
|
|
241
|
+
$anchor?: string;
|
|
242
|
+
$comment?: string;
|
|
243
|
+
$defs?: Record<string, JSONSchema>;
|
|
244
|
+
$dynamicAnchor?: string;
|
|
245
|
+
$dynamicRef?: string;
|
|
246
|
+
$id?: string;
|
|
247
|
+
$ref?: string;
|
|
248
|
+
$schema?: string;
|
|
249
|
+
$vocabulary?: Record<string, string>;
|
|
250
|
+
additionalItems?: JSONSchema;
|
|
251
|
+
additionalProperties?: JSONSchema;
|
|
252
|
+
allOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
253
|
+
anyOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
254
|
+
const?: typeof $output | undefined;
|
|
255
|
+
contains?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
256
|
+
contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
|
|
257
|
+
contentMediaType?: string;
|
|
258
|
+
contentSchema?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
259
|
+
default?: typeof $output | undefined;
|
|
260
|
+
definitions?: Record<string, JSONSchema>;
|
|
261
|
+
dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
|
|
262
|
+
dependentRequired?: Record<string, string[] | readonly string[]>;
|
|
263
|
+
dependentSchemas?: Record<string, JSONSchema>;
|
|
264
|
+
deprecated?: boolean;
|
|
265
|
+
description?: string;
|
|
266
|
+
else?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
267
|
+
enum?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
|
|
268
|
+
examples?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
|
|
269
|
+
exclusiveMaximum?: number;
|
|
270
|
+
exclusiveMinimum?: number;
|
|
271
|
+
format?: string;
|
|
272
|
+
if?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
273
|
+
items?: JSONSchema;
|
|
274
|
+
maxContains?: number;
|
|
275
|
+
maximum?: number;
|
|
276
|
+
maxItems?: number;
|
|
277
|
+
maxLength?: number;
|
|
278
|
+
maxProperties?: number;
|
|
279
|
+
minContains?: number;
|
|
280
|
+
minimum?: number;
|
|
281
|
+
minItems?: number;
|
|
282
|
+
minLength?: number;
|
|
283
|
+
minProperties?: number;
|
|
284
|
+
multipleOf?: number;
|
|
285
|
+
not?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
286
|
+
oneOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
|
|
287
|
+
pattern?: string;
|
|
288
|
+
patternProperties?: Record<string, JSONSchema>;
|
|
289
|
+
prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
|
|
290
|
+
properties?: Record<string, JSONSchema>;
|
|
291
|
+
propertyNames?: JSONSchema;
|
|
292
|
+
readOnly?: boolean;
|
|
293
|
+
required?: string[] | readonly string[];
|
|
294
|
+
then?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
|
|
295
|
+
title?: string;
|
|
296
|
+
type?: JSONSchema.TypeValue | undefined;
|
|
297
|
+
unevaluatedItems?: JSONSchema;
|
|
298
|
+
unevaluatedProperties?: JSONSchema;
|
|
299
|
+
uniqueItems?: boolean;
|
|
300
|
+
writeOnly?: boolean;
|
|
301
|
+
}, zod_v4_core.$ZodType<unknown, unknown, zod_v4_core.$ZodTypeInternals<unknown, unknown>>>;
|
|
302
|
+
|
|
303
|
+
export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY, ZodToJsonSchemaConverter };
|
|
304
|
+
export type { ZodToJsonSchemaConverterOptions };
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { JsonSchemaXNativeType } from '@temporary-name/json-schema';
|
|
2
|
+
import { JSONSchemaFormat, JSONSchemaContentEncoding } from '@temporary-name/server/openapi';
|
|
3
|
+
import { intercept, toArray } from '@temporary-name/shared';
|
|
4
|
+
import { registry, globalRegistry } from 'zod/v4/core';
|
|
5
|
+
|
|
6
|
+
const JSON_SCHEMA_REGISTRY = registry();
|
|
7
|
+
const JSON_SCHEMA_INPUT_REGISTRY = registry();
|
|
8
|
+
const JSON_SCHEMA_OUTPUT_REGISTRY = registry();
|
|
9
|
+
|
|
10
|
+
class ZodToJsonSchemaConverter {
|
|
11
|
+
maxLazyDepth;
|
|
12
|
+
maxStructureDepth;
|
|
13
|
+
anyJsonSchema;
|
|
14
|
+
unsupportedJsonSchema;
|
|
15
|
+
undefinedJsonSchema;
|
|
16
|
+
interceptors;
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.maxLazyDepth = options.maxLazyDepth ?? 2;
|
|
19
|
+
this.maxStructureDepth = options.maxStructureDepth ?? 10;
|
|
20
|
+
this.anyJsonSchema = options.anyJsonSchema ?? {};
|
|
21
|
+
this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
|
|
22
|
+
this.undefinedJsonSchema = options.undefinedJsonSchema ?? { not: {} };
|
|
23
|
+
this.interceptors = options.interceptors ?? [];
|
|
24
|
+
}
|
|
25
|
+
condition(schema) {
|
|
26
|
+
return schema !== void 0 && schema["~standard"].vendor === "zod" && "_zod" in schema;
|
|
27
|
+
}
|
|
28
|
+
convert(schema, options) {
|
|
29
|
+
return this.#convert(schema, options, 0, 0);
|
|
30
|
+
}
|
|
31
|
+
#convert(schema, options, lazyDepth, structureDepth, isHandledCustomJSONSchema = false) {
|
|
32
|
+
return intercept(
|
|
33
|
+
this.interceptors,
|
|
34
|
+
{ schema, options, lazyDepth, isHandledCustomJSONSchema },
|
|
35
|
+
({ schema: schema2, options: options2, lazyDepth: lazyDepth2, isHandledCustomJSONSchema: isHandledCustomJSONSchema2 }) => {
|
|
36
|
+
if (structureDepth > this.maxStructureDepth) {
|
|
37
|
+
return [false, this.anyJsonSchema];
|
|
38
|
+
}
|
|
39
|
+
if (!options2.minStructureDepthForRef || options2.minStructureDepthForRef <= structureDepth) {
|
|
40
|
+
const components = toArray(options2.components);
|
|
41
|
+
for (const component of components) {
|
|
42
|
+
if (component.schema === schema2 && component.allowedStrategies.includes(options2.strategy)) {
|
|
43
|
+
return [component.required, { $ref: component.ref }];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!isHandledCustomJSONSchema2) {
|
|
48
|
+
const customJSONSchema = this.#getCustomJsonSchema(schema2, options2);
|
|
49
|
+
if (customJSONSchema) {
|
|
50
|
+
const [required, json] = this.#convert(schema2, options2, lazyDepth2, structureDepth, true);
|
|
51
|
+
return [required, { ...json, ...customJSONSchema }];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
switch (schema2._zod.def.type) {
|
|
55
|
+
case "string": {
|
|
56
|
+
const string = schema2;
|
|
57
|
+
const json = { type: "string" };
|
|
58
|
+
const { minimum, maximum, format, patterns, contentEncoding } = string._zod.bag;
|
|
59
|
+
if (typeof minimum === "number") {
|
|
60
|
+
json.minLength = minimum;
|
|
61
|
+
}
|
|
62
|
+
if (typeof maximum === "number") {
|
|
63
|
+
json.maxLength = maximum;
|
|
64
|
+
}
|
|
65
|
+
if (typeof contentEncoding === "string") {
|
|
66
|
+
json.contentEncoding = this.#handleContentEncoding(contentEncoding);
|
|
67
|
+
}
|
|
68
|
+
if (typeof format === "string" && format !== "regex" && json.contentEncoding === void 0) {
|
|
69
|
+
json.format = this.#handleStringFormat(format);
|
|
70
|
+
}
|
|
71
|
+
if (patterns instanceof Set && json.contentEncoding === void 0 && json.format === void 0) {
|
|
72
|
+
for (const pattern of patterns) {
|
|
73
|
+
if (json.pattern === void 0) {
|
|
74
|
+
json.pattern = pattern.source;
|
|
75
|
+
} else {
|
|
76
|
+
json.allOf ??= [];
|
|
77
|
+
json.allOf.push({ pattern: pattern.source });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (format === "jwt" && json.contentEncoding === void 0 && json.format === void 0 && json.pattern === void 0) {
|
|
82
|
+
json.pattern = /^[\w-]+\.[\w-]+\.[\w-]+$/.source;
|
|
83
|
+
}
|
|
84
|
+
return [true, json];
|
|
85
|
+
}
|
|
86
|
+
case "number": {
|
|
87
|
+
const number = schema2;
|
|
88
|
+
const json = { type: "number" };
|
|
89
|
+
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = number._zod.bag;
|
|
90
|
+
if (typeof format === "string" && format?.includes("int")) {
|
|
91
|
+
json.type = "integer";
|
|
92
|
+
}
|
|
93
|
+
if (typeof minimum === "number") {
|
|
94
|
+
json.minimum = minimum;
|
|
95
|
+
}
|
|
96
|
+
if (typeof maximum === "number") {
|
|
97
|
+
json.maximum = maximum;
|
|
98
|
+
}
|
|
99
|
+
if (typeof exclusiveMinimum === "number") {
|
|
100
|
+
json.exclusiveMinimum = exclusiveMinimum;
|
|
101
|
+
}
|
|
102
|
+
if (typeof exclusiveMaximum === "number") {
|
|
103
|
+
json.exclusiveMaximum = exclusiveMaximum;
|
|
104
|
+
}
|
|
105
|
+
if (typeof multipleOf === "number") {
|
|
106
|
+
json.multipleOf = multipleOf;
|
|
107
|
+
}
|
|
108
|
+
return [true, json];
|
|
109
|
+
}
|
|
110
|
+
case "boolean": {
|
|
111
|
+
return [true, { type: "boolean" }];
|
|
112
|
+
}
|
|
113
|
+
case "bigint": {
|
|
114
|
+
return [
|
|
115
|
+
true,
|
|
116
|
+
{
|
|
117
|
+
type: "string",
|
|
118
|
+
pattern: "^-?[0-9]+$",
|
|
119
|
+
"x-native-type": JsonSchemaXNativeType.BigInt
|
|
120
|
+
}
|
|
121
|
+
];
|
|
122
|
+
}
|
|
123
|
+
case "date": {
|
|
124
|
+
return [
|
|
125
|
+
true,
|
|
126
|
+
{
|
|
127
|
+
type: "string",
|
|
128
|
+
format: JSONSchemaFormat.DateTime,
|
|
129
|
+
"x-native-type": JsonSchemaXNativeType.Date
|
|
130
|
+
}
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
case "null": {
|
|
134
|
+
return [true, { type: "null" }];
|
|
135
|
+
}
|
|
136
|
+
case "undefined":
|
|
137
|
+
case "void": {
|
|
138
|
+
return [false, this.undefinedJsonSchema];
|
|
139
|
+
}
|
|
140
|
+
case "any": {
|
|
141
|
+
return [false, this.anyJsonSchema];
|
|
142
|
+
}
|
|
143
|
+
case "unknown": {
|
|
144
|
+
return [false, this.anyJsonSchema];
|
|
145
|
+
}
|
|
146
|
+
case "never": {
|
|
147
|
+
return [true, this.unsupportedJsonSchema];
|
|
148
|
+
}
|
|
149
|
+
case "array": {
|
|
150
|
+
const array = schema2;
|
|
151
|
+
const json = { type: "array" };
|
|
152
|
+
const { minimum, maximum } = array._zod.bag;
|
|
153
|
+
if (typeof minimum === "number") {
|
|
154
|
+
json.minItems = minimum;
|
|
155
|
+
}
|
|
156
|
+
if (typeof maximum === "number") {
|
|
157
|
+
json.maxItems = maximum;
|
|
158
|
+
}
|
|
159
|
+
json.items = this.#handleArrayItemJsonSchema(
|
|
160
|
+
this.#convert(array._zod.def.element, options2, lazyDepth2, structureDepth + 1),
|
|
161
|
+
options2
|
|
162
|
+
);
|
|
163
|
+
return [true, json];
|
|
164
|
+
}
|
|
165
|
+
case "object": {
|
|
166
|
+
const object = schema2;
|
|
167
|
+
const json = { type: "object" };
|
|
168
|
+
for (const [key, value] of Object.entries(object._zod.def.shape)) {
|
|
169
|
+
const [itemRequired, itemJson] = this.#convert(value, options2, lazyDepth2, structureDepth + 1);
|
|
170
|
+
json.properties ??= {};
|
|
171
|
+
json.properties[key] = itemJson;
|
|
172
|
+
if (itemRequired) {
|
|
173
|
+
json.required ??= [];
|
|
174
|
+
json.required.push(key);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (object._zod.def.catchall) {
|
|
178
|
+
if (object._zod.def.catchall._zod.def.type === "never") {
|
|
179
|
+
json.additionalProperties = false;
|
|
180
|
+
} else {
|
|
181
|
+
const [_, addJson] = this.#convert(
|
|
182
|
+
object._zod.def.catchall,
|
|
183
|
+
options2,
|
|
184
|
+
lazyDepth2,
|
|
185
|
+
structureDepth + 1
|
|
186
|
+
);
|
|
187
|
+
json.additionalProperties = addJson;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return [true, json];
|
|
191
|
+
}
|
|
192
|
+
case "union": {
|
|
193
|
+
const union = schema2;
|
|
194
|
+
const anyOf = [];
|
|
195
|
+
let required = true;
|
|
196
|
+
for (const item of union._zod.def.options) {
|
|
197
|
+
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2, structureDepth);
|
|
198
|
+
if (!itemRequired) {
|
|
199
|
+
required = false;
|
|
200
|
+
}
|
|
201
|
+
if (options2.strategy === "input") {
|
|
202
|
+
if (itemJson !== this.undefinedJsonSchema && itemJson !== this.unsupportedJsonSchema) {
|
|
203
|
+
anyOf.push(itemJson);
|
|
204
|
+
}
|
|
205
|
+
} else {
|
|
206
|
+
if (itemJson !== this.undefinedJsonSchema) {
|
|
207
|
+
anyOf.push(itemJson);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return [required, anyOf.length === 1 ? anyOf[0] : { anyOf }];
|
|
212
|
+
}
|
|
213
|
+
case "intersection": {
|
|
214
|
+
const intersection = schema2;
|
|
215
|
+
const json = { allOf: [] };
|
|
216
|
+
let required = false;
|
|
217
|
+
for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
|
|
218
|
+
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2, structureDepth);
|
|
219
|
+
json.allOf.push(itemJson);
|
|
220
|
+
if (itemRequired) {
|
|
221
|
+
required = true;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return [required, json];
|
|
225
|
+
}
|
|
226
|
+
case "tuple": {
|
|
227
|
+
const tuple = schema2;
|
|
228
|
+
const json = { type: "array", prefixItems: [] };
|
|
229
|
+
for (const item of tuple._zod.def.items) {
|
|
230
|
+
json.prefixItems.push(
|
|
231
|
+
this.#handleArrayItemJsonSchema(
|
|
232
|
+
this.#convert(item, options2, lazyDepth2, structureDepth + 1),
|
|
233
|
+
options2
|
|
234
|
+
)
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
if (tuple._zod.def.rest) {
|
|
238
|
+
json.items = this.#handleArrayItemJsonSchema(
|
|
239
|
+
this.#convert(tuple._zod.def.rest, options2, lazyDepth2, structureDepth + 1),
|
|
240
|
+
options2
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const { minimum, maximum } = tuple._zod.bag;
|
|
244
|
+
if (typeof minimum === "number") {
|
|
245
|
+
json.minItems = minimum;
|
|
246
|
+
}
|
|
247
|
+
if (typeof maximum === "number") {
|
|
248
|
+
json.maxItems = maximum;
|
|
249
|
+
}
|
|
250
|
+
return [true, json];
|
|
251
|
+
}
|
|
252
|
+
case "record": {
|
|
253
|
+
const record = schema2;
|
|
254
|
+
const json = { type: "object" };
|
|
255
|
+
json.propertyNames = this.#convert(
|
|
256
|
+
record._zod.def.keyType,
|
|
257
|
+
options2,
|
|
258
|
+
lazyDepth2,
|
|
259
|
+
structureDepth + 1
|
|
260
|
+
)[1];
|
|
261
|
+
json.additionalProperties = this.#convert(
|
|
262
|
+
record._zod.def.valueType,
|
|
263
|
+
options2,
|
|
264
|
+
lazyDepth2,
|
|
265
|
+
structureDepth + 1
|
|
266
|
+
)[1];
|
|
267
|
+
return [true, json];
|
|
268
|
+
}
|
|
269
|
+
case "map": {
|
|
270
|
+
const map = schema2;
|
|
271
|
+
return [
|
|
272
|
+
true,
|
|
273
|
+
{
|
|
274
|
+
type: "array",
|
|
275
|
+
items: {
|
|
276
|
+
type: "array",
|
|
277
|
+
prefixItems: [
|
|
278
|
+
this.#handleArrayItemJsonSchema(
|
|
279
|
+
this.#convert(map._zod.def.keyType, options2, lazyDepth2, structureDepth + 1),
|
|
280
|
+
options2
|
|
281
|
+
),
|
|
282
|
+
this.#handleArrayItemJsonSchema(
|
|
283
|
+
this.#convert(map._zod.def.valueType, options2, lazyDepth2, structureDepth + 1),
|
|
284
|
+
options2
|
|
285
|
+
)
|
|
286
|
+
],
|
|
287
|
+
maxItems: 2,
|
|
288
|
+
minItems: 2
|
|
289
|
+
},
|
|
290
|
+
"x-native-type": JsonSchemaXNativeType.Map
|
|
291
|
+
}
|
|
292
|
+
];
|
|
293
|
+
}
|
|
294
|
+
case "set": {
|
|
295
|
+
const set = schema2;
|
|
296
|
+
return [
|
|
297
|
+
true,
|
|
298
|
+
{
|
|
299
|
+
type: "array",
|
|
300
|
+
uniqueItems: true,
|
|
301
|
+
items: this.#handleArrayItemJsonSchema(
|
|
302
|
+
this.#convert(set._zod.def.valueType, options2, lazyDepth2, structureDepth + 1),
|
|
303
|
+
options2
|
|
304
|
+
),
|
|
305
|
+
"x-native-type": JsonSchemaXNativeType.Set
|
|
306
|
+
}
|
|
307
|
+
];
|
|
308
|
+
}
|
|
309
|
+
case "enum": {
|
|
310
|
+
const enum_ = schema2;
|
|
311
|
+
return [true, { enum: Object.values(enum_._zod.def.entries) }];
|
|
312
|
+
}
|
|
313
|
+
case "literal": {
|
|
314
|
+
const literal = schema2;
|
|
315
|
+
let required = true;
|
|
316
|
+
const values = /* @__PURE__ */ new Set();
|
|
317
|
+
for (const value of literal._zod.def.values) {
|
|
318
|
+
if (value === void 0) {
|
|
319
|
+
required = false;
|
|
320
|
+
} else {
|
|
321
|
+
values.add(typeof value === "bigint" ? value.toString() : value);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const json = values.size === 0 ? this.undefinedJsonSchema : values.size === 1 ? { const: values.values().next().value } : { enum: Array.from(values) };
|
|
325
|
+
return [required, json];
|
|
326
|
+
}
|
|
327
|
+
case "file": {
|
|
328
|
+
const file = schema2;
|
|
329
|
+
const oneOf = [];
|
|
330
|
+
const { mime } = file._zod.bag;
|
|
331
|
+
if (mime === void 0 || Array.isArray(mime) && mime.every((m) => typeof m === "string")) {
|
|
332
|
+
for (const type of mime ?? ["*/*"]) {
|
|
333
|
+
oneOf.push({
|
|
334
|
+
type: "string",
|
|
335
|
+
contentMediaType: type
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return [true, oneOf.length === 1 ? oneOf[0] : { anyOf: oneOf }];
|
|
340
|
+
}
|
|
341
|
+
case "transform": {
|
|
342
|
+
return [false, this.anyJsonSchema];
|
|
343
|
+
}
|
|
344
|
+
case "nullable": {
|
|
345
|
+
const nullable = schema2;
|
|
346
|
+
const [required, json] = this.#convert(
|
|
347
|
+
nullable._zod.def.innerType,
|
|
348
|
+
options2,
|
|
349
|
+
lazyDepth2,
|
|
350
|
+
structureDepth
|
|
351
|
+
);
|
|
352
|
+
return [required, { anyOf: [json, { type: "null" }] }];
|
|
353
|
+
}
|
|
354
|
+
case "nonoptional": {
|
|
355
|
+
const nonoptional = schema2;
|
|
356
|
+
const [, json] = this.#convert(
|
|
357
|
+
nonoptional._zod.def.innerType,
|
|
358
|
+
options2,
|
|
359
|
+
lazyDepth2,
|
|
360
|
+
structureDepth
|
|
361
|
+
);
|
|
362
|
+
return [true, json];
|
|
363
|
+
}
|
|
364
|
+
case "success": {
|
|
365
|
+
return [true, { type: "boolean" }];
|
|
366
|
+
}
|
|
367
|
+
case "default":
|
|
368
|
+
case "prefault": {
|
|
369
|
+
const default_ = schema2;
|
|
370
|
+
const [, json] = this.#convert(default_._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
|
371
|
+
return [
|
|
372
|
+
false,
|
|
373
|
+
{
|
|
374
|
+
...json,
|
|
375
|
+
default: default_._zod.def.defaultValue
|
|
376
|
+
}
|
|
377
|
+
];
|
|
378
|
+
}
|
|
379
|
+
case "catch": {
|
|
380
|
+
const catch_ = schema2;
|
|
381
|
+
return this.#convert(catch_._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
|
382
|
+
}
|
|
383
|
+
case "nan": {
|
|
384
|
+
return [true, options2.strategy === "input" ? this.unsupportedJsonSchema : { type: "null" }];
|
|
385
|
+
}
|
|
386
|
+
case "pipe": {
|
|
387
|
+
const pipe = schema2;
|
|
388
|
+
return this.#convert(
|
|
389
|
+
options2.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out,
|
|
390
|
+
options2,
|
|
391
|
+
lazyDepth2,
|
|
392
|
+
structureDepth
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
case "readonly": {
|
|
396
|
+
const readonly_ = schema2;
|
|
397
|
+
const [required, json] = this.#convert(
|
|
398
|
+
readonly_._zod.def.innerType,
|
|
399
|
+
options2,
|
|
400
|
+
lazyDepth2,
|
|
401
|
+
structureDepth
|
|
402
|
+
);
|
|
403
|
+
return [required, { ...json, readOnly: true }];
|
|
404
|
+
}
|
|
405
|
+
case "template_literal": {
|
|
406
|
+
const templateLiteral = schema2;
|
|
407
|
+
return [
|
|
408
|
+
true,
|
|
409
|
+
{
|
|
410
|
+
type: "string",
|
|
411
|
+
pattern: templateLiteral._zod.pattern.source
|
|
412
|
+
}
|
|
413
|
+
];
|
|
414
|
+
}
|
|
415
|
+
case "optional": {
|
|
416
|
+
const optional = schema2;
|
|
417
|
+
const [, json] = this.#convert(optional._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
|
418
|
+
return [false, json];
|
|
419
|
+
}
|
|
420
|
+
case "lazy": {
|
|
421
|
+
const lazy = schema2;
|
|
422
|
+
const currentLazyDepth = lazyDepth2 + 1;
|
|
423
|
+
if (currentLazyDepth > this.maxLazyDepth) {
|
|
424
|
+
return [false, this.anyJsonSchema];
|
|
425
|
+
}
|
|
426
|
+
return this.#convert(lazy._zod.def.getter(), options2, currentLazyDepth, structureDepth);
|
|
427
|
+
}
|
|
428
|
+
default: {
|
|
429
|
+
schema2._zod.def.type;
|
|
430
|
+
return [true, this.unsupportedJsonSchema];
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
#getCustomJsonSchema(schema, options) {
|
|
437
|
+
if (options.strategy === "input" && JSON_SCHEMA_INPUT_REGISTRY.has(schema)) {
|
|
438
|
+
return JSON_SCHEMA_INPUT_REGISTRY.get(schema);
|
|
439
|
+
}
|
|
440
|
+
if (options.strategy === "output" && JSON_SCHEMA_OUTPUT_REGISTRY.has(schema)) {
|
|
441
|
+
return JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
|
|
442
|
+
}
|
|
443
|
+
if (JSON_SCHEMA_REGISTRY.has(schema)) {
|
|
444
|
+
return JSON_SCHEMA_REGISTRY.get(schema);
|
|
445
|
+
}
|
|
446
|
+
const global = globalRegistry.get(schema);
|
|
447
|
+
if (global) {
|
|
448
|
+
return {
|
|
449
|
+
title: global.title,
|
|
450
|
+
description: global.description,
|
|
451
|
+
examples: Array.isArray(global.examples) ? global.examples : void 0
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
#handleArrayItemJsonSchema([required, schema], options) {
|
|
456
|
+
if (required || options.strategy === "input" || schema.default !== void 0) {
|
|
457
|
+
return schema;
|
|
458
|
+
}
|
|
459
|
+
if (schema === this.undefinedJsonSchema) {
|
|
460
|
+
return { type: "null" };
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
anyOf: [
|
|
464
|
+
// schema can contain { type: 'null' } so we should use anyOf instead of oneOf
|
|
465
|
+
schema,
|
|
466
|
+
{ type: "null" }
|
|
467
|
+
]
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
#handleStringFormat(format) {
|
|
471
|
+
if (format === "guid") {
|
|
472
|
+
return JSONSchemaFormat.UUID;
|
|
473
|
+
}
|
|
474
|
+
if (format === "url") {
|
|
475
|
+
return JSONSchemaFormat.URI;
|
|
476
|
+
}
|
|
477
|
+
if (format === "datetime") {
|
|
478
|
+
return JSONSchemaFormat.DateTime;
|
|
479
|
+
}
|
|
480
|
+
return Object.values(JSONSchemaFormat).includes(format) ? format : void 0;
|
|
481
|
+
}
|
|
482
|
+
#handleContentEncoding(contentEncoding) {
|
|
483
|
+
return Object.values(JSONSchemaContentEncoding).includes(contentEncoding) ? contentEncoding : void 0;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY, ZodToJsonSchemaConverter };
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temporary-name/json-schema",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.9.3-alpha.
|
|
4
|
+
"version": "1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://www.stainless.com/",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/
|
|
9
|
+
"url": "git+https://github.com/stainless-api/krusty.git",
|
|
10
10
|
"directory": "packages/json-schema"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
@@ -17,21 +17,24 @@
|
|
|
17
17
|
"types": "./dist/index.d.mts",
|
|
18
18
|
"import": "./dist/index.mjs",
|
|
19
19
|
"default": "./dist/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./zod4": {
|
|
22
|
+
"types": "./dist/zod4/index.d.mts",
|
|
23
|
+
"import": "./dist/zod4/index.mjs",
|
|
24
|
+
"default": "./dist/zod4/index.mjs"
|
|
20
25
|
}
|
|
21
26
|
},
|
|
22
27
|
"files": [
|
|
23
28
|
"dist"
|
|
24
29
|
],
|
|
25
30
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"@temporary-name/interop": "1.9.3-alpha.
|
|
28
|
-
"@temporary-name/
|
|
29
|
-
"@temporary-name/shared": "1.9.3-alpha.
|
|
30
|
-
"@temporary-name/
|
|
31
|
-
},
|
|
32
|
-
"devDependencies": {
|
|
33
|
-
"zod": "^4.1.11"
|
|
31
|
+
"zod": "^4.1.11",
|
|
32
|
+
"@temporary-name/interop": "1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e",
|
|
33
|
+
"@temporary-name/server": "1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e",
|
|
34
|
+
"@temporary-name/shared": "1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e",
|
|
35
|
+
"@temporary-name/zod": "1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e"
|
|
34
36
|
},
|
|
37
|
+
"devDependencies": {},
|
|
35
38
|
"scripts": {
|
|
36
39
|
"build": "unbuild",
|
|
37
40
|
"build:watch": "pnpm run build --watch",
|