@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.
@@ -1,16 +1,16 @@
1
- import type { AnySchema, JsonSchema, JsonSchemaConverter, JsonSchemaConverterDirection } from '@orpc/json-schema';
2
- import type { 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;
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
- 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
- }
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 { isStandardSchemaOptional, JsonSchemaFormat, JsonSchemaXNativeType, } from '@orpc/json-schema';
2
- import { process as processZodSchema, toJSONSchema } from 'zod/v4/core';
3
- import { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY, } from './registries.js';
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
- * Converts Zod schemas into JSON Schema using Zod's built-in `toJSONSchema`,
6
- * with additional support for types such as `z.bigint()`, `z.date()`, `z.set()`, and `z.map()`.
7
- *
8
- * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
9
- */
10
- export class ZodToJsonSchemaConverter {
11
- toJSONSchemaParams;
12
- cache;
13
- constructor({ cache, ...toJSONSchemaParams } = {}) {
14
- this.toJSONSchemaParams = toJSONSchemaParams;
15
- if (cache) {
16
- this.cache = { input: new WeakMap(), output: new WeakMap() };
17
- }
18
- }
19
- condition(schema, _direction) {
20
- return schema?.['~standard'].vendor === 'zod';
21
- }
22
- convert(schema, direction) {
23
- const zodSchema = schema;
24
- if (this.cache) {
25
- const cached = this.cache[direction].get(zodSchema);
26
- if (cached) {
27
- return cached;
28
- }
29
- }
30
- const result = this.convertUncached(zodSchema, direction);
31
- if (this.cache) {
32
- this.cache[direction].set(zodSchema, result);
33
- }
34
- return result;
35
- }
36
- convertUncached(zodSchema, direction) {
37
- const jsonSchema = this.convertZod(zodSchema, direction);
38
- return [jsonSchema, isStandardSchemaOptional(zodSchema, direction)];
39
- }
40
- convertZod(schema, direction) {
41
- const { unrepresentable = 'any' } = this.toJSONSchemaParams;
42
- const jsonSchema = toJSONSchema(schema, {
43
- ...this.toJSONSchemaParams,
44
- target: 'draft-2020-12',
45
- io: direction,
46
- unrepresentable(info) {
47
- return (convertNativeType(this, info.zodSchema, info.path) ??
48
- (typeof unrepresentable === 'function' ? unrepresentable(info) : unrepresentable));
49
- },
50
- override: (ctx) => {
51
- const customJsonSchema = this.getCustomJsonSchema(ctx.zodSchema, direction);
52
- if (customJsonSchema) {
53
- Object.assign(ctx.jsonSchema, customJsonSchema);
54
- }
55
- this.toJSONSchemaParams.override?.(ctx);
56
- },
57
- });
58
- // Since the default oRPC format is always draft/2020-12,
59
- // `$schema` can be safely omitted here.
60
- const { $schema: _schema, ...rest } = jsonSchema;
61
- return rest;
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
- const def = zodSchema._zod.def;
76
- switch (def.type) {
77
- case 'bigint':
78
- return {
79
- 'type': 'string',
80
- 'pattern': '^-?[0-9]+$',
81
- 'x-native-type': JsonSchemaXNativeType.BigInt,
82
- };
83
- case 'date':
84
- return {
85
- 'type': 'string',
86
- 'format': JsonSchemaFormat.DateTime,
87
- 'x-native-type': JsonSchemaXNativeType.Date,
88
- };
89
- case 'set':
90
- case 'map': {
91
- const schemaPath = [];
92
- for (const [seenSchema, seen] of ctx.seen) {
93
- if (seen.path &&
94
- seen.path.length <= path.length &&
95
- seen.path.every((segment, i) => segment === path[i])) {
96
- schemaPath.push(seenSchema);
97
- }
98
- }
99
- const processItems = (schema, ...segments) => processZodSchema(schema, ctx, { path: [...path, 'items', ...segments], schemaPath });
100
- return def.type === 'set'
101
- ? {
102
- 'type': 'array',
103
- 'uniqueItems': true,
104
- 'items': processItems(def.valueType),
105
- 'x-native-type': JsonSchemaXNativeType.Set,
106
- }
107
- : {
108
- 'type': 'array',
109
- 'items': {
110
- type: 'array',
111
- prefixItems: [
112
- processItems(def.keyType, 'prefixItems', 0),
113
- processItems(def.valueType, 'prefixItems', 1),
114
- ],
115
- maxItems: 2,
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
- export * from './converter.ts';
2
- export * from './registries.ts';
3
- export type { JsonSchema } from '@orpc/json-schema';
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 * from './converter.js';
2
- export * from './registries.js';
1
+ export { ZodToJsonSchemaConverter } from "./converter.js";
2
+ export { JSON_SCHEMA_INPUT_REGISTRY, JSON_SCHEMA_OUTPUT_REGISTRY, JSON_SCHEMA_REGISTRY } from "./registries.js";
@@ -1,5 +1,5 @@
1
- import type { JsonSchema } from '@orpc/json-schema';
2
- import type { $input, $output, $ZodRegistry } from 'zod/v4/core';
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>>;
@@ -1,64 +1,63 @@
1
- import { registry } from 'zod/v4/core';
1
+ import { registry } from "zod/v4/core";
2
+ // src/registries.ts
2
3
  /**
3
- * Zod registry for customizing generated JSON schema, can use both for .input and .output
4
- *
5
- * @example
6
- * ```ts
7
- * import { JSON_SCHEMA_REGISTRY } from '@orpc/zod'
8
- *
9
- * const user = z.object({
10
- * name: z.string(),
11
- * age: z.number(),
12
- * })
13
- *
14
- * JSON_SCHEMA_REGISTRY.add(user, {
15
- * examples: [{ name: 'John', age: 20 }],
16
- * })
17
- * ```
18
- *
19
- * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
20
- */
21
- export const JSON_SCHEMA_REGISTRY =
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
- * 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
- export const JSON_SCHEMA_INPUT_REGISTRY =
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
- * Zod registry for customizing generated JSON schema, only useful for .output
46
- *
47
- * @example
48
- * ```ts
49
- * import { JSON_SCHEMA_OUTPUT_REGISTRY } from '@orpc/zod'
50
- *
51
- * const user = z.object({
52
- * name: z.string(),
53
- * age: z.string().transform(v => Number(v)),
54
- * })
55
- *
56
- * JSON_SCHEMA_OUTPUT_REGISTRY.add(user, {
57
- * examples: [{ name: 'John', age: 20 }],
58
- * })
59
- * ```
60
- *
61
- * @see {@link https://orpc.dev/docs/integrations/zod | Zod Integration}
62
- */
63
- export const JSON_SCHEMA_OUTPUT_REGISTRY =
64
- /* @__PURE__ */ registry();
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.49",
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.49"
39
+ "@orpc/json-schema": "2.0.0-beta.50"
40
40
  },
41
41
  "devDependencies": {
42
42
  "zod": "^4.6.7"