@orpc/zod 1.1.1 → 1.2.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.
@@ -0,0 +1,302 @@
1
+ import { Context } from '@orpc/server';
2
+ import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
3
+ import { AnySchema } from '@orpc/contract';
4
+ import { JSONSchema, SchemaConvertOptions, ConditionalSchemaConverter } from '@orpc/openapi';
5
+ import { Interceptor, ThrowableError, Promisable } from '@orpc/shared';
6
+ import * as _zod_core from '@zod/core';
7
+ import { $ZodType, $input, $output } from '@zod/core';
8
+
9
+ declare class experimental_ZodSmartCoercionPlugin<TContext extends Context> implements StandardHandlerPlugin<TContext> {
10
+ #private;
11
+ init(options: StandardHandlerOptions<TContext>): void;
12
+ }
13
+
14
+ interface experimental_ZodToJsonSchemaOptions {
15
+ /**
16
+ * Max depth of lazy type, if it exceeds.
17
+ *
18
+ * Used anyJsonSchema (`{}`) when reach max depth
19
+ *
20
+ * @default 2
21
+ */
22
+ maxLazyDepth?: number;
23
+ /**
24
+ * The schema to be used to represent the any | unknown type.
25
+ *
26
+ * @default { }
27
+ */
28
+ anyJsonSchema?: Exclude<JSONSchema, boolean>;
29
+ /**
30
+ * The schema to be used when the Zod schema is unsupported.
31
+ *
32
+ * @default { not: {} }
33
+ */
34
+ unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
35
+ /**
36
+ * The schema to be used to represent the undefined type.
37
+ *
38
+ * @default { not: {} }
39
+ */
40
+ undefinedJsonSchema?: Exclude<JSONSchema, boolean>;
41
+ interceptors?: Interceptor<{
42
+ schema: $ZodType;
43
+ options: SchemaConvertOptions;
44
+ lazyDepth: number;
45
+ isHandledCustomJSONSchema: boolean;
46
+ }, [
47
+ required: boolean,
48
+ jsonSchema: Exclude<JSONSchema, boolean>
49
+ ], ThrowableError>[];
50
+ }
51
+ declare class experimental_ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
52
+ #private;
53
+ private readonly maxLazyDepth;
54
+ private readonly anyJsonSchema;
55
+ private readonly unsupportedJsonSchema;
56
+ private readonly undefinedJsonSchema;
57
+ private readonly interceptors;
58
+ constructor(options?: experimental_ZodToJsonSchemaOptions);
59
+ condition(schema: AnySchema | undefined): boolean;
60
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: Exclude<JSONSchema, boolean>]>;
61
+ }
62
+
63
+ /**
64
+ * Zod registry for customizing generated JSON schema, can use both for .input and .output
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * import { JSON_SCHEMA_REGISTRY } from '@orpc/zod/zod4'
69
+ *
70
+ * const user = z.object({
71
+ * name: z.string(),
72
+ * age: z.number(),
73
+ * })
74
+ *
75
+ * JSON_SCHEMA_REGISTRY.add(user, {
76
+ * examples: [{ name: 'John', age: 20 }],
77
+ * })
78
+ * ```
79
+ */
80
+ declare const experimental_JSON_SCHEMA_REGISTRY: _zod_core.$ZodRegistry<{
81
+ $anchor?: string;
82
+ $comment?: string;
83
+ $defs?: Record<string, JSONSchema>;
84
+ $dynamicAnchor?: string;
85
+ $dynamicRef?: string;
86
+ $id?: string;
87
+ $ref?: string;
88
+ $schema?: string;
89
+ $vocabulary?: Record<string, string>;
90
+ additionalItems?: JSONSchema;
91
+ additionalProperties?: JSONSchema;
92
+ allOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
93
+ anyOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
94
+ const?: typeof $input | typeof $output | undefined;
95
+ contains?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
96
+ contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
97
+ contentMediaType?: string;
98
+ contentSchema?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
99
+ default?: typeof $input | typeof $output | undefined;
100
+ definitions?: Record<string, JSONSchema>;
101
+ dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
102
+ dependentRequired?: Record<string, string[] | readonly string[]>;
103
+ dependentSchemas?: Record<string, JSONSchema>;
104
+ deprecated?: boolean;
105
+ description?: string;
106
+ else?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
107
+ enum?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
108
+ examples?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
109
+ exclusiveMaximum?: number;
110
+ exclusiveMinimum?: number;
111
+ format?: string;
112
+ if?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
113
+ items?: JSONSchema;
114
+ maxContains?: number;
115
+ maximum?: number;
116
+ maxItems?: number;
117
+ maxLength?: number;
118
+ maxProperties?: number;
119
+ minContains?: number;
120
+ minimum?: number;
121
+ minItems?: number;
122
+ minLength?: number;
123
+ minProperties?: number;
124
+ multipleOf?: number;
125
+ not?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
126
+ oneOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
127
+ pattern?: string;
128
+ patternProperties?: Record<string, JSONSchema>;
129
+ prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
130
+ properties?: Record<string, JSONSchema>;
131
+ propertyNames?: JSONSchema;
132
+ readOnly?: boolean;
133
+ required?: string[] | readonly string[];
134
+ then?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
135
+ title?: string;
136
+ type?: JSONSchema.TypeValue | undefined;
137
+ unevaluatedItems?: JSONSchema;
138
+ unevaluatedProperties?: JSONSchema;
139
+ uniqueItems?: boolean;
140
+ writeOnly?: boolean;
141
+ }, _zod_core.$ZodType<unknown, unknown>>;
142
+ /**
143
+ * Zod registry for customizing generated JSON schema, only useful for .input
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * import { JSON_SCHEMA_INPUT_REGISTRY } from '@orpc/zod/zod4'
148
+ *
149
+ * const user = z.object({
150
+ * name: z.string(),
151
+ * age: z.string().transform(v => Number(v)),
152
+ * })
153
+ *
154
+ * JSON_SCHEMA_REGISTRY.add(user, {
155
+ * examples: [{ name: 'John', age: "20" }],
156
+ * })
157
+ * ```
158
+ */
159
+ declare const experimental_JSON_SCHEMA_INPUT_REGISTRY: _zod_core.$ZodRegistry<{
160
+ $anchor?: string;
161
+ $comment?: string;
162
+ $defs?: Record<string, JSONSchema>;
163
+ $dynamicAnchor?: string;
164
+ $dynamicRef?: string;
165
+ $id?: string;
166
+ $ref?: string;
167
+ $schema?: string;
168
+ $vocabulary?: Record<string, string>;
169
+ additionalItems?: JSONSchema;
170
+ additionalProperties?: JSONSchema;
171
+ allOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
172
+ anyOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
173
+ const?: typeof $input | undefined;
174
+ contains?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
175
+ contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
176
+ contentMediaType?: string;
177
+ contentSchema?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
178
+ default?: typeof $input | undefined;
179
+ definitions?: Record<string, JSONSchema>;
180
+ dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
181
+ dependentRequired?: Record<string, string[] | readonly string[]>;
182
+ dependentSchemas?: Record<string, JSONSchema>;
183
+ deprecated?: boolean;
184
+ description?: string;
185
+ else?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
186
+ enum?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
187
+ examples?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
188
+ exclusiveMaximum?: number;
189
+ exclusiveMinimum?: number;
190
+ format?: string;
191
+ if?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
192
+ items?: JSONSchema;
193
+ maxContains?: number;
194
+ maximum?: number;
195
+ maxItems?: number;
196
+ maxLength?: number;
197
+ maxProperties?: number;
198
+ minContains?: number;
199
+ minimum?: number;
200
+ minItems?: number;
201
+ minLength?: number;
202
+ minProperties?: number;
203
+ multipleOf?: number;
204
+ not?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
205
+ oneOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
206
+ pattern?: string;
207
+ patternProperties?: Record<string, JSONSchema>;
208
+ prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
209
+ properties?: Record<string, JSONSchema>;
210
+ propertyNames?: JSONSchema;
211
+ readOnly?: boolean;
212
+ required?: string[] | readonly string[];
213
+ then?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
214
+ title?: string;
215
+ type?: JSONSchema.TypeValue | undefined;
216
+ unevaluatedItems?: JSONSchema;
217
+ unevaluatedProperties?: JSONSchema;
218
+ uniqueItems?: boolean;
219
+ writeOnly?: boolean;
220
+ }, _zod_core.$ZodType<unknown, unknown>>;
221
+ /**
222
+ * Zod registry for customizing generated JSON schema, only useful for .input
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * import { JSON_SCHEMA_OUTPUT_REGISTRY } from '@orpc/zod/zod4'
227
+ *
228
+ * const user = z.object({
229
+ * name: z.string(),
230
+ * age: z.string().transform(v => Number(v)),
231
+ * })
232
+ *
233
+ * JSON_SCHEMA_OUTPUT_REGISTRY.add(user, {
234
+ * examples: [{ name: 'John', age: 20 }],
235
+ * })
236
+ * ```
237
+ */
238
+ declare const experimental_JSON_SCHEMA_OUTPUT_REGISTRY: _zod_core.$ZodRegistry<{
239
+ $anchor?: string;
240
+ $comment?: string;
241
+ $defs?: Record<string, JSONSchema>;
242
+ $dynamicAnchor?: string;
243
+ $dynamicRef?: string;
244
+ $id?: string;
245
+ $ref?: string;
246
+ $schema?: string;
247
+ $vocabulary?: Record<string, string>;
248
+ additionalItems?: JSONSchema;
249
+ additionalProperties?: JSONSchema;
250
+ allOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
251
+ anyOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
252
+ const?: typeof $output | undefined;
253
+ contains?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
254
+ contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
255
+ contentMediaType?: string;
256
+ contentSchema?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
257
+ default?: typeof $output | undefined;
258
+ definitions?: Record<string, JSONSchema>;
259
+ dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
260
+ dependentRequired?: Record<string, string[] | readonly string[]>;
261
+ dependentSchemas?: Record<string, JSONSchema>;
262
+ deprecated?: boolean;
263
+ description?: string;
264
+ else?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
265
+ enum?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
266
+ examples?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
267
+ exclusiveMaximum?: number;
268
+ exclusiveMinimum?: number;
269
+ format?: string;
270
+ if?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
271
+ items?: JSONSchema;
272
+ maxContains?: number;
273
+ maximum?: number;
274
+ maxItems?: number;
275
+ maxLength?: number;
276
+ maxProperties?: number;
277
+ minContains?: number;
278
+ minimum?: number;
279
+ minItems?: number;
280
+ minLength?: number;
281
+ minProperties?: number;
282
+ multipleOf?: number;
283
+ not?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
284
+ oneOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
285
+ pattern?: string;
286
+ patternProperties?: Record<string, JSONSchema>;
287
+ prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
288
+ properties?: Record<string, JSONSchema>;
289
+ propertyNames?: JSONSchema;
290
+ readOnly?: boolean;
291
+ required?: string[] | readonly string[];
292
+ then?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
293
+ title?: string;
294
+ type?: JSONSchema.TypeValue | undefined;
295
+ unevaluatedItems?: JSONSchema;
296
+ unevaluatedProperties?: JSONSchema;
297
+ uniqueItems?: boolean;
298
+ writeOnly?: boolean;
299
+ }, _zod_core.$ZodType<unknown, unknown>>;
300
+
301
+ export { experimental_JSON_SCHEMA_INPUT_REGISTRY, experimental_JSON_SCHEMA_OUTPUT_REGISTRY, experimental_JSON_SCHEMA_REGISTRY, experimental_ZodSmartCoercionPlugin, experimental_ZodToJsonSchemaConverter };
302
+ export type { experimental_ZodToJsonSchemaOptions };
@@ -0,0 +1,302 @@
1
+ import { Context } from '@orpc/server';
2
+ import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
3
+ import { AnySchema } from '@orpc/contract';
4
+ import { JSONSchema, SchemaConvertOptions, ConditionalSchemaConverter } from '@orpc/openapi';
5
+ import { Interceptor, ThrowableError, Promisable } from '@orpc/shared';
6
+ import * as _zod_core from '@zod/core';
7
+ import { $ZodType, $input, $output } from '@zod/core';
8
+
9
+ declare class experimental_ZodSmartCoercionPlugin<TContext extends Context> implements StandardHandlerPlugin<TContext> {
10
+ #private;
11
+ init(options: StandardHandlerOptions<TContext>): void;
12
+ }
13
+
14
+ interface experimental_ZodToJsonSchemaOptions {
15
+ /**
16
+ * Max depth of lazy type, if it exceeds.
17
+ *
18
+ * Used anyJsonSchema (`{}`) when reach max depth
19
+ *
20
+ * @default 2
21
+ */
22
+ maxLazyDepth?: number;
23
+ /**
24
+ * The schema to be used to represent the any | unknown type.
25
+ *
26
+ * @default { }
27
+ */
28
+ anyJsonSchema?: Exclude<JSONSchema, boolean>;
29
+ /**
30
+ * The schema to be used when the Zod schema is unsupported.
31
+ *
32
+ * @default { not: {} }
33
+ */
34
+ unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
35
+ /**
36
+ * The schema to be used to represent the undefined type.
37
+ *
38
+ * @default { not: {} }
39
+ */
40
+ undefinedJsonSchema?: Exclude<JSONSchema, boolean>;
41
+ interceptors?: Interceptor<{
42
+ schema: $ZodType;
43
+ options: SchemaConvertOptions;
44
+ lazyDepth: number;
45
+ isHandledCustomJSONSchema: boolean;
46
+ }, [
47
+ required: boolean,
48
+ jsonSchema: Exclude<JSONSchema, boolean>
49
+ ], ThrowableError>[];
50
+ }
51
+ declare class experimental_ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
52
+ #private;
53
+ private readonly maxLazyDepth;
54
+ private readonly anyJsonSchema;
55
+ private readonly unsupportedJsonSchema;
56
+ private readonly undefinedJsonSchema;
57
+ private readonly interceptors;
58
+ constructor(options?: experimental_ZodToJsonSchemaOptions);
59
+ condition(schema: AnySchema | undefined): boolean;
60
+ convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: Exclude<JSONSchema, boolean>]>;
61
+ }
62
+
63
+ /**
64
+ * Zod registry for customizing generated JSON schema, can use both for .input and .output
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * import { JSON_SCHEMA_REGISTRY } from '@orpc/zod/zod4'
69
+ *
70
+ * const user = z.object({
71
+ * name: z.string(),
72
+ * age: z.number(),
73
+ * })
74
+ *
75
+ * JSON_SCHEMA_REGISTRY.add(user, {
76
+ * examples: [{ name: 'John', age: 20 }],
77
+ * })
78
+ * ```
79
+ */
80
+ declare const experimental_JSON_SCHEMA_REGISTRY: _zod_core.$ZodRegistry<{
81
+ $anchor?: string;
82
+ $comment?: string;
83
+ $defs?: Record<string, JSONSchema>;
84
+ $dynamicAnchor?: string;
85
+ $dynamicRef?: string;
86
+ $id?: string;
87
+ $ref?: string;
88
+ $schema?: string;
89
+ $vocabulary?: Record<string, string>;
90
+ additionalItems?: JSONSchema;
91
+ additionalProperties?: JSONSchema;
92
+ allOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
93
+ anyOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
94
+ const?: typeof $input | typeof $output | undefined;
95
+ contains?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
96
+ contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
97
+ contentMediaType?: string;
98
+ contentSchema?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
99
+ default?: typeof $input | typeof $output | undefined;
100
+ definitions?: Record<string, JSONSchema>;
101
+ dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
102
+ dependentRequired?: Record<string, string[] | readonly string[]>;
103
+ dependentSchemas?: Record<string, JSONSchema>;
104
+ deprecated?: boolean;
105
+ description?: string;
106
+ else?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
107
+ enum?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
108
+ examples?: ((typeof $input | typeof $output)[] | readonly (typeof $input | typeof $output)[]) | undefined;
109
+ exclusiveMaximum?: number;
110
+ exclusiveMinimum?: number;
111
+ format?: string;
112
+ if?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
113
+ items?: JSONSchema;
114
+ maxContains?: number;
115
+ maximum?: number;
116
+ maxItems?: number;
117
+ maxLength?: number;
118
+ maxProperties?: number;
119
+ minContains?: number;
120
+ minimum?: number;
121
+ minItems?: number;
122
+ minLength?: number;
123
+ minProperties?: number;
124
+ multipleOf?: number;
125
+ not?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
126
+ oneOf?: (JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue>[]) | undefined;
127
+ pattern?: string;
128
+ patternProperties?: Record<string, JSONSchema>;
129
+ prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
130
+ properties?: Record<string, JSONSchema>;
131
+ propertyNames?: JSONSchema;
132
+ readOnly?: boolean;
133
+ required?: string[] | readonly string[];
134
+ then?: JSONSchema<typeof $input | typeof $output, JSONSchema.TypeValue> | undefined;
135
+ title?: string;
136
+ type?: JSONSchema.TypeValue | undefined;
137
+ unevaluatedItems?: JSONSchema;
138
+ unevaluatedProperties?: JSONSchema;
139
+ uniqueItems?: boolean;
140
+ writeOnly?: boolean;
141
+ }, _zod_core.$ZodType<unknown, unknown>>;
142
+ /**
143
+ * Zod registry for customizing generated JSON schema, only useful for .input
144
+ *
145
+ * @example
146
+ * ```ts
147
+ * import { JSON_SCHEMA_INPUT_REGISTRY } from '@orpc/zod/zod4'
148
+ *
149
+ * const user = z.object({
150
+ * name: z.string(),
151
+ * age: z.string().transform(v => Number(v)),
152
+ * })
153
+ *
154
+ * JSON_SCHEMA_REGISTRY.add(user, {
155
+ * examples: [{ name: 'John', age: "20" }],
156
+ * })
157
+ * ```
158
+ */
159
+ declare const experimental_JSON_SCHEMA_INPUT_REGISTRY: _zod_core.$ZodRegistry<{
160
+ $anchor?: string;
161
+ $comment?: string;
162
+ $defs?: Record<string, JSONSchema>;
163
+ $dynamicAnchor?: string;
164
+ $dynamicRef?: string;
165
+ $id?: string;
166
+ $ref?: string;
167
+ $schema?: string;
168
+ $vocabulary?: Record<string, string>;
169
+ additionalItems?: JSONSchema;
170
+ additionalProperties?: JSONSchema;
171
+ allOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
172
+ anyOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
173
+ const?: typeof $input | undefined;
174
+ contains?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
175
+ contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
176
+ contentMediaType?: string;
177
+ contentSchema?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
178
+ default?: typeof $input | undefined;
179
+ definitions?: Record<string, JSONSchema>;
180
+ dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
181
+ dependentRequired?: Record<string, string[] | readonly string[]>;
182
+ dependentSchemas?: Record<string, JSONSchema>;
183
+ deprecated?: boolean;
184
+ description?: string;
185
+ else?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
186
+ enum?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
187
+ examples?: ((typeof $input)[] | readonly (typeof $input)[]) | undefined;
188
+ exclusiveMaximum?: number;
189
+ exclusiveMinimum?: number;
190
+ format?: string;
191
+ if?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
192
+ items?: JSONSchema;
193
+ maxContains?: number;
194
+ maximum?: number;
195
+ maxItems?: number;
196
+ maxLength?: number;
197
+ maxProperties?: number;
198
+ minContains?: number;
199
+ minimum?: number;
200
+ minItems?: number;
201
+ minLength?: number;
202
+ minProperties?: number;
203
+ multipleOf?: number;
204
+ not?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
205
+ oneOf?: (JSONSchema<typeof $input, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $input, JSONSchema.TypeValue>[]) | undefined;
206
+ pattern?: string;
207
+ patternProperties?: Record<string, JSONSchema>;
208
+ prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
209
+ properties?: Record<string, JSONSchema>;
210
+ propertyNames?: JSONSchema;
211
+ readOnly?: boolean;
212
+ required?: string[] | readonly string[];
213
+ then?: JSONSchema<typeof $input, JSONSchema.TypeValue> | undefined;
214
+ title?: string;
215
+ type?: JSONSchema.TypeValue | undefined;
216
+ unevaluatedItems?: JSONSchema;
217
+ unevaluatedProperties?: JSONSchema;
218
+ uniqueItems?: boolean;
219
+ writeOnly?: boolean;
220
+ }, _zod_core.$ZodType<unknown, unknown>>;
221
+ /**
222
+ * Zod registry for customizing generated JSON schema, only useful for .input
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * import { JSON_SCHEMA_OUTPUT_REGISTRY } from '@orpc/zod/zod4'
227
+ *
228
+ * const user = z.object({
229
+ * name: z.string(),
230
+ * age: z.string().transform(v => Number(v)),
231
+ * })
232
+ *
233
+ * JSON_SCHEMA_OUTPUT_REGISTRY.add(user, {
234
+ * examples: [{ name: 'John', age: 20 }],
235
+ * })
236
+ * ```
237
+ */
238
+ declare const experimental_JSON_SCHEMA_OUTPUT_REGISTRY: _zod_core.$ZodRegistry<{
239
+ $anchor?: string;
240
+ $comment?: string;
241
+ $defs?: Record<string, JSONSchema>;
242
+ $dynamicAnchor?: string;
243
+ $dynamicRef?: string;
244
+ $id?: string;
245
+ $ref?: string;
246
+ $schema?: string;
247
+ $vocabulary?: Record<string, string>;
248
+ additionalItems?: JSONSchema;
249
+ additionalProperties?: JSONSchema;
250
+ allOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
251
+ anyOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
252
+ const?: typeof $output | undefined;
253
+ contains?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
254
+ contentEncoding?: "7bit" | "8bit" | "base64" | "binary" | "ietf-token" | "quoted-printable" | "x-token";
255
+ contentMediaType?: string;
256
+ contentSchema?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
257
+ default?: typeof $output | undefined;
258
+ definitions?: Record<string, JSONSchema>;
259
+ dependencies?: Record<string, (string[] | readonly string[]) | JSONSchema>;
260
+ dependentRequired?: Record<string, string[] | readonly string[]>;
261
+ dependentSchemas?: Record<string, JSONSchema>;
262
+ deprecated?: boolean;
263
+ description?: string;
264
+ else?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
265
+ enum?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
266
+ examples?: ((typeof $output)[] | readonly (typeof $output)[]) | undefined;
267
+ exclusiveMaximum?: number;
268
+ exclusiveMinimum?: number;
269
+ format?: string;
270
+ if?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
271
+ items?: JSONSchema;
272
+ maxContains?: number;
273
+ maximum?: number;
274
+ maxItems?: number;
275
+ maxLength?: number;
276
+ maxProperties?: number;
277
+ minContains?: number;
278
+ minimum?: number;
279
+ minItems?: number;
280
+ minLength?: number;
281
+ minProperties?: number;
282
+ multipleOf?: number;
283
+ not?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
284
+ oneOf?: (JSONSchema<typeof $output, JSONSchema.TypeValue>[] | readonly JSONSchema<typeof $output, JSONSchema.TypeValue>[]) | undefined;
285
+ pattern?: string;
286
+ patternProperties?: Record<string, JSONSchema>;
287
+ prefixItems?: (JSONSchema<any, JSONSchema.TypeValue>[] | readonly JSONSchema<any, JSONSchema.TypeValue>[]) | JSONSchema;
288
+ properties?: Record<string, JSONSchema>;
289
+ propertyNames?: JSONSchema;
290
+ readOnly?: boolean;
291
+ required?: string[] | readonly string[];
292
+ then?: JSONSchema<typeof $output, JSONSchema.TypeValue> | undefined;
293
+ title?: string;
294
+ type?: JSONSchema.TypeValue | undefined;
295
+ unevaluatedItems?: JSONSchema;
296
+ unevaluatedProperties?: JSONSchema;
297
+ uniqueItems?: boolean;
298
+ writeOnly?: boolean;
299
+ }, _zod_core.$ZodType<unknown, unknown>>;
300
+
301
+ export { experimental_JSON_SCHEMA_INPUT_REGISTRY, experimental_JSON_SCHEMA_OUTPUT_REGISTRY, experimental_JSON_SCHEMA_REGISTRY, experimental_ZodSmartCoercionPlugin, experimental_ZodToJsonSchemaConverter };
302
+ export type { experimental_ZodToJsonSchemaOptions };
@@ -0,0 +1,653 @@
1
+ import { isObject, guard, intercept } from '@orpc/shared';
2
+ import { JSONSchemaFormat, JSONSchemaContentEncoding } from '@orpc/openapi';
3
+ import { registry, globalRegistry } from '@zod/core';
4
+
5
+ class experimental_ZodSmartCoercionPlugin {
6
+ init(options) {
7
+ options.clientInterceptors ??= [];
8
+ options.clientInterceptors.unshift((options2) => {
9
+ const inputSchema = options2.procedure["~orpc"].inputSchema;
10
+ if (!inputSchema || inputSchema["~standard"].vendor !== "zod") {
11
+ return options2.next();
12
+ }
13
+ const coercedInput = this.#coerce(inputSchema, options2.input);
14
+ return options2.next({ ...options2, input: coercedInput });
15
+ });
16
+ }
17
+ #coerce(schema, value) {
18
+ switch (schema._zod.def.type) {
19
+ case "number": {
20
+ if (typeof value === "string") {
21
+ return this.#stringToNumber(value);
22
+ }
23
+ return value;
24
+ }
25
+ case "bigint": {
26
+ if (typeof value === "string") {
27
+ return this.#stringToBigInt(value);
28
+ }
29
+ return value;
30
+ }
31
+ case "boolean":
32
+ case "success": {
33
+ if (typeof value === "string") {
34
+ return this.#stringToBoolean(value);
35
+ }
36
+ return value;
37
+ }
38
+ case "date": {
39
+ if (typeof value === "string") {
40
+ return this.#stringToDate(value);
41
+ }
42
+ return value;
43
+ }
44
+ case "literal":
45
+ case "enum": {
46
+ const literal = schema;
47
+ if (!literal._zod.values.has(value) && typeof value === "string") {
48
+ const num = this.#stringToNumber(value);
49
+ if (literal._zod.values.has(num)) {
50
+ return num;
51
+ }
52
+ const bool = this.#stringToBoolean(value);
53
+ if (literal._zod.values.has(bool)) {
54
+ return bool;
55
+ }
56
+ const bigint = this.#stringToBigInt(value);
57
+ if (literal._zod.values.has(bigint)) {
58
+ return bigint;
59
+ }
60
+ }
61
+ return value;
62
+ }
63
+ case "array": {
64
+ const array = schema;
65
+ if (value === void 0) {
66
+ return [];
67
+ }
68
+ if (Array.isArray(value)) {
69
+ return value.map((v) => this.#coerce(array._zod.def.element, v));
70
+ }
71
+ return value;
72
+ }
73
+ case "tuple": {
74
+ const tuple = schema;
75
+ if (value === void 0) {
76
+ return [];
77
+ }
78
+ if (Array.isArray(value)) {
79
+ return value.map((v, i) => {
80
+ const s = tuple._zod.def.items[i] ?? tuple._zod.def.rest;
81
+ return s ? this.#coerce(s, v) : v;
82
+ });
83
+ }
84
+ return value;
85
+ }
86
+ case "set": {
87
+ const set = schema;
88
+ if (value === void 0) {
89
+ return /* @__PURE__ */ new Set();
90
+ }
91
+ if (Array.isArray(value)) {
92
+ return new Set(
93
+ value.map((v) => this.#coerce(set._zod.def.valueType, v))
94
+ );
95
+ }
96
+ if (value instanceof Set) {
97
+ return new Set(
98
+ Array.from(value).map((v) => this.#coerce(set._zod.def.valueType, v))
99
+ );
100
+ }
101
+ return value;
102
+ }
103
+ case "object":
104
+ case "interface": {
105
+ const object = schema;
106
+ if (value === void 0) {
107
+ return {};
108
+ }
109
+ if (isObject(value)) {
110
+ const newObj = {};
111
+ const keys = /* @__PURE__ */ new Set([
112
+ ...Object.keys(value),
113
+ ...Object.keys(object._zod.def.shape)
114
+ ]);
115
+ for (const k of keys) {
116
+ const s = object._zod.def.shape[k] ?? object._zod.def.catchall;
117
+ newObj[k] = s ? this.#coerce(s, value[k]) : value[k];
118
+ }
119
+ return newObj;
120
+ }
121
+ return value;
122
+ }
123
+ case "record": {
124
+ const record = schema;
125
+ if (value === void 0) {
126
+ return {};
127
+ }
128
+ if (isObject(value)) {
129
+ const newObj = {};
130
+ for (const [k, v] of Object.entries(value)) {
131
+ const key = this.#coerce(record._zod.def.keyType, k);
132
+ const val = this.#coerce(record._zod.def.valueType, v);
133
+ newObj[key] = val;
134
+ }
135
+ return newObj;
136
+ }
137
+ return value;
138
+ }
139
+ case "map": {
140
+ const map = schema;
141
+ if (value === void 0) {
142
+ return /* @__PURE__ */ new Map();
143
+ }
144
+ if (Array.isArray(value) && value.every((i) => Array.isArray(i) && i.length <= 2)) {
145
+ return new Map(
146
+ value.map(([k, v]) => [
147
+ this.#coerce(map._zod.def.keyType, k),
148
+ this.#coerce(map._zod.def.valueType, v)
149
+ ])
150
+ );
151
+ }
152
+ if (value instanceof Map) {
153
+ return new Map(
154
+ Array.from(value).map(([k, v]) => [
155
+ this.#coerce(map._zod.def.keyType, k),
156
+ this.#coerce(map._zod.def.valueType, v)
157
+ ])
158
+ );
159
+ }
160
+ return value;
161
+ }
162
+ case "union": {
163
+ const union = schema;
164
+ if (union._zod.def.options.length === 1) {
165
+ return this.#coerce(union._zod.def.options[0], value);
166
+ }
167
+ if (isObject(value)) {
168
+ for (const option of union._zod.def.options) {
169
+ if (option._zod.disc && this.#matchDiscriminators(value, option._zod.disc)) {
170
+ return this.#coerce(option, value);
171
+ }
172
+ }
173
+ }
174
+ return value;
175
+ }
176
+ case "intersection": {
177
+ const intersection = schema;
178
+ return this.#coerce(
179
+ intersection._zod.def.right,
180
+ this.#coerce(intersection._zod.def.left, value)
181
+ );
182
+ }
183
+ case "optional": {
184
+ const optional = schema;
185
+ if (value === void 0) {
186
+ return void 0;
187
+ }
188
+ return this.#coerce(optional._zod.def.innerType, value);
189
+ }
190
+ case "nonoptional": {
191
+ const nonoptional = schema;
192
+ return this.#coerce(nonoptional._zod.def.innerType, value);
193
+ }
194
+ case "nullable": {
195
+ const nullable = schema;
196
+ if (value === null) {
197
+ return null;
198
+ }
199
+ return this.#coerce(nullable._zod.def.innerType, value);
200
+ }
201
+ case "readonly": {
202
+ const readonly_ = schema;
203
+ return this.#coerce(readonly_._zod.def.innerType, value);
204
+ }
205
+ case "pipe": {
206
+ const pipe = schema;
207
+ return this.#coerce(pipe._zod.def.in, value);
208
+ }
209
+ case "default":
210
+ case "catch": {
211
+ const default_ = schema;
212
+ return this.#coerce(default_._zod.def.innerType, value);
213
+ }
214
+ case "lazy": {
215
+ const lazy = schema;
216
+ if (value !== void 0) {
217
+ return this.#coerce(lazy._zod.def.getter(), value);
218
+ }
219
+ return value;
220
+ }
221
+ default: {
222
+ schema._zod.def.type;
223
+ return value;
224
+ }
225
+ }
226
+ }
227
+ #stringToNumber(value) {
228
+ const num = Number(value);
229
+ return Number.isNaN(num) || num.toString() !== value ? value : num;
230
+ }
231
+ #stringToBigInt(value) {
232
+ return guard(() => BigInt(value)) ?? value;
233
+ }
234
+ #stringToBoolean(value) {
235
+ const lower = value.toLowerCase();
236
+ if (lower === "false" || lower === "off" || lower === "f") {
237
+ return false;
238
+ }
239
+ if (lower === "true" || lower === "on" || lower === "t") {
240
+ return true;
241
+ }
242
+ return value;
243
+ }
244
+ #stringToDate(value) {
245
+ const date = new Date(value);
246
+ if (!Number.isNaN(date.getTime()) && date.toISOString().startsWith(value)) {
247
+ return date;
248
+ }
249
+ return value;
250
+ }
251
+ /**
252
+ * This function is inspired from Zod, because it's not exported
253
+ * https://github.com/colinhacks/zod/blob/v4/packages/core/src/schemas.ts#L1903C1-L1921C2
254
+ */
255
+ #matchDiscriminators(input, discs) {
256
+ for (const [key, value] of discs) {
257
+ const data = input[key];
258
+ if (value.values.size && !value.values.has(data)) {
259
+ return false;
260
+ }
261
+ if (value.maps.length === 0) {
262
+ continue;
263
+ }
264
+ if (!isObject(data)) {
265
+ return false;
266
+ }
267
+ for (const m of value.maps) {
268
+ if (!this.#matchDiscriminators(data, m)) {
269
+ return false;
270
+ }
271
+ }
272
+ }
273
+ return true;
274
+ }
275
+ }
276
+
277
+ const experimental_JSON_SCHEMA_REGISTRY = registry();
278
+ const experimental_JSON_SCHEMA_INPUT_REGISTRY = registry();
279
+ const experimental_JSON_SCHEMA_OUTPUT_REGISTRY = registry();
280
+
281
+ class experimental_ZodToJsonSchemaConverter {
282
+ maxLazyDepth;
283
+ anyJsonSchema;
284
+ unsupportedJsonSchema;
285
+ undefinedJsonSchema;
286
+ interceptors;
287
+ constructor(options = {}) {
288
+ this.maxLazyDepth = options.maxLazyDepth ?? 2;
289
+ this.anyJsonSchema = options.anyJsonSchema ?? {};
290
+ this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
291
+ this.undefinedJsonSchema = options.undefinedJsonSchema ?? { not: {} };
292
+ this.interceptors = options.interceptors ?? [];
293
+ }
294
+ condition(schema) {
295
+ return schema !== void 0 && schema["~standard"].vendor === "zod";
296
+ }
297
+ convert(schema, options) {
298
+ return this.#convert(schema, options, 0);
299
+ }
300
+ #convert(schema, options, lazyDepth, isHandledCustomJSONSchema = false) {
301
+ return intercept(
302
+ this.interceptors,
303
+ { schema, options, lazyDepth, isHandledCustomJSONSchema },
304
+ async ({ schema: schema2, options: options2, lazyDepth: lazyDepth2, isHandledCustomJSONSchema: isHandledCustomJSONSchema2 }) => {
305
+ if (!isHandledCustomJSONSchema2) {
306
+ const customJSONSchema = this.#getCustomJsonSchema(schema2, options2);
307
+ if (customJSONSchema) {
308
+ const [required, json] = await this.#convert(schema2, options2, lazyDepth2, true);
309
+ return [required, { ...json, ...customJSONSchema }];
310
+ }
311
+ }
312
+ switch (schema2._zod.def.type) {
313
+ case "string": {
314
+ const string = schema2;
315
+ const json = { type: "string" };
316
+ const { minimum, maximum, format, pattern, contentEncoding } = string._zod.computed;
317
+ if (minimum !== void 0) {
318
+ json.minLength = minimum;
319
+ }
320
+ if (maximum !== void 0) {
321
+ json.maxLength = maximum;
322
+ }
323
+ if (contentEncoding !== void 0) {
324
+ json.contentEncoding = this.#handleContentEncoding(contentEncoding);
325
+ }
326
+ if (format !== void 0 && format !== "regex" && json.contentEncoding === void 0) {
327
+ json.format = this.#handleStringFormat(format);
328
+ }
329
+ if (pattern !== void 0 && json.contentEncoding === void 0 && json.format === void 0) {
330
+ json.pattern = pattern.source;
331
+ }
332
+ if (format === "jwt" && json.contentEncoding === void 0 && json.format === void 0 && json.pattern === void 0) {
333
+ json.pattern = /^[\w-]+\.[\w-]+\.[\w-]+$/.source;
334
+ }
335
+ return [true, json];
336
+ }
337
+ case "number": {
338
+ const number = schema2;
339
+ const json = { type: "number" };
340
+ const { minimum, maximum, format, multipleOf, inclusive } = number._zod.computed;
341
+ if (format?.includes("int")) {
342
+ json.type = "integer";
343
+ }
344
+ if (minimum !== void 0) {
345
+ if (inclusive) {
346
+ json.minimum = minimum;
347
+ } else {
348
+ json.exclusiveMinimum = minimum;
349
+ }
350
+ }
351
+ if (maximum !== void 0) {
352
+ if (inclusive) {
353
+ json.maximum = maximum;
354
+ } else {
355
+ json.exclusiveMaximum = maximum;
356
+ }
357
+ }
358
+ if (multipleOf !== void 0) {
359
+ json.multipleOf = multipleOf;
360
+ }
361
+ return [true, json];
362
+ }
363
+ case "boolean": {
364
+ return [true, { type: "boolean" }];
365
+ }
366
+ case "bigint": {
367
+ return [true, { type: "string", pattern: "^-?[0-9]+$" }];
368
+ }
369
+ case "date": {
370
+ return [true, { type: "string", format: JSONSchemaFormat.DateTime }];
371
+ }
372
+ case "null": {
373
+ return [true, { type: "null" }];
374
+ }
375
+ case "undefined":
376
+ case "void": {
377
+ return [false, this.undefinedJsonSchema];
378
+ }
379
+ case "any": {
380
+ return [false, this.anyJsonSchema];
381
+ }
382
+ case "unknown": {
383
+ return [false, this.anyJsonSchema];
384
+ }
385
+ case "never": {
386
+ return [true, this.unsupportedJsonSchema];
387
+ }
388
+ case "array": {
389
+ const array = schema2;
390
+ const json = { type: "array" };
391
+ const { minimum, maximum } = array._zod.computed;
392
+ if (minimum !== void 0) {
393
+ json.minItems = minimum;
394
+ }
395
+ if (maximum !== void 0) {
396
+ json.maxItems = maximum;
397
+ }
398
+ json.items = this.#handleArrayItemJsonSchema(await this.#convert(array._zod.def.element, options2, lazyDepth2), options2);
399
+ return [true, json];
400
+ }
401
+ case "object": {
402
+ const object = schema2;
403
+ const json = { type: "object" };
404
+ for (const [key, value] of Object.entries(object._zod.def.shape)) {
405
+ const [itemRequired, itemJson] = await this.#convert(value, options2, lazyDepth2);
406
+ json.properties ??= {};
407
+ json.properties[key] = itemJson;
408
+ if (itemRequired) {
409
+ json.required ??= [];
410
+ json.required.push(key);
411
+ }
412
+ }
413
+ if (object._zod.def.catchall) {
414
+ if (object._zod.def.catchall._zod.def.type === "never") {
415
+ json.additionalProperties = false;
416
+ } else {
417
+ const [_, addJson] = await this.#convert(object._zod.def.catchall, options2, lazyDepth2);
418
+ json.additionalProperties = addJson;
419
+ }
420
+ }
421
+ return [true, json];
422
+ }
423
+ case "union": {
424
+ const union = schema2;
425
+ const anyOf = [];
426
+ let required = true;
427
+ for (const item of union._zod.def.options) {
428
+ const [itemRequired, itemJson] = await this.#convert(item, options2, lazyDepth2);
429
+ if (!itemRequired) {
430
+ required = false;
431
+ }
432
+ if (options2.strategy === "input") {
433
+ if (itemJson !== this.undefinedJsonSchema && itemJson !== this.unsupportedJsonSchema) {
434
+ anyOf.push(itemJson);
435
+ }
436
+ } else {
437
+ if (itemJson !== this.undefinedJsonSchema) {
438
+ anyOf.push(itemJson);
439
+ }
440
+ }
441
+ }
442
+ return [required, anyOf.length === 1 ? anyOf[0] : { anyOf }];
443
+ }
444
+ case "intersection": {
445
+ const intersection = schema2;
446
+ const json = { allOf: [] };
447
+ let required = false;
448
+ for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
449
+ const [itemRequired, itemJson] = await this.#convert(item, options2, lazyDepth2);
450
+ json.allOf.push(itemJson);
451
+ if (itemRequired) {
452
+ required = true;
453
+ }
454
+ }
455
+ return [required, json];
456
+ }
457
+ case "tuple": {
458
+ const tuple = schema2;
459
+ const json = { type: "array", prefixItems: [] };
460
+ for (const item of tuple._zod.def.items) {
461
+ json.prefixItems.push(this.#handleArrayItemJsonSchema(await this.#convert(item, options2, lazyDepth2), options2));
462
+ }
463
+ if (tuple._zod.def.rest) {
464
+ json.items = this.#handleArrayItemJsonSchema(await this.#convert(tuple._zod.def.rest, options2, lazyDepth2), options2);
465
+ }
466
+ const { minimum, maximum } = tuple._zod.computed;
467
+ if (minimum !== void 0) {
468
+ json.minItems = minimum;
469
+ }
470
+ if (maximum !== void 0) {
471
+ json.maxItems = maximum;
472
+ }
473
+ return [true, json];
474
+ }
475
+ case "record": {
476
+ const record = schema2;
477
+ const json = { type: "object" };
478
+ json.propertyNames = (await this.#convert(record._zod.def.keyType, options2, lazyDepth2))[1];
479
+ json.additionalProperties = (await this.#convert(record._zod.def.valueType, options2, lazyDepth2))[1];
480
+ return [true, json];
481
+ }
482
+ case "map": {
483
+ const map = schema2;
484
+ return [true, {
485
+ type: "array",
486
+ items: {
487
+ type: "array",
488
+ prefixItems: [
489
+ this.#handleArrayItemJsonSchema(await this.#convert(map._zod.def.keyType, options2, lazyDepth2), options2),
490
+ this.#handleArrayItemJsonSchema(await this.#convert(map._zod.def.valueType, options2, lazyDepth2), options2)
491
+ ],
492
+ maxItems: 2,
493
+ minItems: 2
494
+ }
495
+ }];
496
+ }
497
+ case "set": {
498
+ const set = schema2;
499
+ return [true, {
500
+ type: "array",
501
+ uniqueItems: true,
502
+ items: this.#handleArrayItemJsonSchema(await this.#convert(set._zod.def.valueType, options2, lazyDepth2), options2)
503
+ }];
504
+ }
505
+ case "enum": {
506
+ const enum_ = schema2;
507
+ return [true, { enum: Object.values(enum_._zod.def.entries) }];
508
+ }
509
+ case "literal": {
510
+ const literal = schema2;
511
+ let required = true;
512
+ const values = /* @__PURE__ */ new Set();
513
+ for (const value of literal._zod.def.values) {
514
+ if (value === void 0) {
515
+ required = false;
516
+ } else {
517
+ values.add(typeof value === "bigint" ? value.toString() : value);
518
+ }
519
+ }
520
+ const json = values.size === 0 ? this.undefinedJsonSchema : values.size === 1 ? { const: values.values().next().value } : { enum: Array.from(values) };
521
+ return [required, json];
522
+ }
523
+ case "file": {
524
+ const file = schema2;
525
+ const oneOf = [];
526
+ const { mime } = file._zod.computed;
527
+ for (const type of mime ?? ["*/*"]) {
528
+ oneOf.push({
529
+ type: "string",
530
+ contentMediaType: type
531
+ });
532
+ }
533
+ return [true, oneOf.length === 1 ? oneOf[0] : { anyOf: oneOf }];
534
+ }
535
+ case "transform": {
536
+ return [false, this.anyJsonSchema];
537
+ }
538
+ case "nullable": {
539
+ const nullable = schema2;
540
+ const [required, json] = await this.#convert(nullable._zod.def.innerType, options2, lazyDepth2);
541
+ return [required, { anyOf: [json, { type: "null" }] }];
542
+ }
543
+ case "nonoptional": {
544
+ const nonoptional = schema2;
545
+ const [, json] = await this.#convert(nonoptional._zod.def.innerType, options2, lazyDepth2);
546
+ return [true, json];
547
+ }
548
+ case "success": {
549
+ return [true, { type: "boolean" }];
550
+ }
551
+ case "default": {
552
+ const default_ = schema2;
553
+ const [, json] = await this.#convert(default_._zod.def.innerType, options2, lazyDepth2);
554
+ return [false, {
555
+ ...json,
556
+ default: default_._zod.def.defaultValue()
557
+ }];
558
+ }
559
+ case "catch": {
560
+ const catch_ = schema2;
561
+ const [, json] = await this.#convert(catch_._zod.def.innerType, options2, lazyDepth2);
562
+ return [false, json];
563
+ }
564
+ case "nan": {
565
+ return [true, options2.strategy === "input" ? this.unsupportedJsonSchema : { type: "null" }];
566
+ }
567
+ case "pipe": {
568
+ const pipe = schema2;
569
+ return await this.#convert(options2.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out, options2, lazyDepth2);
570
+ }
571
+ case "readonly": {
572
+ const readonly_ = schema2;
573
+ const [required, json] = await this.#convert(readonly_._zod.def.innerType, options2, lazyDepth2);
574
+ return [required, { ...json, readOnly: true }];
575
+ }
576
+ case "template_literal": {
577
+ const templateLiteral = schema2;
578
+ return [true, {
579
+ type: "string",
580
+ pattern: templateLiteral._zod.pattern.source
581
+ }];
582
+ }
583
+ case "optional": {
584
+ const optional = schema2;
585
+ const [, json] = await this.#convert(optional._zod.def.innerType, options2, lazyDepth2);
586
+ return [false, json];
587
+ }
588
+ case "lazy": {
589
+ const lazy = schema2;
590
+ if (lazyDepth2 >= this.maxLazyDepth) {
591
+ return [false, this.anyJsonSchema];
592
+ }
593
+ return await this.#convert(lazy._zod.def.getter(), options2, lazyDepth2 + 1);
594
+ }
595
+ default: {
596
+ schema2._zod.def.type;
597
+ return [true, this.unsupportedJsonSchema];
598
+ }
599
+ }
600
+ }
601
+ );
602
+ }
603
+ #getCustomJsonSchema(schema, options) {
604
+ if (options.strategy === "input" && experimental_JSON_SCHEMA_INPUT_REGISTRY.has(schema)) {
605
+ return experimental_JSON_SCHEMA_INPUT_REGISTRY.get(schema);
606
+ }
607
+ if (options.strategy === "output" && experimental_JSON_SCHEMA_OUTPUT_REGISTRY.has(schema)) {
608
+ return experimental_JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
609
+ }
610
+ if (experimental_JSON_SCHEMA_REGISTRY.has(schema)) {
611
+ return experimental_JSON_SCHEMA_REGISTRY.get(schema);
612
+ }
613
+ const global = globalRegistry.get(schema);
614
+ if (global) {
615
+ return {
616
+ description: global.description,
617
+ examples: global.examples
618
+ };
619
+ }
620
+ }
621
+ #handleArrayItemJsonSchema([required, schema], options) {
622
+ if (required || options.strategy === "input") {
623
+ return schema;
624
+ }
625
+ if (schema === this.undefinedJsonSchema) {
626
+ return { type: "null" };
627
+ }
628
+ return {
629
+ anyOf: [
630
+ // schema can contain { type: 'null' } so we should use anyOf instead of oneOf
631
+ schema,
632
+ { type: "null" }
633
+ ]
634
+ };
635
+ }
636
+ #handleStringFormat(format) {
637
+ if (format === "guid") {
638
+ return JSONSchemaFormat.UUID;
639
+ }
640
+ if (format === "url") {
641
+ return JSONSchemaFormat.URI;
642
+ }
643
+ if (format === "datetime") {
644
+ return JSONSchemaFormat.DateTime;
645
+ }
646
+ return Object.values(JSONSchemaFormat).includes(format) ? format : void 0;
647
+ }
648
+ #handleContentEncoding(contentEncoding) {
649
+ return Object.values(JSONSchemaContentEncoding).includes(contentEncoding) ? contentEncoding : void 0;
650
+ }
651
+ }
652
+
653
+ export { experimental_JSON_SCHEMA_INPUT_REGISTRY, experimental_JSON_SCHEMA_OUTPUT_REGISTRY, experimental_JSON_SCHEMA_REGISTRY, experimental_ZodSmartCoercionPlugin, experimental_ZodToJsonSchemaConverter };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/zod",
3
3
  "type": "module",
4
- "version": "1.1.1",
4
+ "version": "1.2.0",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -18,24 +18,42 @@
18
18
  "types": "./dist/index.d.mts",
19
19
  "import": "./dist/index.mjs",
20
20
  "default": "./dist/index.mjs"
21
+ },
22
+ "./zod4": {
23
+ "types": "./dist/zod4/index.d.mts",
24
+ "import": "./dist/zod4/index.mjs",
25
+ "default": "./dist/zod4/index.mjs"
21
26
  }
22
27
  },
23
28
  "files": [
24
29
  "dist"
25
30
  ],
26
31
  "peerDependencies": {
27
- "zod": "^3.24.2",
28
- "@orpc/contract": "1.1.1",
29
- "@orpc/server": "1.1.1"
32
+ "@zod/core": ">=0.11.4",
33
+ "zod": ">=3.24.2",
34
+ "@orpc/contract": "1.2.0",
35
+ "@orpc/server": "1.2.0"
36
+ },
37
+ "peerDependenciesMeta": {
38
+ "@zod/core": {
39
+ "optional": true
40
+ },
41
+ "zod": {
42
+ "optional": true
43
+ }
30
44
  },
31
45
  "dependencies": {
32
46
  "escape-string-regexp": "^5.0.0",
33
47
  "wildcard-match": "^5.1.3",
34
- "@orpc/shared": "1.1.1",
35
- "@orpc/openapi": "1.1.1"
48
+ "@orpc/openapi": "1.2.0",
49
+ "@orpc/shared": "1.2.0"
36
50
  },
37
51
  "devDependencies": {
38
- "zod-to-json-schema": "^3.24.5"
52
+ "@zod/core": "^0.11.4",
53
+ "@zod/mini": "^4.0.0-beta.20250505T012514",
54
+ "zod": "^3.24.2",
55
+ "zod-to-json-schema": "^3.24.5",
56
+ "zod4": "npm:zod@^4.0.0-beta.20250505T012514"
39
57
  },
40
58
  "scripts": {
41
59
  "build": "unbuild",