@orpc/zod 2.0.0-beta.49 → 2.0.0-beta.50
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/converter.d.ts +22 -22
- package/dist/converter.js +104 -120
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -2
- package/dist/registries.d.ts +3 -3
- package/dist/registries.js +60 -61
- package/package.json +2 -2
package/dist/converter.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
export interface ZodToJsonSchemaConverterOptions extends Omit<ToJSONSchemaParams,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { AnySchema, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from "@orpc/json-schema";
|
|
2
|
+
import { ToJSONSchemaParams } from "zod/v4/core";
|
|
3
|
+
export interface ZodToJsonSchemaConverterOptions extends Omit<ToJSONSchemaParams, "target" | "io"> {
|
|
4
|
+
/**
|
|
5
|
+
* Caches conversion results in a WeakMap keyed by the Zod schema instance,
|
|
6
|
+
* so converting the same schema again is free.
|
|
7
|
+
*
|
|
8
|
+
* When enabled, repeated conversions return the same JSON schema object,
|
|
9
|
+
* so treat returned schemas as immutable.
|
|
10
|
+
*
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
cache?: boolean | undefined;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Converts Zod schemas into JSON Schema using Zod's built-in `toJSONSchema`,
|
|
@@ -19,12 +19,12 @@ export interface ZodToJsonSchemaConverterOptions extends Omit<ToJSONSchemaParams
|
|
|
19
19
|
* @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
|
|
20
20
|
*/
|
|
21
21
|
export declare class ZodToJsonSchemaConverter implements JsonSchemaConverter {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
22
|
+
private readonly toJSONSchemaParams;
|
|
23
|
+
private readonly cache;
|
|
24
|
+
constructor({ cache, ...toJSONSchemaParams }?: ZodToJsonSchemaConverterOptions);
|
|
25
|
+
condition(schema: AnySchema | undefined, _direction: JsonSchemaConverterDirection): boolean;
|
|
26
|
+
convert(schema: AnySchema | undefined, direction: JsonSchemaConverterDirection): [jsonSchema: JsonSchema, optional: boolean];
|
|
27
|
+
private convertUncached;
|
|
28
|
+
private convertZod;
|
|
29
|
+
private getCustomJsonSchema;
|
|
30
|
+
}
|
package/dist/converter.js
CHANGED
|
@@ -1,123 +1,107 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY } from "./registries.js";
|
|
2
|
+
import { JsonSchemaFormat, JsonSchemaXNativeType, isStandardSchemaOptional } from "@orpc/json-schema";
|
|
3
|
+
import { process, toJSONSchema } from "zod/v4/core";
|
|
4
|
+
// src/converter.ts
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
getCustomJsonSchema(schema, direction) {
|
|
64
|
-
const general = JSON_SCHEMA_REGISTRY.get(schema);
|
|
65
|
-
const directional = direction === 'input'
|
|
66
|
-
? JSON_SCHEMA_INPUT_REGISTRY.get(schema)
|
|
67
|
-
: JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
|
|
68
|
-
if (general === undefined && directional === undefined) {
|
|
69
|
-
return undefined;
|
|
70
|
-
}
|
|
71
|
-
return { ...general, ...directional };
|
|
72
|
-
}
|
|
73
|
-
}
|
|
6
|
+
* Converts Zod schemas into JSON Schema using Zod's built-in `toJSONSchema`,
|
|
7
|
+
* with additional support for types such as `z.bigint()`, `z.date()`, `z.set()`, and `z.map()`.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
|
|
10
|
+
*/
|
|
11
|
+
var ZodToJsonSchemaConverter = class {
|
|
12
|
+
toJSONSchemaParams;
|
|
13
|
+
cache;
|
|
14
|
+
constructor({ cache, ...toJSONSchemaParams } = {}) {
|
|
15
|
+
this.toJSONSchemaParams = toJSONSchemaParams;
|
|
16
|
+
if (cache) this.cache = {
|
|
17
|
+
input: /* @__PURE__ */ new WeakMap(),
|
|
18
|
+
output: /* @__PURE__ */ new WeakMap()
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
condition(schema, _direction) {
|
|
22
|
+
return schema?.["~standard"].vendor === "zod";
|
|
23
|
+
}
|
|
24
|
+
convert(schema, direction) {
|
|
25
|
+
const zodSchema = schema;
|
|
26
|
+
if (this.cache) {
|
|
27
|
+
const cached = this.cache[direction].get(zodSchema);
|
|
28
|
+
if (cached) return cached;
|
|
29
|
+
}
|
|
30
|
+
const result = this.convertUncached(zodSchema, direction);
|
|
31
|
+
if (this.cache) this.cache[direction].set(zodSchema, result);
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
convertUncached(zodSchema, direction) {
|
|
35
|
+
return [this.convertZod(zodSchema, direction), isStandardSchemaOptional(zodSchema, direction)];
|
|
36
|
+
}
|
|
37
|
+
convertZod(schema, direction) {
|
|
38
|
+
const { unrepresentable = "any" } = this.toJSONSchemaParams;
|
|
39
|
+
const { $schema: _schema, ...rest } = toJSONSchema(schema, {
|
|
40
|
+
...this.toJSONSchemaParams,
|
|
41
|
+
target: "draft-2020-12",
|
|
42
|
+
io: direction,
|
|
43
|
+
unrepresentable(info) {
|
|
44
|
+
return convertNativeType(this, info.zodSchema, info.path) ?? (typeof unrepresentable === "function" ? unrepresentable(info) : unrepresentable);
|
|
45
|
+
},
|
|
46
|
+
override: (ctx) => {
|
|
47
|
+
const customJsonSchema = this.getCustomJsonSchema(ctx.zodSchema, direction);
|
|
48
|
+
if (customJsonSchema) Object.assign(ctx.jsonSchema, customJsonSchema);
|
|
49
|
+
this.toJSONSchemaParams.override?.(ctx);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return rest;
|
|
53
|
+
}
|
|
54
|
+
getCustomJsonSchema(schema, direction) {
|
|
55
|
+
const general = JSON_SCHEMA_REGISTRY.get(schema);
|
|
56
|
+
const directional = direction === "input" ? JSON_SCHEMA_INPUT_REGISTRY.get(schema) : JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
|
|
57
|
+
if (general === void 0 && directional === void 0) return;
|
|
58
|
+
return {
|
|
59
|
+
...general,
|
|
60
|
+
...directional
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
};
|
|
74
64
|
function convertNativeType(ctx, zodSchema, path) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
minItems: 2,
|
|
117
|
-
},
|
|
118
|
-
'x-native-type': JsonSchemaXNativeType.Map,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return undefined;
|
|
65
|
+
const def = zodSchema._zod.def;
|
|
66
|
+
switch (def.type) {
|
|
67
|
+
case "bigint": return {
|
|
68
|
+
"type": "string",
|
|
69
|
+
"pattern": "^-?[0-9]+$",
|
|
70
|
+
"x-native-type": JsonSchemaXNativeType.BigInt
|
|
71
|
+
};
|
|
72
|
+
case "date": return {
|
|
73
|
+
"type": "string",
|
|
74
|
+
"format": JsonSchemaFormat.DateTime,
|
|
75
|
+
"x-native-type": JsonSchemaXNativeType.Date
|
|
76
|
+
};
|
|
77
|
+
case "set":
|
|
78
|
+
case "map": {
|
|
79
|
+
const schemaPath = [];
|
|
80
|
+
for (const [seenSchema, seen] of ctx.seen) if (seen.path && seen.path.length <= path.length && seen.path.every((segment, i) => segment === path[i])) schemaPath.push(seenSchema);
|
|
81
|
+
const processItems = (schema, ...segments) => process(schema, ctx, {
|
|
82
|
+
path: [
|
|
83
|
+
...path,
|
|
84
|
+
"items",
|
|
85
|
+
...segments
|
|
86
|
+
],
|
|
87
|
+
schemaPath
|
|
88
|
+
});
|
|
89
|
+
return def.type === "set" ? {
|
|
90
|
+
"type": "array",
|
|
91
|
+
"uniqueItems": true,
|
|
92
|
+
"items": processItems(def.valueType),
|
|
93
|
+
"x-native-type": JsonSchemaXNativeType.Set
|
|
94
|
+
} : {
|
|
95
|
+
"type": "array",
|
|
96
|
+
"items": {
|
|
97
|
+
type: "array",
|
|
98
|
+
prefixItems: [processItems(def.keyType, "prefixItems", 0), processItems(def.valueType, "prefixItems", 1)],
|
|
99
|
+
maxItems: 2,
|
|
100
|
+
minItems: 2
|
|
101
|
+
},
|
|
102
|
+
"x-native-type": JsonSchemaXNativeType.Map
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
123
106
|
}
|
|
107
|
+
export { ZodToJsonSchemaConverter };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { ZodToJsonSchemaConverter, ZodToJsonSchemaConverterOptions } from "./converter.js";
|
|
2
|
+
import { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY } from "./registries.js";
|
|
3
|
+
import { JsonSchema } from "@orpc/json-schema";
|
|
4
|
+
export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY, type JsonSchema, ZodToJsonSchemaConverter, ZodToJsonSchemaConverterOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { ZodToJsonSchemaConverter } from "./converter.js";
|
|
2
|
+
export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY } from "./registries.js";
|
package/dist/registries.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { JsonSchema } from "@orpc/json-schema";
|
|
2
|
+
import { $ZodRegistry, $input, $output } from "zod/v4/core";
|
|
3
3
|
/**
|
|
4
4
|
* Zod registry for customizing generated JSON schema, can use both for .input and .output
|
|
5
5
|
*
|
|
@@ -59,4 +59,4 @@ export declare const JSON_SCHEMA_INPUT_REGISTRY: $ZodRegistry<Exclude<JsonSchema
|
|
|
59
59
|
*
|
|
60
60
|
* @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
|
|
61
61
|
*/
|
|
62
|
-
export declare const JSON_SCHEMA_OUTPUT_REGISTRY: $ZodRegistry<Exclude<JsonSchema<$output>, boolean>>;
|
|
62
|
+
export declare const JSON_SCHEMA_OUTPUT_REGISTRY: $ZodRegistry<Exclude<JsonSchema<$output>, boolean>>;
|
package/dist/registries.js
CHANGED
|
@@ -1,64 +1,63 @@
|
|
|
1
|
-
import { registry } from
|
|
1
|
+
import { registry } from "zod/v4/core";
|
|
2
|
+
// src/registries.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/* @__PURE__ */ registry();
|
|
4
|
+
* Zod registry for customizing generated JSON schema, can use both for .input and .output
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { JSON_SCHEMA_REGISTRY } from '@orpc/zod'
|
|
9
|
+
*
|
|
10
|
+
* const user = z.object({
|
|
11
|
+
* name: z.string(),
|
|
12
|
+
* age: z.number(),
|
|
13
|
+
* })
|
|
14
|
+
*
|
|
15
|
+
* JSON_SCHEMA_REGISTRY.add(user, {
|
|
16
|
+
* examples: [{ name: 'John', age: 20 }],
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
|
|
21
|
+
*/
|
|
22
|
+
const JSON_SCHEMA_REGISTRY = /* @__PURE__ */ registry();
|
|
23
23
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/* @__PURE__ */ registry();
|
|
24
|
+
* Zod registry for customizing generated JSON schema, only useful for .input
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { JSON_SCHEMA_INPUT_REGISTRY } from '@orpc/zod'
|
|
29
|
+
*
|
|
30
|
+
* const user = z.object({
|
|
31
|
+
* name: z.string(),
|
|
32
|
+
* age: z.string().transform(v => Number(v)),
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* JSON_SCHEMA_INPUT_REGISTRY.add(user, {
|
|
36
|
+
* examples: [{ name: 'John', age: "20" }],
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
|
|
41
|
+
*/
|
|
42
|
+
const JSON_SCHEMA_INPUT_REGISTRY = /* @__PURE__ */ registry();
|
|
44
43
|
/**
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
44
|
+
* Zod registry for customizing generated JSON schema, only useful for .output
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { JSON_SCHEMA_OUTPUT_REGISTRY } from '@orpc/zod'
|
|
49
|
+
*
|
|
50
|
+
* const user = z.object({
|
|
51
|
+
* name: z.string(),
|
|
52
|
+
* age: z.string().transform(v => Number(v)),
|
|
53
|
+
* })
|
|
54
|
+
*
|
|
55
|
+
* JSON_SCHEMA_OUTPUT_REGISTRY.add(user, {
|
|
56
|
+
* examples: [{ name: 'John', age: 20 }],
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
|
|
61
|
+
*/
|
|
62
|
+
const JSON_SCHEMA_OUTPUT_REGISTRY = /* @__PURE__ */ registry();
|
|
63
|
+
export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/zod",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
4
4
|
"description": "Zod integration for oRPC: convert Zod schemas into JSON Schema for OpenAPI generation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"json-schema",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@orpc/json-schema": "2.0.0-beta.
|
|
39
|
+
"@orpc/json-schema": "2.0.0-beta.50"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"zod": "^4.6.7"
|