@orpc/zod 0.0.0-next.f4d410a → 0.0.0-next.f50512c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -25
- package/dist/index.d.mts +20 -11
- package/dist/index.d.ts +20 -11
- package/dist/index.mjs +76 -39
- package/dist/zod4/index.d.mts +314 -0
- package/dist/zod4/index.d.ts +314 -0
- package/dist/zod4/index.mjs +680 -0
- package/package.json +13 -7
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
|
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
|
-
-
|
31
|
-
-
|
32
|
-
-
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
36
|
-
-
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
-
|
41
|
-
-
|
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, Angular), 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,13 +49,15 @@ 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.
|
56
|
-
- [@orpc/react-query](https://www.npmjs.com/package/@orpc/react-query): Integration with [React Query](https://tanstack.com/query/latest/docs/framework/react/overview).
|
57
|
-
- [@orpc/vue-query](https://www.npmjs.com/package/@orpc/vue-query): Integration with [Vue Query](https://tanstack.com/query/latest/docs/framework/vue/overview).
|
58
|
-
- [@orpc/solid-query](https://www.npmjs.com/package/@orpc/solid-query): Integration with [Solid Query](https://tanstack.com/query/latest/docs/framework/solid/overview).
|
59
|
-
- [@orpc/svelte-query](https://www.npmjs.com/package/@orpc/svelte-query): Integration with [Svelte Query](https://tanstack.com/query/latest/docs/framework/svelte/overview).
|
60
|
-
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
61
52
|
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
53
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
54
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
55
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
56
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
57
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
62
58
|
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
59
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
60
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
63
61
|
|
64
62
|
## `@orpc/zod`
|
65
63
|
|
@@ -74,7 +72,7 @@ More schemas that [Zod](https://zod.dev/) doesn't support yet, and provides `Zod
|
|
74
72
|
|
75
73
|
```ts
|
76
74
|
import { oz } from '@orpc/zod'
|
77
|
-
import { z } from 'zod'
|
75
|
+
import { z } from 'zod/v3'
|
78
76
|
|
79
77
|
const Example = z.object({
|
80
78
|
url: oz.url(),
|
@@ -88,7 +86,7 @@ const Example = z.object({
|
|
88
86
|
|
89
87
|
```ts
|
90
88
|
import { OpenAPIGenerator } from '@orpc/openapi'
|
91
|
-
import { ZodToJsonSchemaConverter } from '@orpc/zod'
|
89
|
+
import { ZodToJsonSchemaConverter } from '@orpc/zod/zod4'
|
92
90
|
|
93
91
|
const openAPIGenerator = new OpenAPIGenerator({
|
94
92
|
schemaConverters: [new ZodToJsonSchemaConverter()],
|
@@ -113,7 +111,7 @@ const specFromRouter = await openAPIGenerator.generate(router, {
|
|
113
111
|
|
114
112
|
```ts
|
115
113
|
import { oz } from '@orpc/zod'
|
116
|
-
import
|
114
|
+
import * as z from 'zod'
|
117
115
|
|
118
116
|
const InputSchema = oz.openapi(
|
119
117
|
z.object({
|
@@ -129,6 +127,14 @@ const InputSchema = oz.openapi(
|
|
129
127
|
)
|
130
128
|
```
|
131
129
|
|
130
|
+
## Sponsors
|
131
|
+
|
132
|
+
<p align="center">
|
133
|
+
<a href="https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg">
|
134
|
+
<img src='https://cdn.jsdelivr.net/gh/unnoq/unnoq/sponsors.svg'/>
|
135
|
+
</a>
|
136
|
+
</p>
|
137
|
+
|
132
138
|
## License
|
133
139
|
|
134
140
|
Distributed under the MIT License. See [LICENSE](https://github.com/unnoq/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
import { JSONSchema, ConditionalSchemaConverter, SchemaConvertOptions } from '@orpc/openapi';
|
2
|
-
import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod';
|
2
|
+
import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod/v3';
|
3
3
|
import { Context } from '@orpc/server';
|
4
|
-
import {
|
5
|
-
import { StandardHandlerOptions } from '@orpc/server/standard';
|
4
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
6
5
|
import { AnySchema } from '@orpc/contract';
|
7
6
|
|
8
7
|
declare function getCustomJsonSchema(def: ZodTypeDef, options: {
|
@@ -35,40 +34,49 @@ declare function regexp(params?: string | CustomParams | ((input: unknown) => Cu
|
|
35
34
|
|
36
35
|
declare function url(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<URL, ZodTypeDef, URL>;
|
37
36
|
|
38
|
-
declare class ZodSmartCoercionPlugin<TContext extends Context> implements
|
37
|
+
declare class ZodSmartCoercionPlugin<TContext extends Context> implements StandardHandlerPlugin<TContext> {
|
39
38
|
init(options: StandardHandlerOptions<TContext>): void;
|
40
39
|
}
|
41
40
|
|
42
41
|
interface ZodToJsonSchemaOptions {
|
43
42
|
/**
|
44
|
-
* Max depth of lazy type
|
43
|
+
* Max depth of lazy type
|
45
44
|
*
|
46
|
-
* Used `{}` when
|
45
|
+
* Used `{}` when exceed max depth
|
47
46
|
*
|
48
47
|
* @default 3
|
49
48
|
*/
|
50
49
|
maxLazyDepth?: number;
|
51
50
|
/**
|
52
|
-
*
|
51
|
+
* Max depth of nested types
|
53
52
|
*
|
54
|
-
*
|
53
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
54
|
+
*
|
55
|
+
* @default 10
|
55
56
|
*/
|
56
|
-
|
57
|
+
maxStructureDepth?: number;
|
57
58
|
/**
|
58
59
|
* The schema to be used to represent the any | unknown type.
|
59
60
|
*
|
60
61
|
* @default { }
|
61
62
|
*/
|
62
63
|
anyJsonSchema?: Exclude<JSONSchema, boolean>;
|
64
|
+
/**
|
65
|
+
* The schema to be used when the Zod schema is unsupported.
|
66
|
+
*
|
67
|
+
* @default { not: {} }
|
68
|
+
*/
|
69
|
+
unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
|
63
70
|
}
|
64
71
|
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
65
72
|
#private;
|
66
73
|
private readonly maxLazyDepth;
|
74
|
+
private readonly maxStructureDepth;
|
67
75
|
private readonly unsupportedJsonSchema;
|
68
76
|
private readonly anyJsonSchema;
|
69
77
|
constructor(options?: ZodToJsonSchemaOptions);
|
70
78
|
condition(schema: AnySchema | undefined): boolean;
|
71
|
-
convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
79
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean, structureDepth?: number): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
72
80
|
}
|
73
81
|
|
74
82
|
declare const oz: {
|
@@ -79,4 +87,5 @@ declare const oz: {
|
|
79
87
|
openapi: typeof customJsonSchema;
|
80
88
|
};
|
81
89
|
|
82
|
-
export {
|
90
|
+
export { ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, blob, composeParams, customJsonSchema, file, getCustomJsonSchema, getCustomZodDef, oz, regexp, setCustomZodDef, url };
|
91
|
+
export type { CustomParams, CustomZodDef, ZodToJsonSchemaOptions };
|
package/dist/index.d.ts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
import { JSONSchema, ConditionalSchemaConverter, SchemaConvertOptions } from '@orpc/openapi';
|
2
|
-
import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod';
|
2
|
+
import { ZodTypeAny, input, output, ZodTypeDef, CustomErrorParams, ZodType, ZodEffects } from 'zod/v3';
|
3
3
|
import { Context } from '@orpc/server';
|
4
|
-
import {
|
5
|
-
import { StandardHandlerOptions } from '@orpc/server/standard';
|
4
|
+
import { StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
6
5
|
import { AnySchema } from '@orpc/contract';
|
7
6
|
|
8
7
|
declare function getCustomJsonSchema(def: ZodTypeDef, options: {
|
@@ -35,40 +34,49 @@ declare function regexp(params?: string | CustomParams | ((input: unknown) => Cu
|
|
35
34
|
|
36
35
|
declare function url(params?: string | CustomParams | ((input: unknown) => CustomParams)): ZodType<URL, ZodTypeDef, URL>;
|
37
36
|
|
38
|
-
declare class ZodSmartCoercionPlugin<TContext extends Context> implements
|
37
|
+
declare class ZodSmartCoercionPlugin<TContext extends Context> implements StandardHandlerPlugin<TContext> {
|
39
38
|
init(options: StandardHandlerOptions<TContext>): void;
|
40
39
|
}
|
41
40
|
|
42
41
|
interface ZodToJsonSchemaOptions {
|
43
42
|
/**
|
44
|
-
* Max depth of lazy type
|
43
|
+
* Max depth of lazy type
|
45
44
|
*
|
46
|
-
* Used `{}` when
|
45
|
+
* Used `{}` when exceed max depth
|
47
46
|
*
|
48
47
|
* @default 3
|
49
48
|
*/
|
50
49
|
maxLazyDepth?: number;
|
51
50
|
/**
|
52
|
-
*
|
51
|
+
* Max depth of nested types
|
53
52
|
*
|
54
|
-
*
|
53
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
54
|
+
*
|
55
|
+
* @default 10
|
55
56
|
*/
|
56
|
-
|
57
|
+
maxStructureDepth?: number;
|
57
58
|
/**
|
58
59
|
* The schema to be used to represent the any | unknown type.
|
59
60
|
*
|
60
61
|
* @default { }
|
61
62
|
*/
|
62
63
|
anyJsonSchema?: Exclude<JSONSchema, boolean>;
|
64
|
+
/**
|
65
|
+
* The schema to be used when the Zod schema is unsupported.
|
66
|
+
*
|
67
|
+
* @default { not: {} }
|
68
|
+
*/
|
69
|
+
unsupportedJsonSchema?: Exclude<JSONSchema, boolean>;
|
63
70
|
}
|
64
71
|
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
65
72
|
#private;
|
66
73
|
private readonly maxLazyDepth;
|
74
|
+
private readonly maxStructureDepth;
|
67
75
|
private readonly unsupportedJsonSchema;
|
68
76
|
private readonly anyJsonSchema;
|
69
77
|
constructor(options?: ZodToJsonSchemaOptions);
|
70
78
|
condition(schema: AnySchema | undefined): boolean;
|
71
|
-
convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
79
|
+
convert(schema: AnySchema | undefined, options: SchemaConvertOptions, lazyDepth?: number, isHandledCustomJSONSchema?: boolean, isHandledZodDescription?: boolean, structureDepth?: number): [required: boolean, jsonSchema: Exclude<JSONSchema, boolean>];
|
72
80
|
}
|
73
81
|
|
74
82
|
declare const oz: {
|
@@ -79,4 +87,5 @@ declare const oz: {
|
|
79
87
|
openapi: typeof customJsonSchema;
|
80
88
|
};
|
81
89
|
|
82
|
-
export {
|
90
|
+
export { ZodSmartCoercionPlugin, ZodToJsonSchemaConverter, blob, composeParams, customJsonSchema, file, getCustomJsonSchema, getCustomZodDef, oz, regexp, setCustomZodDef, url };
|
91
|
+
export type { CustomParams, CustomZodDef, ZodToJsonSchemaOptions };
|
package/dist/index.mjs
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import { custom, ZodFirstPartyTypeKind } from 'zod';
|
1
|
+
import { custom, ZodFirstPartyTypeKind } from 'zod/v3';
|
2
2
|
import wcmatch from 'wildcard-match';
|
3
|
-
import { isObject, guard } from '@orpc/shared';
|
3
|
+
import { isObject, guard, toArray } from '@orpc/shared';
|
4
|
+
import { JsonSchemaXNativeType } from '@orpc/json-schema';
|
4
5
|
import { JSONSchemaFormat } from '@orpc/openapi';
|
5
6
|
import escapeStringRegexp from 'escape-string-regexp';
|
6
7
|
|
@@ -390,25 +391,39 @@ function safeToDate(value) {
|
|
390
391
|
|
391
392
|
class ZodToJsonSchemaConverter {
|
392
393
|
maxLazyDepth;
|
394
|
+
maxStructureDepth;
|
393
395
|
unsupportedJsonSchema;
|
394
396
|
anyJsonSchema;
|
395
397
|
constructor(options = {}) {
|
396
398
|
this.maxLazyDepth = options.maxLazyDepth ?? 3;
|
399
|
+
this.maxStructureDepth = options.maxStructureDepth ?? 10;
|
397
400
|
this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
|
398
401
|
this.anyJsonSchema = options.anyJsonSchema ?? {};
|
399
402
|
}
|
400
403
|
condition(schema) {
|
401
404
|
return schema !== void 0 && schema["~standard"].vendor === "zod";
|
402
405
|
}
|
403
|
-
convert(schema, options, lazyDepth = 0, isHandledCustomJSONSchema = false, isHandledZodDescription = false) {
|
406
|
+
convert(schema, options, lazyDepth = 0, isHandledCustomJSONSchema = false, isHandledZodDescription = false, structureDepth = 0) {
|
404
407
|
const def = schema._def;
|
408
|
+
if (structureDepth > this.maxStructureDepth) {
|
409
|
+
return [false, this.anyJsonSchema];
|
410
|
+
}
|
411
|
+
if (!options.minStructureDepthForRef || options.minStructureDepthForRef <= structureDepth) {
|
412
|
+
const components = toArray(options.components);
|
413
|
+
for (const component of components) {
|
414
|
+
if (component.schema === schema && component.allowedStrategies.includes(options.strategy)) {
|
415
|
+
return [component.required, { $ref: component.ref }];
|
416
|
+
}
|
417
|
+
}
|
418
|
+
}
|
405
419
|
if (!isHandledZodDescription && "description" in def && typeof def.description === "string") {
|
406
420
|
const [required, json] = this.convert(
|
407
421
|
schema,
|
408
422
|
options,
|
409
423
|
lazyDepth,
|
410
424
|
isHandledCustomJSONSchema,
|
411
|
-
true
|
425
|
+
true,
|
426
|
+
structureDepth
|
412
427
|
);
|
413
428
|
return [required, { ...json, description: def.description }];
|
414
429
|
}
|
@@ -420,7 +435,8 @@ class ZodToJsonSchemaConverter {
|
|
420
435
|
options,
|
421
436
|
lazyDepth,
|
422
437
|
true,
|
423
|
-
isHandledZodDescription
|
438
|
+
isHandledZodDescription,
|
439
|
+
structureDepth
|
424
440
|
);
|
425
441
|
return [required, { ...json, ...customJSONSchema }];
|
426
442
|
}
|
@@ -548,7 +564,11 @@ class ZodToJsonSchemaConverter {
|
|
548
564
|
return [true, json];
|
549
565
|
}
|
550
566
|
case ZodFirstPartyTypeKind.ZodBigInt: {
|
551
|
-
const json = {
|
567
|
+
const json = {
|
568
|
+
"type": "string",
|
569
|
+
"pattern": "^-?[0-9]+$",
|
570
|
+
"x-native-type": JsonSchemaXNativeType.BigInt
|
571
|
+
};
|
552
572
|
return [true, json];
|
553
573
|
}
|
554
574
|
case ZodFirstPartyTypeKind.ZodNaN: {
|
@@ -558,7 +578,11 @@ class ZodToJsonSchemaConverter {
|
|
558
578
|
return [true, { type: "boolean" }];
|
559
579
|
}
|
560
580
|
case ZodFirstPartyTypeKind.ZodDate: {
|
561
|
-
const schema2 = {
|
581
|
+
const schema2 = {
|
582
|
+
"type": "string",
|
583
|
+
"format": JSONSchemaFormat.DateTime,
|
584
|
+
"x-native-type": JsonSchemaXNativeType.Date
|
585
|
+
};
|
562
586
|
return [true, schema2];
|
563
587
|
}
|
564
588
|
case ZodFirstPartyTypeKind.ZodNull: {
|
@@ -591,7 +615,7 @@ class ZodToJsonSchemaConverter {
|
|
591
615
|
const schema_ = schema;
|
592
616
|
const def2 = schema_._def;
|
593
617
|
const json = { type: "array" };
|
594
|
-
const [itemRequired, itemJson] = this.convert(def2.type, options, lazyDepth, false, false);
|
618
|
+
const [itemRequired, itemJson] = this.convert(def2.type, options, lazyDepth, false, false, structureDepth + 1);
|
595
619
|
json.items = this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy);
|
596
620
|
if (def2.exactLength) {
|
597
621
|
json.maxItems = def2.exactLength.value;
|
@@ -610,7 +634,7 @@ class ZodToJsonSchemaConverter {
|
|
610
634
|
const prefixItems = [];
|
611
635
|
const json = { type: "array" };
|
612
636
|
for (const item of schema_._def.items) {
|
613
|
-
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false);
|
637
|
+
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth + 1);
|
614
638
|
prefixItems.push(
|
615
639
|
this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy)
|
616
640
|
);
|
@@ -619,7 +643,7 @@ class ZodToJsonSchemaConverter {
|
|
619
643
|
json.prefixItems = prefixItems;
|
620
644
|
}
|
621
645
|
if (schema_._def.rest) {
|
622
|
-
const [itemRequired, itemJson] = this.convert(schema_._def.rest, options, lazyDepth, false, false);
|
646
|
+
const [itemRequired, itemJson] = this.convert(schema_._def.rest, options, lazyDepth, false, false, structureDepth + 1);
|
623
647
|
json.items = this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy);
|
624
648
|
}
|
625
649
|
return [true, json];
|
@@ -630,7 +654,7 @@ class ZodToJsonSchemaConverter {
|
|
630
654
|
const properties = {};
|
631
655
|
const required = [];
|
632
656
|
for (const [key, value] of Object.entries(schema_.shape)) {
|
633
|
-
const [itemRequired, itemJson] = this.convert(value, options, lazyDepth, false, false);
|
657
|
+
const [itemRequired, itemJson] = this.convert(value, options, lazyDepth, false, false, structureDepth + 1);
|
634
658
|
properties[key] = itemJson;
|
635
659
|
if (itemRequired) {
|
636
660
|
required.push(key);
|
@@ -648,7 +672,7 @@ class ZodToJsonSchemaConverter {
|
|
648
672
|
json.additionalProperties = false;
|
649
673
|
}
|
650
674
|
} else {
|
651
|
-
const [_, addJson] = this.convert(schema_._def.catchall, options, lazyDepth, false, false);
|
675
|
+
const [_, addJson] = this.convert(schema_._def.catchall, options, lazyDepth, false, false, structureDepth + 1);
|
652
676
|
json.additionalProperties = addJson;
|
653
677
|
}
|
654
678
|
return [true, json];
|
@@ -656,28 +680,32 @@ class ZodToJsonSchemaConverter {
|
|
656
680
|
case ZodFirstPartyTypeKind.ZodRecord: {
|
657
681
|
const schema_ = schema;
|
658
682
|
const json = { type: "object" };
|
659
|
-
const [__, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false);
|
683
|
+
const [__, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false, structureDepth + 1);
|
660
684
|
if (Object.entries(keyJson).some(([k, v]) => k !== "type" || v !== "string")) {
|
661
685
|
json.propertyNames = keyJson;
|
662
686
|
}
|
663
|
-
const [_, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false);
|
687
|
+
const [_, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false, structureDepth + 1);
|
664
688
|
json.additionalProperties = itemJson;
|
665
689
|
return [true, json];
|
666
690
|
}
|
667
691
|
case ZodFirstPartyTypeKind.ZodSet: {
|
668
692
|
const schema_ = schema;
|
669
|
-
const json = {
|
670
|
-
|
693
|
+
const json = {
|
694
|
+
"type": "array",
|
695
|
+
"uniqueItems": true,
|
696
|
+
"x-native-type": JsonSchemaXNativeType.Set
|
697
|
+
};
|
698
|
+
const [itemRequired, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false, structureDepth + 1);
|
671
699
|
json.items = this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy);
|
672
700
|
return [true, json];
|
673
701
|
}
|
674
702
|
case ZodFirstPartyTypeKind.ZodMap: {
|
675
703
|
const schema_ = schema;
|
676
|
-
const [keyRequired, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false);
|
677
|
-
const [valueRequired, valueJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false);
|
678
|
-
|
679
|
-
type: "array",
|
680
|
-
items: {
|
704
|
+
const [keyRequired, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false, structureDepth + 1);
|
705
|
+
const [valueRequired, valueJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false, structureDepth + 1);
|
706
|
+
const json = {
|
707
|
+
"type": "array",
|
708
|
+
"items": {
|
681
709
|
type: "array",
|
682
710
|
prefixItems: [
|
683
711
|
this.#toArrayItemJsonSchema(keyRequired, keyJson, options.strategy),
|
@@ -685,8 +713,10 @@ class ZodToJsonSchemaConverter {
|
|
685
713
|
],
|
686
714
|
maxItems: 2,
|
687
715
|
minItems: 2
|
688
|
-
}
|
689
|
-
|
716
|
+
},
|
717
|
+
"x-native-type": JsonSchemaXNativeType.Map
|
718
|
+
};
|
719
|
+
return [true, json];
|
690
720
|
}
|
691
721
|
case ZodFirstPartyTypeKind.ZodUnion:
|
692
722
|
case ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
|
@@ -694,7 +724,7 @@ class ZodToJsonSchemaConverter {
|
|
694
724
|
const anyOf = [];
|
695
725
|
let required = true;
|
696
726
|
for (const item of schema_._def.options) {
|
697
|
-
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false);
|
727
|
+
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth);
|
698
728
|
if (!itemRequired) {
|
699
729
|
required = false;
|
700
730
|
if (itemJson !== this.unsupportedJsonSchema) {
|
@@ -714,7 +744,7 @@ class ZodToJsonSchemaConverter {
|
|
714
744
|
const allOf = [];
|
715
745
|
let required = false;
|
716
746
|
for (const item of [schema_._def.left, schema_._def.right]) {
|
717
|
-
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false);
|
747
|
+
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth);
|
718
748
|
allOf.push(itemJson);
|
719
749
|
if (itemRequired) {
|
720
750
|
required = true;
|
@@ -723,25 +753,26 @@ class ZodToJsonSchemaConverter {
|
|
723
753
|
return [required, { allOf }];
|
724
754
|
}
|
725
755
|
case ZodFirstPartyTypeKind.ZodLazy: {
|
726
|
-
|
756
|
+
const currentLazyDepth = lazyDepth + 1;
|
757
|
+
if (currentLazyDepth > this.maxLazyDepth) {
|
727
758
|
return [false, this.anyJsonSchema];
|
728
759
|
}
|
729
760
|
const schema_ = schema;
|
730
|
-
return this.convert(schema_._def.getter(), options,
|
761
|
+
return this.convert(schema_._def.getter(), options, currentLazyDepth, false, false, structureDepth);
|
731
762
|
}
|
732
763
|
case ZodFirstPartyTypeKind.ZodOptional: {
|
733
764
|
const schema_ = schema;
|
734
|
-
const [_, inner] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
765
|
+
const [_, inner] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
735
766
|
return [false, inner];
|
736
767
|
}
|
737
768
|
case ZodFirstPartyTypeKind.ZodReadonly: {
|
738
769
|
const schema_ = schema;
|
739
|
-
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
770
|
+
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
740
771
|
return [required, { ...json, readOnly: true }];
|
741
772
|
}
|
742
773
|
case ZodFirstPartyTypeKind.ZodDefault: {
|
743
774
|
const schema_ = schema;
|
744
|
-
const [_, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
775
|
+
const [_, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
745
776
|
return [false, { default: schema_._def.defaultValue(), ...json }];
|
746
777
|
}
|
747
778
|
case ZodFirstPartyTypeKind.ZodEffects: {
|
@@ -749,15 +780,15 @@ class ZodToJsonSchemaConverter {
|
|
749
780
|
if (schema_._def.effect.type === "transform" && options.strategy === "output") {
|
750
781
|
return [false, this.anyJsonSchema];
|
751
782
|
}
|
752
|
-
return this.convert(schema_._def.schema, options, lazyDepth, false, false);
|
783
|
+
return this.convert(schema_._def.schema, options, lazyDepth, false, false, structureDepth);
|
753
784
|
}
|
754
785
|
case ZodFirstPartyTypeKind.ZodCatch: {
|
755
786
|
const schema_ = schema;
|
756
|
-
return this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
787
|
+
return this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
757
788
|
}
|
758
789
|
case ZodFirstPartyTypeKind.ZodBranded: {
|
759
790
|
const schema_ = schema;
|
760
|
-
return this.convert(schema_._def.type, options, lazyDepth, false, false);
|
791
|
+
return this.convert(schema_._def.type, options, lazyDepth, false, false, structureDepth);
|
761
792
|
}
|
762
793
|
case ZodFirstPartyTypeKind.ZodPipeline: {
|
763
794
|
const schema_ = schema;
|
@@ -766,13 +797,14 @@ class ZodToJsonSchemaConverter {
|
|
766
797
|
options,
|
767
798
|
lazyDepth,
|
768
799
|
false,
|
769
|
-
false
|
800
|
+
false,
|
801
|
+
structureDepth
|
770
802
|
);
|
771
803
|
}
|
772
804
|
case ZodFirstPartyTypeKind.ZodNullable: {
|
773
805
|
const schema_ = schema;
|
774
|
-
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
775
|
-
return [required, { anyOf: [{ type: "null" }
|
806
|
+
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
807
|
+
return [required, { anyOf: [json, { type: "null" }] }];
|
776
808
|
}
|
777
809
|
}
|
778
810
|
return [true, this.unsupportedJsonSchema];
|
@@ -791,12 +823,17 @@ class ZodToJsonSchemaConverter {
|
|
791
823
|
}
|
792
824
|
case "regexp": {
|
793
825
|
return {
|
794
|
-
type: "string",
|
795
|
-
pattern: "^\\/(.*)\\/([a-z]*)$"
|
826
|
+
"type": "string",
|
827
|
+
"pattern": "^\\/(.*)\\/([a-z]*)$",
|
828
|
+
"x-native-type": JsonSchemaXNativeType.RegExp
|
796
829
|
};
|
797
830
|
}
|
798
831
|
case "url": {
|
799
|
-
return {
|
832
|
+
return {
|
833
|
+
"type": "string",
|
834
|
+
"format": JSONSchemaFormat.URI,
|
835
|
+
"x-native-type": JsonSchemaXNativeType.Url
|
836
|
+
};
|
800
837
|
}
|
801
838
|
}
|
802
839
|
}
|