@orpc/zod 0.0.0-next.f81b4a2 → 0.0.0-next.fa8d145

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,28 +21,24 @@
21
21
 
22
22
  <h3 align="center">Typesafe APIs Made Simple 🪄</h3>
23
23
 
24
- **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards, ensuring a smooth and enjoyable developer experience.
24
+ **oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
25
25
 
26
26
  ---
27
27
 
28
28
  ## Highlights
29
29
 
30
- - **End-to-End Type Safety 🔒**: Ensure complete type safety from inputs to outputs and errors, bridging server and client seamlessly.
31
- - **First-Class OpenAPI 📄**: Adheres to the OpenAPI standard out of the box, ensuring seamless integration and comprehensive API documentation.
32
- - **Contract-First Development 📜**: (Optional) Define your API contract upfront and implement it with confidence.
33
- - **Exceptional Developer Experience ✨**: Enjoy a streamlined workflow with robust typing and clear, in-code documentation.
34
- - **Multi-Runtime Support 🌍**: Run your code seamlessly on Cloudflare, Deno, Bun, Node.js, and more.
35
- - **Framework Integrations 🧩**: Supports Tanstack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
36
- - **Server Actions ⚡️**: Fully compatible with React Server Actions on Next.js, TanStack Start, and more.
37
- - **Standard Schema Support 🗂️**: Effortlessly work with Zod, Valibot, ArkType, and others right out of the box.
38
- - **Fast & Lightweight 💨**: Built on native APIs across all runtimes – optimized for speed and efficiency.
39
- - **Native Types 📦**: Enjoy built-in support for Date, File, Blob, BigInt, URL and more with no extra setup.
40
- - **Lazy Router ⏱️**: Improve cold start times with our lazy routing feature.
41
- - **SSE & Streaming 📡**: Provides SSE and streaming features – perfect for real-time notifications and AI-powered streaming responses.
42
- - **Reusability 🔄**: Write once and reuse your code across multiple purposes effortlessly.
43
- - **Extendability 🔌**: Easily enhance oRPC with plugins, middleware, and interceptors.
44
- - **Reliability 🛡️**: Well-tested, fully TypeScript, production-ready, and MIT licensed for peace of mind.
45
- - **Simplicity 💡**: Enjoy straightforward, clean code with no hidden magic.
30
+ - **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
31
+ - **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
32
+ - **📝 Contract-First Development**: Optionally define your API contract before implementation.
33
+ - **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte), Pinia Colada, and more.
34
+ - **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
35
+ - **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
36
+ - **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
37
+ - **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
38
+ - **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
39
+ - **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
40
+ - **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
41
+ - **🛡️ Reliability**: Well-tested, TypeScript-based, production-ready, and MIT licensed.
46
42
 
47
43
  ## Documentation
48
44
 
@@ -53,6 +49,7 @@ You can find the full documentation [here](https://orpc.unnoq.com).
53
49
  - [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
54
50
  - [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
55
51
  - [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
52
+ - [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with NestJS.
56
53
  - [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
57
54
  - [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
58
55
  - [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
@@ -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_v4_core from 'zod/v4/core';
7
+ import { $ZodType, $input, $output } from 'zod/v4/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_v4_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_v4_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_v4_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_v4_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_v4_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_v4_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_v4_core from 'zod/v4/core';
7
+ import { $ZodType, $input, $output } from 'zod/v4/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_v4_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_v4_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_v4_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_v4_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_v4_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_v4_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,660 @@
1
+ import { isObject, guard, intercept } from '@orpc/shared';
2
+ import { JSONSchemaFormat, JSONSchemaContentEncoding } from '@orpc/openapi';
3
+ import { registry, globalRegistry } from 'zod/v4/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 "prefault": {
211
+ const default_ = schema;
212
+ if (value === void 0) {
213
+ return value;
214
+ }
215
+ return this.#coerce(default_._zod.def.innerType, value);
216
+ }
217
+ case "catch": {
218
+ const catch_ = schema;
219
+ return this.#coerce(catch_._zod.def.innerType, value);
220
+ }
221
+ case "lazy": {
222
+ const lazy = schema;
223
+ if (value !== void 0) {
224
+ return this.#coerce(lazy._zod.def.getter(), value);
225
+ }
226
+ return value;
227
+ }
228
+ default: {
229
+ schema._zod.def.type;
230
+ return value;
231
+ }
232
+ }
233
+ }
234
+ #stringToNumber(value) {
235
+ const num = Number(value);
236
+ return Number.isNaN(num) || num.toString() !== value ? value : num;
237
+ }
238
+ #stringToBigInt(value) {
239
+ return guard(() => BigInt(value)) ?? value;
240
+ }
241
+ #stringToBoolean(value) {
242
+ const lower = value.toLowerCase();
243
+ if (lower === "false" || lower === "off" || lower === "f") {
244
+ return false;
245
+ }
246
+ if (lower === "true" || lower === "on" || lower === "t") {
247
+ return true;
248
+ }
249
+ return value;
250
+ }
251
+ #stringToDate(value) {
252
+ const date = new Date(value);
253
+ if (!Number.isNaN(date.getTime()) && date.toISOString().startsWith(value)) {
254
+ return date;
255
+ }
256
+ return value;
257
+ }
258
+ /**
259
+ * This function is inspired from Zod, because it's not exported
260
+ * https://github.com/colinhacks/zod/blob/v4/packages/core/src/schemas.ts#L1903C1-L1921C2
261
+ */
262
+ #matchDiscriminators(input, discs) {
263
+ for (const [key, value] of discs) {
264
+ const data = input[key];
265
+ if (value.values.size && !value.values.has(data)) {
266
+ return false;
267
+ }
268
+ if (value.maps.length === 0) {
269
+ continue;
270
+ }
271
+ if (!isObject(data)) {
272
+ return false;
273
+ }
274
+ for (const m of value.maps) {
275
+ if (!this.#matchDiscriminators(data, m)) {
276
+ return false;
277
+ }
278
+ }
279
+ }
280
+ return true;
281
+ }
282
+ }
283
+
284
+ const experimental_JSON_SCHEMA_REGISTRY = registry();
285
+ const experimental_JSON_SCHEMA_INPUT_REGISTRY = registry();
286
+ const experimental_JSON_SCHEMA_OUTPUT_REGISTRY = registry();
287
+
288
+ class experimental_ZodToJsonSchemaConverter {
289
+ maxLazyDepth;
290
+ anyJsonSchema;
291
+ unsupportedJsonSchema;
292
+ undefinedJsonSchema;
293
+ interceptors;
294
+ constructor(options = {}) {
295
+ this.maxLazyDepth = options.maxLazyDepth ?? 2;
296
+ this.anyJsonSchema = options.anyJsonSchema ?? {};
297
+ this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
298
+ this.undefinedJsonSchema = options.undefinedJsonSchema ?? { not: {} };
299
+ this.interceptors = options.interceptors ?? [];
300
+ }
301
+ condition(schema) {
302
+ return schema !== void 0 && schema["~standard"].vendor === "zod";
303
+ }
304
+ convert(schema, options) {
305
+ return this.#convert(schema, options, 0);
306
+ }
307
+ #convert(schema, options, lazyDepth, isHandledCustomJSONSchema = false) {
308
+ return intercept(
309
+ this.interceptors,
310
+ { schema, options, lazyDepth, isHandledCustomJSONSchema },
311
+ async ({ schema: schema2, options: options2, lazyDepth: lazyDepth2, isHandledCustomJSONSchema: isHandledCustomJSONSchema2 }) => {
312
+ if (!isHandledCustomJSONSchema2) {
313
+ const customJSONSchema = this.#getCustomJsonSchema(schema2, options2);
314
+ if (customJSONSchema) {
315
+ const [required, json] = await this.#convert(schema2, options2, lazyDepth2, true);
316
+ return [required, { ...json, ...customJSONSchema }];
317
+ }
318
+ }
319
+ switch (schema2._zod.def.type) {
320
+ case "string": {
321
+ const string = schema2;
322
+ const json = { type: "string" };
323
+ const { minimum, maximum, format, pattern, contentEncoding } = string._zod.computed;
324
+ if (minimum !== void 0) {
325
+ json.minLength = minimum;
326
+ }
327
+ if (maximum !== void 0) {
328
+ json.maxLength = maximum;
329
+ }
330
+ if (contentEncoding !== void 0) {
331
+ json.contentEncoding = this.#handleContentEncoding(contentEncoding);
332
+ }
333
+ if (format !== void 0 && format !== "regex" && json.contentEncoding === void 0) {
334
+ json.format = this.#handleStringFormat(format);
335
+ }
336
+ if (pattern !== void 0 && json.contentEncoding === void 0 && json.format === void 0) {
337
+ json.pattern = pattern.source;
338
+ }
339
+ if (format === "jwt" && json.contentEncoding === void 0 && json.format === void 0 && json.pattern === void 0) {
340
+ json.pattern = /^[\w-]+\.[\w-]+\.[\w-]+$/.source;
341
+ }
342
+ return [true, json];
343
+ }
344
+ case "number": {
345
+ const number = schema2;
346
+ const json = { type: "number" };
347
+ const { minimum, maximum, format, multipleOf, inclusive } = number._zod.computed;
348
+ if (format?.includes("int")) {
349
+ json.type = "integer";
350
+ }
351
+ if (minimum !== void 0) {
352
+ if (inclusive) {
353
+ json.minimum = minimum;
354
+ } else {
355
+ json.exclusiveMinimum = minimum;
356
+ }
357
+ }
358
+ if (maximum !== void 0) {
359
+ if (inclusive) {
360
+ json.maximum = maximum;
361
+ } else {
362
+ json.exclusiveMaximum = maximum;
363
+ }
364
+ }
365
+ if (multipleOf !== void 0) {
366
+ json.multipleOf = multipleOf;
367
+ }
368
+ return [true, json];
369
+ }
370
+ case "boolean": {
371
+ return [true, { type: "boolean" }];
372
+ }
373
+ case "bigint": {
374
+ return [true, { type: "string", pattern: "^-?[0-9]+$" }];
375
+ }
376
+ case "date": {
377
+ return [true, { type: "string", format: JSONSchemaFormat.DateTime }];
378
+ }
379
+ case "null": {
380
+ return [true, { type: "null" }];
381
+ }
382
+ case "undefined":
383
+ case "void": {
384
+ return [false, this.undefinedJsonSchema];
385
+ }
386
+ case "any": {
387
+ return [false, this.anyJsonSchema];
388
+ }
389
+ case "unknown": {
390
+ return [false, this.anyJsonSchema];
391
+ }
392
+ case "never": {
393
+ return [true, this.unsupportedJsonSchema];
394
+ }
395
+ case "array": {
396
+ const array = schema2;
397
+ const json = { type: "array" };
398
+ const { minimum, maximum } = array._zod.computed;
399
+ if (minimum !== void 0) {
400
+ json.minItems = minimum;
401
+ }
402
+ if (maximum !== void 0) {
403
+ json.maxItems = maximum;
404
+ }
405
+ json.items = this.#handleArrayItemJsonSchema(await this.#convert(array._zod.def.element, options2, lazyDepth2), options2);
406
+ return [true, json];
407
+ }
408
+ case "object": {
409
+ const object = schema2;
410
+ const json = { type: "object" };
411
+ for (const [key, value] of Object.entries(object._zod.def.shape)) {
412
+ const [itemRequired, itemJson] = await this.#convert(value, options2, lazyDepth2);
413
+ json.properties ??= {};
414
+ json.properties[key] = itemJson;
415
+ if (itemRequired) {
416
+ json.required ??= [];
417
+ json.required.push(key);
418
+ }
419
+ }
420
+ if (object._zod.def.catchall) {
421
+ if (object._zod.def.catchall._zod.def.type === "never") {
422
+ json.additionalProperties = false;
423
+ } else {
424
+ const [_, addJson] = await this.#convert(object._zod.def.catchall, options2, lazyDepth2);
425
+ json.additionalProperties = addJson;
426
+ }
427
+ }
428
+ return [true, json];
429
+ }
430
+ case "union": {
431
+ const union = schema2;
432
+ const anyOf = [];
433
+ let required = true;
434
+ for (const item of union._zod.def.options) {
435
+ const [itemRequired, itemJson] = await this.#convert(item, options2, lazyDepth2);
436
+ if (!itemRequired) {
437
+ required = false;
438
+ }
439
+ if (options2.strategy === "input") {
440
+ if (itemJson !== this.undefinedJsonSchema && itemJson !== this.unsupportedJsonSchema) {
441
+ anyOf.push(itemJson);
442
+ }
443
+ } else {
444
+ if (itemJson !== this.undefinedJsonSchema) {
445
+ anyOf.push(itemJson);
446
+ }
447
+ }
448
+ }
449
+ return [required, anyOf.length === 1 ? anyOf[0] : { anyOf }];
450
+ }
451
+ case "intersection": {
452
+ const intersection = schema2;
453
+ const json = { allOf: [] };
454
+ let required = false;
455
+ for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
456
+ const [itemRequired, itemJson] = await this.#convert(item, options2, lazyDepth2);
457
+ json.allOf.push(itemJson);
458
+ if (itemRequired) {
459
+ required = true;
460
+ }
461
+ }
462
+ return [required, json];
463
+ }
464
+ case "tuple": {
465
+ const tuple = schema2;
466
+ const json = { type: "array", prefixItems: [] };
467
+ for (const item of tuple._zod.def.items) {
468
+ json.prefixItems.push(this.#handleArrayItemJsonSchema(await this.#convert(item, options2, lazyDepth2), options2));
469
+ }
470
+ if (tuple._zod.def.rest) {
471
+ json.items = this.#handleArrayItemJsonSchema(await this.#convert(tuple._zod.def.rest, options2, lazyDepth2), options2);
472
+ }
473
+ const { minimum, maximum } = tuple._zod.computed;
474
+ if (minimum !== void 0) {
475
+ json.minItems = minimum;
476
+ }
477
+ if (maximum !== void 0) {
478
+ json.maxItems = maximum;
479
+ }
480
+ return [true, json];
481
+ }
482
+ case "record": {
483
+ const record = schema2;
484
+ const json = { type: "object" };
485
+ json.propertyNames = (await this.#convert(record._zod.def.keyType, options2, lazyDepth2))[1];
486
+ json.additionalProperties = (await this.#convert(record._zod.def.valueType, options2, lazyDepth2))[1];
487
+ return [true, json];
488
+ }
489
+ case "map": {
490
+ const map = schema2;
491
+ return [true, {
492
+ type: "array",
493
+ items: {
494
+ type: "array",
495
+ prefixItems: [
496
+ this.#handleArrayItemJsonSchema(await this.#convert(map._zod.def.keyType, options2, lazyDepth2), options2),
497
+ this.#handleArrayItemJsonSchema(await this.#convert(map._zod.def.valueType, options2, lazyDepth2), options2)
498
+ ],
499
+ maxItems: 2,
500
+ minItems: 2
501
+ }
502
+ }];
503
+ }
504
+ case "set": {
505
+ const set = schema2;
506
+ return [true, {
507
+ type: "array",
508
+ uniqueItems: true,
509
+ items: this.#handleArrayItemJsonSchema(await this.#convert(set._zod.def.valueType, options2, lazyDepth2), options2)
510
+ }];
511
+ }
512
+ case "enum": {
513
+ const enum_ = schema2;
514
+ return [true, { enum: Object.values(enum_._zod.def.entries) }];
515
+ }
516
+ case "literal": {
517
+ const literal = schema2;
518
+ let required = true;
519
+ const values = /* @__PURE__ */ new Set();
520
+ for (const value of literal._zod.def.values) {
521
+ if (value === void 0) {
522
+ required = false;
523
+ } else {
524
+ values.add(typeof value === "bigint" ? value.toString() : value);
525
+ }
526
+ }
527
+ const json = values.size === 0 ? this.undefinedJsonSchema : values.size === 1 ? { const: values.values().next().value } : { enum: Array.from(values) };
528
+ return [required, json];
529
+ }
530
+ case "file": {
531
+ const file = schema2;
532
+ const oneOf = [];
533
+ const { mime } = file._zod.computed;
534
+ for (const type of mime ?? ["*/*"]) {
535
+ oneOf.push({
536
+ type: "string",
537
+ contentMediaType: type
538
+ });
539
+ }
540
+ return [true, oneOf.length === 1 ? oneOf[0] : { anyOf: oneOf }];
541
+ }
542
+ case "transform": {
543
+ return [false, this.anyJsonSchema];
544
+ }
545
+ case "nullable": {
546
+ const nullable = schema2;
547
+ const [required, json] = await this.#convert(nullable._zod.def.innerType, options2, lazyDepth2);
548
+ return [required, { anyOf: [json, { type: "null" }] }];
549
+ }
550
+ case "nonoptional": {
551
+ const nonoptional = schema2;
552
+ const [, json] = await this.#convert(nonoptional._zod.def.innerType, options2, lazyDepth2);
553
+ return [true, json];
554
+ }
555
+ case "success": {
556
+ return [true, { type: "boolean" }];
557
+ }
558
+ case "default":
559
+ case "prefault": {
560
+ const default_ = schema2;
561
+ const [, json] = await this.#convert(default_._zod.def.innerType, options2, lazyDepth2);
562
+ return [false, {
563
+ ...json,
564
+ default: default_._zod.def.defaultValue
565
+ }];
566
+ }
567
+ case "catch": {
568
+ const catch_ = schema2;
569
+ return await this.#convert(catch_._zod.def.innerType, options2, lazyDepth2);
570
+ }
571
+ case "nan": {
572
+ return [true, options2.strategy === "input" ? this.unsupportedJsonSchema : { type: "null" }];
573
+ }
574
+ case "pipe": {
575
+ const pipe = schema2;
576
+ return await this.#convert(options2.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out, options2, lazyDepth2);
577
+ }
578
+ case "readonly": {
579
+ const readonly_ = schema2;
580
+ const [required, json] = await this.#convert(readonly_._zod.def.innerType, options2, lazyDepth2);
581
+ return [required, { ...json, readOnly: true }];
582
+ }
583
+ case "template_literal": {
584
+ const templateLiteral = schema2;
585
+ return [true, {
586
+ type: "string",
587
+ pattern: templateLiteral._zod.pattern.source
588
+ }];
589
+ }
590
+ case "optional": {
591
+ const optional = schema2;
592
+ const [, json] = await this.#convert(optional._zod.def.innerType, options2, lazyDepth2);
593
+ return [false, json];
594
+ }
595
+ case "lazy": {
596
+ const lazy = schema2;
597
+ if (lazyDepth2 >= this.maxLazyDepth) {
598
+ return [false, this.anyJsonSchema];
599
+ }
600
+ return await this.#convert(lazy._zod.def.getter(), options2, lazyDepth2 + 1);
601
+ }
602
+ default: {
603
+ schema2._zod.def.type;
604
+ return [true, this.unsupportedJsonSchema];
605
+ }
606
+ }
607
+ }
608
+ );
609
+ }
610
+ #getCustomJsonSchema(schema, options) {
611
+ if (options.strategy === "input" && experimental_JSON_SCHEMA_INPUT_REGISTRY.has(schema)) {
612
+ return experimental_JSON_SCHEMA_INPUT_REGISTRY.get(schema);
613
+ }
614
+ if (options.strategy === "output" && experimental_JSON_SCHEMA_OUTPUT_REGISTRY.has(schema)) {
615
+ return experimental_JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
616
+ }
617
+ if (experimental_JSON_SCHEMA_REGISTRY.has(schema)) {
618
+ return experimental_JSON_SCHEMA_REGISTRY.get(schema);
619
+ }
620
+ const global = globalRegistry.get(schema);
621
+ if (global) {
622
+ return {
623
+ description: global.description,
624
+ examples: global.examples
625
+ };
626
+ }
627
+ }
628
+ #handleArrayItemJsonSchema([required, schema], options) {
629
+ if (required || options.strategy === "input" || schema.default !== void 0) {
630
+ return schema;
631
+ }
632
+ if (schema === this.undefinedJsonSchema) {
633
+ return { type: "null" };
634
+ }
635
+ return {
636
+ anyOf: [
637
+ // schema can contain { type: 'null' } so we should use anyOf instead of oneOf
638
+ schema,
639
+ { type: "null" }
640
+ ]
641
+ };
642
+ }
643
+ #handleStringFormat(format) {
644
+ if (format === "guid") {
645
+ return JSONSchemaFormat.UUID;
646
+ }
647
+ if (format === "url") {
648
+ return JSONSchemaFormat.URI;
649
+ }
650
+ if (format === "datetime") {
651
+ return JSONSchemaFormat.DateTime;
652
+ }
653
+ return Object.values(JSONSchemaFormat).includes(format) ? format : void 0;
654
+ }
655
+ #handleContentEncoding(contentEncoding) {
656
+ return Object.values(JSONSchemaContentEncoding).includes(contentEncoding) ? contentEncoding : void 0;
657
+ }
658
+ }
659
+
660
+ 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": "0.0.0-next.f81b4a2",
4
+ "version": "0.0.0-next.fa8d145",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -18,23 +18,29 @@
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": "0.0.0-next.f81b4a2",
29
- "@orpc/server": "0.0.0-next.f81b4a2"
32
+ "zod": ">=3.24.2",
33
+ "@orpc/contract": "0.0.0-next.fa8d145",
34
+ "@orpc/server": "0.0.0-next.fa8d145"
30
35
  },
31
36
  "dependencies": {
32
37
  "escape-string-regexp": "^5.0.0",
33
38
  "wildcard-match": "^5.1.3",
34
- "@orpc/openapi": "0.0.0-next.f81b4a2",
35
- "@orpc/shared": "0.0.0-next.f81b4a2"
39
+ "@orpc/openapi": "0.0.0-next.fa8d145",
40
+ "@orpc/shared": "0.0.0-next.fa8d145"
36
41
  },
37
42
  "devDependencies": {
43
+ "zod": "3.25.0-beta.20250516T005923",
38
44
  "zod-to-json-schema": "^3.24.5"
39
45
  },
40
46
  "scripts": {