@orpc/zod 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +15 -6
- package/dist/index.d.ts +15 -6
- package/dist/index.mjs +43 -26
- package/dist/zod4/index.d.mts +11 -2
- package/dist/zod4/index.d.ts +11 -2
- package/dist/zod4/index.mjs +39 -25
- package/package.json +6 -6
package/dist/index.d.mts
CHANGED
@@ -40,34 +40,43 @@ declare class ZodSmartCoercionPlugin<TContext extends Context> implements Standa
|
|
40
40
|
|
41
41
|
interface ZodToJsonSchemaOptions {
|
42
42
|
/**
|
43
|
-
* Max depth of lazy type
|
43
|
+
* Max depth of lazy type
|
44
44
|
*
|
45
|
-
* Used `{}` when
|
45
|
+
* Used `{}` when exceed max depth
|
46
46
|
*
|
47
47
|
* @default 3
|
48
48
|
*/
|
49
49
|
maxLazyDepth?: number;
|
50
50
|
/**
|
51
|
-
*
|
51
|
+
* Max depth of nested types
|
52
52
|
*
|
53
|
-
*
|
53
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
54
|
+
*
|
55
|
+
* @default 10
|
54
56
|
*/
|
55
|
-
|
57
|
+
maxStructureDepth?: number;
|
56
58
|
/**
|
57
59
|
* The schema to be used to represent the any | unknown type.
|
58
60
|
*
|
59
61
|
* @default { }
|
60
62
|
*/
|
61
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>;
|
62
70
|
}
|
63
71
|
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
64
72
|
#private;
|
65
73
|
private readonly maxLazyDepth;
|
74
|
+
private readonly maxStructureDepth;
|
66
75
|
private readonly unsupportedJsonSchema;
|
67
76
|
private readonly anyJsonSchema;
|
68
77
|
constructor(options?: ZodToJsonSchemaOptions);
|
69
78
|
condition(schema: AnySchema | undefined): boolean;
|
70
|
-
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>];
|
71
80
|
}
|
72
81
|
|
73
82
|
declare const oz: {
|
package/dist/index.d.ts
CHANGED
@@ -40,34 +40,43 @@ declare class ZodSmartCoercionPlugin<TContext extends Context> implements Standa
|
|
40
40
|
|
41
41
|
interface ZodToJsonSchemaOptions {
|
42
42
|
/**
|
43
|
-
* Max depth of lazy type
|
43
|
+
* Max depth of lazy type
|
44
44
|
*
|
45
|
-
* Used `{}` when
|
45
|
+
* Used `{}` when exceed max depth
|
46
46
|
*
|
47
47
|
* @default 3
|
48
48
|
*/
|
49
49
|
maxLazyDepth?: number;
|
50
50
|
/**
|
51
|
-
*
|
51
|
+
* Max depth of nested types
|
52
52
|
*
|
53
|
-
*
|
53
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
54
|
+
*
|
55
|
+
* @default 10
|
54
56
|
*/
|
55
|
-
|
57
|
+
maxStructureDepth?: number;
|
56
58
|
/**
|
57
59
|
* The schema to be used to represent the any | unknown type.
|
58
60
|
*
|
59
61
|
* @default { }
|
60
62
|
*/
|
61
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>;
|
62
70
|
}
|
63
71
|
declare class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
64
72
|
#private;
|
65
73
|
private readonly maxLazyDepth;
|
74
|
+
private readonly maxStructureDepth;
|
66
75
|
private readonly unsupportedJsonSchema;
|
67
76
|
private readonly anyJsonSchema;
|
68
77
|
constructor(options?: ZodToJsonSchemaOptions);
|
69
78
|
condition(schema: AnySchema | undefined): boolean;
|
70
|
-
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>];
|
71
80
|
}
|
72
81
|
|
73
82
|
declare const oz: {
|
package/dist/index.mjs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { custom, ZodFirstPartyTypeKind } from 'zod';
|
2
2
|
import wcmatch from 'wildcard-match';
|
3
|
-
import { isObject, guard } from '@orpc/shared';
|
3
|
+
import { isObject, guard, toArray } from '@orpc/shared';
|
4
4
|
import { JSONSchemaFormat } from '@orpc/openapi';
|
5
5
|
import escapeStringRegexp from 'escape-string-regexp';
|
6
6
|
|
@@ -390,25 +390,39 @@ function safeToDate(value) {
|
|
390
390
|
|
391
391
|
class ZodToJsonSchemaConverter {
|
392
392
|
maxLazyDepth;
|
393
|
+
maxStructureDepth;
|
393
394
|
unsupportedJsonSchema;
|
394
395
|
anyJsonSchema;
|
395
396
|
constructor(options = {}) {
|
396
397
|
this.maxLazyDepth = options.maxLazyDepth ?? 3;
|
398
|
+
this.maxStructureDepth = options.maxStructureDepth ?? 10;
|
397
399
|
this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
|
398
400
|
this.anyJsonSchema = options.anyJsonSchema ?? {};
|
399
401
|
}
|
400
402
|
condition(schema) {
|
401
403
|
return schema !== void 0 && schema["~standard"].vendor === "zod";
|
402
404
|
}
|
403
|
-
convert(schema, options, lazyDepth = 0, isHandledCustomJSONSchema = false, isHandledZodDescription = false) {
|
405
|
+
convert(schema, options, lazyDepth = 0, isHandledCustomJSONSchema = false, isHandledZodDescription = false, structureDepth = 0) {
|
404
406
|
const def = schema._def;
|
407
|
+
if (structureDepth > this.maxStructureDepth) {
|
408
|
+
return [false, this.anyJsonSchema];
|
409
|
+
}
|
410
|
+
if (!options.minStructureDepthForRef || options.minStructureDepthForRef <= structureDepth) {
|
411
|
+
const components = toArray(options.components);
|
412
|
+
for (const component of components) {
|
413
|
+
if (component.schema === schema && component.allowedStrategies.includes(options.strategy)) {
|
414
|
+
return [component.required, { $ref: component.ref }];
|
415
|
+
}
|
416
|
+
}
|
417
|
+
}
|
405
418
|
if (!isHandledZodDescription && "description" in def && typeof def.description === "string") {
|
406
419
|
const [required, json] = this.convert(
|
407
420
|
schema,
|
408
421
|
options,
|
409
422
|
lazyDepth,
|
410
423
|
isHandledCustomJSONSchema,
|
411
|
-
true
|
424
|
+
true,
|
425
|
+
structureDepth
|
412
426
|
);
|
413
427
|
return [required, { ...json, description: def.description }];
|
414
428
|
}
|
@@ -420,7 +434,8 @@ class ZodToJsonSchemaConverter {
|
|
420
434
|
options,
|
421
435
|
lazyDepth,
|
422
436
|
true,
|
423
|
-
isHandledZodDescription
|
437
|
+
isHandledZodDescription,
|
438
|
+
structureDepth
|
424
439
|
);
|
425
440
|
return [required, { ...json, ...customJSONSchema }];
|
426
441
|
}
|
@@ -591,7 +606,7 @@ class ZodToJsonSchemaConverter {
|
|
591
606
|
const schema_ = schema;
|
592
607
|
const def2 = schema_._def;
|
593
608
|
const json = { type: "array" };
|
594
|
-
const [itemRequired, itemJson] = this.convert(def2.type, options, lazyDepth, false, false);
|
609
|
+
const [itemRequired, itemJson] = this.convert(def2.type, options, lazyDepth, false, false, structureDepth + 1);
|
595
610
|
json.items = this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy);
|
596
611
|
if (def2.exactLength) {
|
597
612
|
json.maxItems = def2.exactLength.value;
|
@@ -610,7 +625,7 @@ class ZodToJsonSchemaConverter {
|
|
610
625
|
const prefixItems = [];
|
611
626
|
const json = { type: "array" };
|
612
627
|
for (const item of schema_._def.items) {
|
613
|
-
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false);
|
628
|
+
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth + 1);
|
614
629
|
prefixItems.push(
|
615
630
|
this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy)
|
616
631
|
);
|
@@ -619,7 +634,7 @@ class ZodToJsonSchemaConverter {
|
|
619
634
|
json.prefixItems = prefixItems;
|
620
635
|
}
|
621
636
|
if (schema_._def.rest) {
|
622
|
-
const [itemRequired, itemJson] = this.convert(schema_._def.rest, options, lazyDepth, false, false);
|
637
|
+
const [itemRequired, itemJson] = this.convert(schema_._def.rest, options, lazyDepth, false, false, structureDepth + 1);
|
623
638
|
json.items = this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy);
|
624
639
|
}
|
625
640
|
return [true, json];
|
@@ -630,7 +645,7 @@ class ZodToJsonSchemaConverter {
|
|
630
645
|
const properties = {};
|
631
646
|
const required = [];
|
632
647
|
for (const [key, value] of Object.entries(schema_.shape)) {
|
633
|
-
const [itemRequired, itemJson] = this.convert(value, options, lazyDepth, false, false);
|
648
|
+
const [itemRequired, itemJson] = this.convert(value, options, lazyDepth, false, false, structureDepth + 1);
|
634
649
|
properties[key] = itemJson;
|
635
650
|
if (itemRequired) {
|
636
651
|
required.push(key);
|
@@ -648,7 +663,7 @@ class ZodToJsonSchemaConverter {
|
|
648
663
|
json.additionalProperties = false;
|
649
664
|
}
|
650
665
|
} else {
|
651
|
-
const [_, addJson] = this.convert(schema_._def.catchall, options, lazyDepth, false, false);
|
666
|
+
const [_, addJson] = this.convert(schema_._def.catchall, options, lazyDepth, false, false, structureDepth + 1);
|
652
667
|
json.additionalProperties = addJson;
|
653
668
|
}
|
654
669
|
return [true, json];
|
@@ -656,25 +671,25 @@ class ZodToJsonSchemaConverter {
|
|
656
671
|
case ZodFirstPartyTypeKind.ZodRecord: {
|
657
672
|
const schema_ = schema;
|
658
673
|
const json = { type: "object" };
|
659
|
-
const [__, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false);
|
674
|
+
const [__, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false, structureDepth + 1);
|
660
675
|
if (Object.entries(keyJson).some(([k, v]) => k !== "type" || v !== "string")) {
|
661
676
|
json.propertyNames = keyJson;
|
662
677
|
}
|
663
|
-
const [_, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false);
|
678
|
+
const [_, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false, structureDepth + 1);
|
664
679
|
json.additionalProperties = itemJson;
|
665
680
|
return [true, json];
|
666
681
|
}
|
667
682
|
case ZodFirstPartyTypeKind.ZodSet: {
|
668
683
|
const schema_ = schema;
|
669
684
|
const json = { type: "array", uniqueItems: true };
|
670
|
-
const [itemRequired, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false);
|
685
|
+
const [itemRequired, itemJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false, structureDepth + 1);
|
671
686
|
json.items = this.#toArrayItemJsonSchema(itemRequired, itemJson, options.strategy);
|
672
687
|
return [true, json];
|
673
688
|
}
|
674
689
|
case ZodFirstPartyTypeKind.ZodMap: {
|
675
690
|
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);
|
691
|
+
const [keyRequired, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false, structureDepth + 1);
|
692
|
+
const [valueRequired, valueJson] = this.convert(schema_._def.valueType, options, lazyDepth, false, false, structureDepth + 1);
|
678
693
|
return [true, {
|
679
694
|
type: "array",
|
680
695
|
items: {
|
@@ -694,7 +709,7 @@ class ZodToJsonSchemaConverter {
|
|
694
709
|
const anyOf = [];
|
695
710
|
let required = true;
|
696
711
|
for (const item of schema_._def.options) {
|
697
|
-
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false);
|
712
|
+
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth);
|
698
713
|
if (!itemRequired) {
|
699
714
|
required = false;
|
700
715
|
if (itemJson !== this.unsupportedJsonSchema) {
|
@@ -714,7 +729,7 @@ class ZodToJsonSchemaConverter {
|
|
714
729
|
const allOf = [];
|
715
730
|
let required = false;
|
716
731
|
for (const item of [schema_._def.left, schema_._def.right]) {
|
717
|
-
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false);
|
732
|
+
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth);
|
718
733
|
allOf.push(itemJson);
|
719
734
|
if (itemRequired) {
|
720
735
|
required = true;
|
@@ -723,25 +738,26 @@ class ZodToJsonSchemaConverter {
|
|
723
738
|
return [required, { allOf }];
|
724
739
|
}
|
725
740
|
case ZodFirstPartyTypeKind.ZodLazy: {
|
726
|
-
|
741
|
+
const currentLazyDepth = lazyDepth + 1;
|
742
|
+
if (currentLazyDepth > this.maxLazyDepth) {
|
727
743
|
return [false, this.anyJsonSchema];
|
728
744
|
}
|
729
745
|
const schema_ = schema;
|
730
|
-
return this.convert(schema_._def.getter(), options,
|
746
|
+
return this.convert(schema_._def.getter(), options, currentLazyDepth, false, false, structureDepth);
|
731
747
|
}
|
732
748
|
case ZodFirstPartyTypeKind.ZodOptional: {
|
733
749
|
const schema_ = schema;
|
734
|
-
const [_, inner] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
750
|
+
const [_, inner] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
735
751
|
return [false, inner];
|
736
752
|
}
|
737
753
|
case ZodFirstPartyTypeKind.ZodReadonly: {
|
738
754
|
const schema_ = schema;
|
739
|
-
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
755
|
+
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
740
756
|
return [required, { ...json, readOnly: true }];
|
741
757
|
}
|
742
758
|
case ZodFirstPartyTypeKind.ZodDefault: {
|
743
759
|
const schema_ = schema;
|
744
|
-
const [_, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
760
|
+
const [_, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
745
761
|
return [false, { default: schema_._def.defaultValue(), ...json }];
|
746
762
|
}
|
747
763
|
case ZodFirstPartyTypeKind.ZodEffects: {
|
@@ -749,15 +765,15 @@ class ZodToJsonSchemaConverter {
|
|
749
765
|
if (schema_._def.effect.type === "transform" && options.strategy === "output") {
|
750
766
|
return [false, this.anyJsonSchema];
|
751
767
|
}
|
752
|
-
return this.convert(schema_._def.schema, options, lazyDepth, false, false);
|
768
|
+
return this.convert(schema_._def.schema, options, lazyDepth, false, false, structureDepth);
|
753
769
|
}
|
754
770
|
case ZodFirstPartyTypeKind.ZodCatch: {
|
755
771
|
const schema_ = schema;
|
756
|
-
return this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
772
|
+
return this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
757
773
|
}
|
758
774
|
case ZodFirstPartyTypeKind.ZodBranded: {
|
759
775
|
const schema_ = schema;
|
760
|
-
return this.convert(schema_._def.type, options, lazyDepth, false, false);
|
776
|
+
return this.convert(schema_._def.type, options, lazyDepth, false, false, structureDepth);
|
761
777
|
}
|
762
778
|
case ZodFirstPartyTypeKind.ZodPipeline: {
|
763
779
|
const schema_ = schema;
|
@@ -766,12 +782,13 @@ class ZodToJsonSchemaConverter {
|
|
766
782
|
options,
|
767
783
|
lazyDepth,
|
768
784
|
false,
|
769
|
-
false
|
785
|
+
false,
|
786
|
+
structureDepth
|
770
787
|
);
|
771
788
|
}
|
772
789
|
case ZodFirstPartyTypeKind.ZodNullable: {
|
773
790
|
const schema_ = schema;
|
774
|
-
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false);
|
791
|
+
const [required, json] = this.convert(schema_._def.innerType, options, lazyDepth, false, false, structureDepth);
|
775
792
|
return [required, { anyOf: [{ type: "null" }, json] }];
|
776
793
|
}
|
777
794
|
}
|
package/dist/zod4/index.d.mts
CHANGED
@@ -13,13 +13,21 @@ declare class experimental_ZodSmartCoercionPlugin<TContext extends Context> impl
|
|
13
13
|
|
14
14
|
interface experimental_ZodToJsonSchemaOptions {
|
15
15
|
/**
|
16
|
-
* Max depth of lazy type
|
16
|
+
* Max depth of lazy type.
|
17
17
|
*
|
18
|
-
* Used anyJsonSchema (`{}`) when
|
18
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
19
19
|
*
|
20
20
|
* @default 2
|
21
21
|
*/
|
22
22
|
maxLazyDepth?: number;
|
23
|
+
/**
|
24
|
+
* Max depth of nested types.
|
25
|
+
*
|
26
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
27
|
+
*
|
28
|
+
* @default 10
|
29
|
+
*/
|
30
|
+
maxStructureDepth?: number;
|
23
31
|
/**
|
24
32
|
* The schema to be used to represent the any | unknown type.
|
25
33
|
*
|
@@ -51,6 +59,7 @@ interface experimental_ZodToJsonSchemaOptions {
|
|
51
59
|
declare class experimental_ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
52
60
|
#private;
|
53
61
|
private readonly maxLazyDepth;
|
62
|
+
private readonly maxStructureDepth;
|
54
63
|
private readonly anyJsonSchema;
|
55
64
|
private readonly unsupportedJsonSchema;
|
56
65
|
private readonly undefinedJsonSchema;
|
package/dist/zod4/index.d.ts
CHANGED
@@ -13,13 +13,21 @@ declare class experimental_ZodSmartCoercionPlugin<TContext extends Context> impl
|
|
13
13
|
|
14
14
|
interface experimental_ZodToJsonSchemaOptions {
|
15
15
|
/**
|
16
|
-
* Max depth of lazy type
|
16
|
+
* Max depth of lazy type.
|
17
17
|
*
|
18
|
-
* Used anyJsonSchema (`{}`) when
|
18
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
19
19
|
*
|
20
20
|
* @default 2
|
21
21
|
*/
|
22
22
|
maxLazyDepth?: number;
|
23
|
+
/**
|
24
|
+
* Max depth of nested types.
|
25
|
+
*
|
26
|
+
* Used anyJsonSchema (`{}`) when exceed max depth
|
27
|
+
*
|
28
|
+
* @default 10
|
29
|
+
*/
|
30
|
+
maxStructureDepth?: number;
|
23
31
|
/**
|
24
32
|
* The schema to be used to represent the any | unknown type.
|
25
33
|
*
|
@@ -51,6 +59,7 @@ interface experimental_ZodToJsonSchemaOptions {
|
|
51
59
|
declare class experimental_ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
|
52
60
|
#private;
|
53
61
|
private readonly maxLazyDepth;
|
62
|
+
private readonly maxStructureDepth;
|
54
63
|
private readonly anyJsonSchema;
|
55
64
|
private readonly unsupportedJsonSchema;
|
56
65
|
private readonly undefinedJsonSchema;
|
package/dist/zod4/index.mjs
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { isObject, guard, intercept } from '@orpc/shared';
|
1
|
+
import { isObject, guard, intercept, toArray } from '@orpc/shared';
|
2
2
|
import { JSONSchemaFormat, JSONSchemaContentEncoding } from '@orpc/openapi';
|
3
3
|
import { registry, globalRegistry } from 'zod/v4/core';
|
4
4
|
|
@@ -274,12 +274,14 @@ const experimental_JSON_SCHEMA_OUTPUT_REGISTRY = registry();
|
|
274
274
|
|
275
275
|
class experimental_ZodToJsonSchemaConverter {
|
276
276
|
maxLazyDepth;
|
277
|
+
maxStructureDepth;
|
277
278
|
anyJsonSchema;
|
278
279
|
unsupportedJsonSchema;
|
279
280
|
undefinedJsonSchema;
|
280
281
|
interceptors;
|
281
282
|
constructor(options = {}) {
|
282
283
|
this.maxLazyDepth = options.maxLazyDepth ?? 2;
|
284
|
+
this.maxStructureDepth = options.maxStructureDepth ?? 10;
|
283
285
|
this.anyJsonSchema = options.anyJsonSchema ?? {};
|
284
286
|
this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
|
285
287
|
this.undefinedJsonSchema = options.undefinedJsonSchema ?? { not: {} };
|
@@ -289,17 +291,28 @@ class experimental_ZodToJsonSchemaConverter {
|
|
289
291
|
return schema !== void 0 && schema["~standard"].vendor === "zod";
|
290
292
|
}
|
291
293
|
convert(schema, options) {
|
292
|
-
return this.#convert(schema, options, 0);
|
294
|
+
return this.#convert(schema, options, 0, 0);
|
293
295
|
}
|
294
|
-
#convert(schema, options, lazyDepth, isHandledCustomJSONSchema = false) {
|
296
|
+
#convert(schema, options, lazyDepth, structureDepth, isHandledCustomJSONSchema = false) {
|
295
297
|
return intercept(
|
296
298
|
this.interceptors,
|
297
299
|
{ schema, options, lazyDepth, isHandledCustomJSONSchema },
|
298
300
|
({ schema: schema2, options: options2, lazyDepth: lazyDepth2, isHandledCustomJSONSchema: isHandledCustomJSONSchema2 }) => {
|
301
|
+
if (structureDepth > this.maxStructureDepth) {
|
302
|
+
return [false, this.anyJsonSchema];
|
303
|
+
}
|
304
|
+
if (!options2.minStructureDepthForRef || options2.minStructureDepthForRef <= structureDepth) {
|
305
|
+
const components = toArray(options2.components);
|
306
|
+
for (const component of components) {
|
307
|
+
if (component.schema === schema2 && component.allowedStrategies.includes(options2.strategy)) {
|
308
|
+
return [component.required, { $ref: component.ref }];
|
309
|
+
}
|
310
|
+
}
|
311
|
+
}
|
299
312
|
if (!isHandledCustomJSONSchema2) {
|
300
313
|
const customJSONSchema = this.#getCustomJsonSchema(schema2, options2);
|
301
314
|
if (customJSONSchema) {
|
302
|
-
const [required, json] = this.#convert(schema2, options2, lazyDepth2, true);
|
315
|
+
const [required, json] = this.#convert(schema2, options2, lazyDepth2, structureDepth, true);
|
303
316
|
return [required, { ...json, ...customJSONSchema }];
|
304
317
|
}
|
305
318
|
}
|
@@ -394,14 +407,14 @@ class experimental_ZodToJsonSchemaConverter {
|
|
394
407
|
if (typeof maximum === "number") {
|
395
408
|
json.maxItems = maximum;
|
396
409
|
}
|
397
|
-
json.items = this.#handleArrayItemJsonSchema(this.#convert(array._zod.def.element, options2, lazyDepth2), options2);
|
410
|
+
json.items = this.#handleArrayItemJsonSchema(this.#convert(array._zod.def.element, options2, lazyDepth2, structureDepth + 1), options2);
|
398
411
|
return [true, json];
|
399
412
|
}
|
400
413
|
case "object": {
|
401
414
|
const object = schema2;
|
402
415
|
const json = { type: "object" };
|
403
416
|
for (const [key, value] of Object.entries(object._zod.def.shape)) {
|
404
|
-
const [itemRequired, itemJson] = this.#convert(value, options2, lazyDepth2);
|
417
|
+
const [itemRequired, itemJson] = this.#convert(value, options2, lazyDepth2, structureDepth + 1);
|
405
418
|
json.properties ??= {};
|
406
419
|
json.properties[key] = itemJson;
|
407
420
|
if (itemRequired) {
|
@@ -413,7 +426,7 @@ class experimental_ZodToJsonSchemaConverter {
|
|
413
426
|
if (object._zod.def.catchall._zod.def.type === "never") {
|
414
427
|
json.additionalProperties = false;
|
415
428
|
} else {
|
416
|
-
const [_, addJson] = this.#convert(object._zod.def.catchall, options2, lazyDepth2);
|
429
|
+
const [_, addJson] = this.#convert(object._zod.def.catchall, options2, lazyDepth2, structureDepth + 1);
|
417
430
|
json.additionalProperties = addJson;
|
418
431
|
}
|
419
432
|
}
|
@@ -424,7 +437,7 @@ class experimental_ZodToJsonSchemaConverter {
|
|
424
437
|
const anyOf = [];
|
425
438
|
let required = true;
|
426
439
|
for (const item of union._zod.def.options) {
|
427
|
-
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2);
|
440
|
+
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2, structureDepth);
|
428
441
|
if (!itemRequired) {
|
429
442
|
required = false;
|
430
443
|
}
|
@@ -445,7 +458,7 @@ class experimental_ZodToJsonSchemaConverter {
|
|
445
458
|
const json = { allOf: [] };
|
446
459
|
let required = false;
|
447
460
|
for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
|
448
|
-
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2);
|
461
|
+
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2, structureDepth);
|
449
462
|
json.allOf.push(itemJson);
|
450
463
|
if (itemRequired) {
|
451
464
|
required = true;
|
@@ -457,10 +470,10 @@ class experimental_ZodToJsonSchemaConverter {
|
|
457
470
|
const tuple = schema2;
|
458
471
|
const json = { type: "array", prefixItems: [] };
|
459
472
|
for (const item of tuple._zod.def.items) {
|
460
|
-
json.prefixItems.push(this.#handleArrayItemJsonSchema(this.#convert(item, options2, lazyDepth2), options2));
|
473
|
+
json.prefixItems.push(this.#handleArrayItemJsonSchema(this.#convert(item, options2, lazyDepth2, structureDepth + 1), options2));
|
461
474
|
}
|
462
475
|
if (tuple._zod.def.rest) {
|
463
|
-
json.items = this.#handleArrayItemJsonSchema(this.#convert(tuple._zod.def.rest, options2, lazyDepth2), options2);
|
476
|
+
json.items = this.#handleArrayItemJsonSchema(this.#convert(tuple._zod.def.rest, options2, lazyDepth2, structureDepth + 1), options2);
|
464
477
|
}
|
465
478
|
const { minimum, maximum } = tuple._zod.bag;
|
466
479
|
if (typeof minimum === "number") {
|
@@ -474,8 +487,8 @@ class experimental_ZodToJsonSchemaConverter {
|
|
474
487
|
case "record": {
|
475
488
|
const record = schema2;
|
476
489
|
const json = { type: "object" };
|
477
|
-
json.propertyNames = this.#convert(record._zod.def.keyType, options2, lazyDepth2)[1];
|
478
|
-
json.additionalProperties = this.#convert(record._zod.def.valueType, options2, lazyDepth2)[1];
|
490
|
+
json.propertyNames = this.#convert(record._zod.def.keyType, options2, lazyDepth2, structureDepth + 1)[1];
|
491
|
+
json.additionalProperties = this.#convert(record._zod.def.valueType, options2, lazyDepth2, structureDepth + 1)[1];
|
479
492
|
return [true, json];
|
480
493
|
}
|
481
494
|
case "map": {
|
@@ -485,8 +498,8 @@ class experimental_ZodToJsonSchemaConverter {
|
|
485
498
|
items: {
|
486
499
|
type: "array",
|
487
500
|
prefixItems: [
|
488
|
-
this.#handleArrayItemJsonSchema(this.#convert(map._zod.def.keyType, options2, lazyDepth2), options2),
|
489
|
-
this.#handleArrayItemJsonSchema(this.#convert(map._zod.def.valueType, options2, lazyDepth2), options2)
|
501
|
+
this.#handleArrayItemJsonSchema(this.#convert(map._zod.def.keyType, options2, lazyDepth2, structureDepth + 1), options2),
|
502
|
+
this.#handleArrayItemJsonSchema(this.#convert(map._zod.def.valueType, options2, lazyDepth2, structureDepth + 1), options2)
|
490
503
|
],
|
491
504
|
maxItems: 2,
|
492
505
|
minItems: 2
|
@@ -498,7 +511,7 @@ class experimental_ZodToJsonSchemaConverter {
|
|
498
511
|
return [true, {
|
499
512
|
type: "array",
|
500
513
|
uniqueItems: true,
|
501
|
-
items: this.#handleArrayItemJsonSchema(this.#convert(set._zod.def.valueType, options2, lazyDepth2), options2)
|
514
|
+
items: this.#handleArrayItemJsonSchema(this.#convert(set._zod.def.valueType, options2, lazyDepth2, structureDepth + 1), options2)
|
502
515
|
}];
|
503
516
|
}
|
504
517
|
case "enum": {
|
@@ -538,12 +551,12 @@ class experimental_ZodToJsonSchemaConverter {
|
|
538
551
|
}
|
539
552
|
case "nullable": {
|
540
553
|
const nullable = schema2;
|
541
|
-
const [required, json] = this.#convert(nullable._zod.def.innerType, options2, lazyDepth2);
|
554
|
+
const [required, json] = this.#convert(nullable._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
542
555
|
return [required, { anyOf: [json, { type: "null" }] }];
|
543
556
|
}
|
544
557
|
case "nonoptional": {
|
545
558
|
const nonoptional = schema2;
|
546
|
-
const [, json] = this.#convert(nonoptional._zod.def.innerType, options2, lazyDepth2);
|
559
|
+
const [, json] = this.#convert(nonoptional._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
547
560
|
return [true, json];
|
548
561
|
}
|
549
562
|
case "success": {
|
@@ -552,7 +565,7 @@ class experimental_ZodToJsonSchemaConverter {
|
|
552
565
|
case "default":
|
553
566
|
case "prefault": {
|
554
567
|
const default_ = schema2;
|
555
|
-
const [, json] = this.#convert(default_._zod.def.innerType, options2, lazyDepth2);
|
568
|
+
const [, json] = this.#convert(default_._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
556
569
|
return [false, {
|
557
570
|
...json,
|
558
571
|
default: default_._zod.def.defaultValue
|
@@ -560,18 +573,18 @@ class experimental_ZodToJsonSchemaConverter {
|
|
560
573
|
}
|
561
574
|
case "catch": {
|
562
575
|
const catch_ = schema2;
|
563
|
-
return this.#convert(catch_._zod.def.innerType, options2, lazyDepth2);
|
576
|
+
return this.#convert(catch_._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
564
577
|
}
|
565
578
|
case "nan": {
|
566
579
|
return [true, options2.strategy === "input" ? this.unsupportedJsonSchema : { type: "null" }];
|
567
580
|
}
|
568
581
|
case "pipe": {
|
569
582
|
const pipe = schema2;
|
570
|
-
return this.#convert(options2.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out, options2, lazyDepth2);
|
583
|
+
return this.#convert(options2.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out, options2, lazyDepth2, structureDepth);
|
571
584
|
}
|
572
585
|
case "readonly": {
|
573
586
|
const readonly_ = schema2;
|
574
|
-
const [required, json] = this.#convert(readonly_._zod.def.innerType, options2, lazyDepth2);
|
587
|
+
const [required, json] = this.#convert(readonly_._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
575
588
|
return [required, { ...json, readOnly: true }];
|
576
589
|
}
|
577
590
|
case "template_literal": {
|
@@ -583,15 +596,16 @@ class experimental_ZodToJsonSchemaConverter {
|
|
583
596
|
}
|
584
597
|
case "optional": {
|
585
598
|
const optional = schema2;
|
586
|
-
const [, json] = this.#convert(optional._zod.def.innerType, options2, lazyDepth2);
|
599
|
+
const [, json] = this.#convert(optional._zod.def.innerType, options2, lazyDepth2, structureDepth);
|
587
600
|
return [false, json];
|
588
601
|
}
|
589
602
|
case "lazy": {
|
590
603
|
const lazy = schema2;
|
591
|
-
|
604
|
+
const currentLazyDepth = lazyDepth2 + 1;
|
605
|
+
if (currentLazyDepth > this.maxLazyDepth) {
|
592
606
|
return [false, this.anyJsonSchema];
|
593
607
|
}
|
594
|
-
return this.#convert(lazy._zod.def.getter(), options2,
|
608
|
+
return this.#convert(lazy._zod.def.getter(), options2, currentLazyDepth, structureDepth);
|
595
609
|
}
|
596
610
|
default: {
|
597
611
|
schema2._zod.def.type;
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/zod",
|
3
3
|
"type": "module",
|
4
|
-
"version": "1.5.
|
4
|
+
"version": "1.5.1",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -30,17 +30,17 @@
|
|
30
30
|
],
|
31
31
|
"peerDependencies": {
|
32
32
|
"zod": ">=3.24.2",
|
33
|
-
"@orpc/contract": "1.5.
|
34
|
-
"@orpc/server": "1.5.
|
33
|
+
"@orpc/contract": "1.5.1",
|
34
|
+
"@orpc/server": "1.5.1"
|
35
35
|
},
|
36
36
|
"dependencies": {
|
37
37
|
"escape-string-regexp": "^5.0.0",
|
38
38
|
"wildcard-match": "^5.1.3",
|
39
|
-
"@orpc/openapi": "1.5.
|
40
|
-
"@orpc/shared": "1.5.
|
39
|
+
"@orpc/openapi": "1.5.1",
|
40
|
+
"@orpc/shared": "1.5.1"
|
41
41
|
},
|
42
42
|
"devDependencies": {
|
43
|
-
"zod": "^3.25.
|
43
|
+
"zod": "^3.25.63",
|
44
44
|
"zod-to-json-schema": "^3.24.5"
|
45
45
|
},
|
46
46
|
"scripts": {
|