zod 4.2.0-canary.20251207T223211 → 4.2.0-canary.20251213T203150

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.
Files changed (39) hide show
  1. package/package.json +1 -1
  2. package/src/v4/classic/schemas.ts +97 -5
  3. package/src/v4/classic/tests/json.test.ts +4 -3
  4. package/src/v4/classic/tests/standard-schema.test.ts +77 -0
  5. package/src/v4/classic/tests/to-json-schema-methods.test.ts +438 -0
  6. package/src/v4/classic/tests/to-json-schema.test.ts +66 -30
  7. package/src/v4/core/index.ts +2 -0
  8. package/src/v4/core/json-schema-generator.ts +124 -0
  9. package/src/v4/core/json-schema-processors.ts +630 -0
  10. package/src/v4/core/schemas.ts +8 -13
  11. package/src/v4/core/standard-schema.ts +114 -19
  12. package/src/v4/core/to-json-schema.ts +373 -827
  13. package/src/v4/mini/tests/standard-schema.test.ts +17 -0
  14. package/v4/classic/schemas.cjs +48 -0
  15. package/v4/classic/schemas.d.cts +35 -0
  16. package/v4/classic/schemas.d.ts +35 -0
  17. package/v4/classic/schemas.js +48 -0
  18. package/v4/core/index.cjs +5 -1
  19. package/v4/core/index.d.cts +2 -0
  20. package/v4/core/index.d.ts +2 -0
  21. package/v4/core/index.js +2 -0
  22. package/v4/core/json-schema-generator.cjs +99 -0
  23. package/v4/core/json-schema-generator.d.cts +64 -0
  24. package/v4/core/json-schema-generator.d.ts +64 -0
  25. package/v4/core/json-schema-generator.js +95 -0
  26. package/v4/core/json-schema-processors.cjs +617 -0
  27. package/v4/core/json-schema-processors.d.cts +49 -0
  28. package/v4/core/json-schema-processors.d.ts +49 -0
  29. package/v4/core/json-schema-processors.js +574 -0
  30. package/v4/core/schemas.cjs +0 -10
  31. package/v4/core/schemas.d.cts +4 -1
  32. package/v4/core/schemas.d.ts +4 -1
  33. package/v4/core/schemas.js +0 -10
  34. package/v4/core/standard-schema.d.cts +90 -19
  35. package/v4/core/standard-schema.d.ts +90 -19
  36. package/v4/core/to-json-schema.cjs +302 -793
  37. package/v4/core/to-json-schema.d.cts +56 -33
  38. package/v4/core/to-json-schema.d.ts +56 -33
  39. package/v4/core/to-json-schema.js +296 -791
@@ -1,16 +1,20 @@
1
+ import type * as core from "../core/index.cjs";
1
2
  import type * as JSONSchema from "./json-schema.cjs";
2
- import { $ZodRegistry } from "./registries.cjs";
3
+ import { type $ZodRegistry } from "./registries.cjs";
3
4
  import type * as schemas from "./schemas.cjs";
4
- interface JSONSchemaGeneratorParams {
5
+ import type { StandardJSONSchemaV1, StandardSchemaWithJSONProps } from "./standard-schema.cjs";
6
+ export type Processor<T extends schemas.$ZodType = schemas.$ZodType> = (schema: T, ctx: ToJSONSchemaContext, json: JSONSchema.BaseSchema, params: ProcessParams) => void;
7
+ export interface JSONSchemaGeneratorParams {
8
+ processors: Record<string, Processor>;
5
9
  /** A registry used to look up metadata for each schema. Any schema with an `id` property will be extracted as a $def.
6
10
  * @default globalRegistry */
7
11
  metadata?: $ZodRegistry<Record<string, any>>;
8
12
  /** The JSON Schema version to target.
9
13
  * - `"draft-2020-12"` — Default. JSON Schema Draft 2020-12
10
- * - `"draft-7"` — JSON Schema Draft 7
11
- * - `"draft-4"` — JSON Schema Draft 4
14
+ * - `"draft-07"` — JSON Schema Draft 7
15
+ * - `"draft-04"` — JSON Schema Draft 4
12
16
  * - `"openapi-3.0"` — OpenAPI 3.0 Schema Object */
13
- target?: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0";
17
+ target?: "draft-04" | "draft-07" | "draft-2020-12" | "openapi-3.0" | ({} & string) | undefined;
14
18
  /** How to handle unrepresentable types.
15
19
  * - `"throw"` — Default. Unrepresentable types throw an error
16
20
  * - `"any"` — Unrepresentable types become `{}` */
@@ -21,23 +25,13 @@ interface JSONSchemaGeneratorParams {
21
25
  jsonSchema: JSONSchema.BaseSchema;
22
26
  path: (string | number)[];
23
27
  }) => void;
24
- /** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, Error converting schema to JSONz, defaults, coerced primitives, etc.
28
+ /** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, defaults, coerced primitives, etc.
25
29
  * - `"output"` — Default. Convert the output schema.
26
30
  * - `"input"` — Convert the input schema. */
27
31
  io?: "input" | "output";
28
- }
29
- interface ProcessParams {
30
- schemaPath: schemas.$ZodType[];
31
- path: (string | number)[];
32
- }
33
- interface EmitParams {
34
- /** How to handle cycles.
35
- * - `"ref"` — Default. Cycles will be broken using $defs
36
- * - `"throw"` — Cycles will throw an error if encountered */
37
32
  cycles?: "ref" | "throw";
38
33
  reused?: "ref" | "inline";
39
34
  external?: {
40
- /** */
41
35
  registry: $ZodRegistry<{
42
36
  id?: string | undefined;
43
37
  }>;
@@ -45,7 +39,21 @@ interface EmitParams {
45
39
  defs: Record<string, JSONSchema.BaseSchema>;
46
40
  } | undefined;
47
41
  }
48
- interface Seen {
42
+ /**
43
+ * Parameters for the toJSONSchema function.
44
+ */
45
+ export type ToJSONSchemaParams = Omit<JSONSchemaGeneratorParams, "processors" | "external">;
46
+ /**
47
+ * Parameters for the toJSONSchema function when passing a registry.
48
+ */
49
+ export interface RegistryToJSONSchemaParams extends ToJSONSchemaParams {
50
+ uri?: (id: string) => string;
51
+ }
52
+ export interface ProcessParams {
53
+ schemaPath: schemas.$ZodType[];
54
+ path: (string | number)[];
55
+ }
56
+ export interface Seen {
49
57
  /** JSON Schema result for this Zod schema */
50
58
  schema: JSONSchema.BaseSchema;
51
59
  /** A cached version of the schema that doesn't get overwritten during ref resolution */
@@ -60,31 +68,46 @@ interface Seen {
60
68
  /** JSON Schema property path for this schema */
61
69
  path?: (string | number)[] | undefined;
62
70
  }
63
- export declare class JSONSchemaGenerator {
71
+ export interface ToJSONSchemaContext {
72
+ processors: Record<string, Processor>;
64
73
  metadataRegistry: $ZodRegistry<Record<string, any>>;
65
- target: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0";
74
+ target: "draft-04" | "draft-07" | "draft-2020-12" | "openapi-3.0" | ({} & string);
66
75
  unrepresentable: "throw" | "any";
67
76
  override: (ctx: {
68
- zodSchema: schemas.$ZodTypes;
77
+ zodSchema: schemas.$ZodType;
69
78
  jsonSchema: JSONSchema.BaseSchema;
70
79
  path: (string | number)[];
71
80
  }) => void;
72
81
  io: "input" | "output";
73
82
  counter: number;
74
83
  seen: Map<schemas.$ZodType, Seen>;
75
- constructor(params?: JSONSchemaGeneratorParams);
76
- process(schema: schemas.$ZodType, _params?: ProcessParams): JSONSchema.BaseSchema;
77
- emit(schema: schemas.$ZodType, _params?: EmitParams): JSONSchema.BaseSchema;
78
- }
79
- interface ToJSONSchemaParams extends Omit<JSONSchemaGeneratorParams & EmitParams, "external"> {
84
+ cycles: "ref" | "throw";
85
+ reused: "ref" | "inline";
86
+ external?: {
87
+ registry: $ZodRegistry<{
88
+ id?: string | undefined;
89
+ }>;
90
+ uri?: ((id: string) => string) | undefined;
91
+ defs: Record<string, JSONSchema.BaseSchema>;
92
+ } | undefined;
80
93
  }
81
- interface RegistryToJSONSchemaParams extends Omit<JSONSchemaGeneratorParams & EmitParams, "external"> {
82
- uri?: (id: string) => string;
94
+ export declare function initializeContext(params: JSONSchemaGeneratorParams): ToJSONSchemaContext;
95
+ export declare function process<T extends schemas.$ZodType>(schema: T, ctx: ToJSONSchemaContext, _params?: ProcessParams): JSONSchema.BaseSchema;
96
+ export declare function extractDefs<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): void;
97
+ export declare function finalize<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): ZodStandardJSONSchemaPayload<T>;
98
+ export type ZodStandardSchemaWithJSON<T> = StandardSchemaWithJSONProps<core.input<T>, core.output<T>>;
99
+ export interface ZodStandardJSONSchemaPayload<T> extends JSONSchema.BaseSchema {
100
+ "~standard": ZodStandardSchemaWithJSON<T>;
83
101
  }
84
- export declare function toJSONSchema(schema: schemas.$ZodType, _params?: ToJSONSchemaParams): JSONSchema.BaseSchema;
85
- export declare function toJSONSchema(registry: $ZodRegistry<{
86
- id?: string | undefined;
87
- }>, _params?: RegistryToJSONSchemaParams): {
88
- schemas: Record<string, JSONSchema.BaseSchema>;
89
- };
102
+ /**
103
+ * Creates a toJSONSchema method for a schema instance.
104
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
105
+ */
106
+ export declare const createToJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, processors?: Record<string, Processor>) => (params?: ToJSONSchemaParams) => ZodStandardJSONSchemaPayload<T>;
107
+ /**
108
+ * Creates a toJSONSchema method for a schema instance.
109
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
110
+ */
111
+ type StandardJSONSchemaMethodParams = Parameters<StandardJSONSchemaV1["~standard"]["jsonSchema"]["input"]>[0];
112
+ export declare const createStandardJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, io: "input" | "output") => (params?: StandardJSONSchemaMethodParams) => JSONSchema.BaseSchema;
90
113
  export {};
@@ -1,16 +1,20 @@
1
+ import type * as core from "../core/index.js";
1
2
  import type * as JSONSchema from "./json-schema.js";
2
- import { $ZodRegistry } from "./registries.js";
3
+ import { type $ZodRegistry } from "./registries.js";
3
4
  import type * as schemas from "./schemas.js";
4
- interface JSONSchemaGeneratorParams {
5
+ import type { StandardJSONSchemaV1, StandardSchemaWithJSONProps } from "./standard-schema.js";
6
+ export type Processor<T extends schemas.$ZodType = schemas.$ZodType> = (schema: T, ctx: ToJSONSchemaContext, json: JSONSchema.BaseSchema, params: ProcessParams) => void;
7
+ export interface JSONSchemaGeneratorParams {
8
+ processors: Record<string, Processor>;
5
9
  /** A registry used to look up metadata for each schema. Any schema with an `id` property will be extracted as a $def.
6
10
  * @default globalRegistry */
7
11
  metadata?: $ZodRegistry<Record<string, any>>;
8
12
  /** The JSON Schema version to target.
9
13
  * - `"draft-2020-12"` — Default. JSON Schema Draft 2020-12
10
- * - `"draft-7"` — JSON Schema Draft 7
11
- * - `"draft-4"` — JSON Schema Draft 4
14
+ * - `"draft-07"` — JSON Schema Draft 7
15
+ * - `"draft-04"` — JSON Schema Draft 4
12
16
  * - `"openapi-3.0"` — OpenAPI 3.0 Schema Object */
13
- target?: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0";
17
+ target?: "draft-04" | "draft-07" | "draft-2020-12" | "openapi-3.0" | ({} & string) | undefined;
14
18
  /** How to handle unrepresentable types.
15
19
  * - `"throw"` — Default. Unrepresentable types throw an error
16
20
  * - `"any"` — Unrepresentable types become `{}` */
@@ -21,23 +25,13 @@ interface JSONSchemaGeneratorParams {
21
25
  jsonSchema: JSONSchema.BaseSchema;
22
26
  path: (string | number)[];
23
27
  }) => void;
24
- /** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, Error converting schema to JSONz, defaults, coerced primitives, etc.
28
+ /** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, defaults, coerced primitives, etc.
25
29
  * - `"output"` — Default. Convert the output schema.
26
30
  * - `"input"` — Convert the input schema. */
27
31
  io?: "input" | "output";
28
- }
29
- interface ProcessParams {
30
- schemaPath: schemas.$ZodType[];
31
- path: (string | number)[];
32
- }
33
- interface EmitParams {
34
- /** How to handle cycles.
35
- * - `"ref"` — Default. Cycles will be broken using $defs
36
- * - `"throw"` — Cycles will throw an error if encountered */
37
32
  cycles?: "ref" | "throw";
38
33
  reused?: "ref" | "inline";
39
34
  external?: {
40
- /** */
41
35
  registry: $ZodRegistry<{
42
36
  id?: string | undefined;
43
37
  }>;
@@ -45,7 +39,21 @@ interface EmitParams {
45
39
  defs: Record<string, JSONSchema.BaseSchema>;
46
40
  } | undefined;
47
41
  }
48
- interface Seen {
42
+ /**
43
+ * Parameters for the toJSONSchema function.
44
+ */
45
+ export type ToJSONSchemaParams = Omit<JSONSchemaGeneratorParams, "processors" | "external">;
46
+ /**
47
+ * Parameters for the toJSONSchema function when passing a registry.
48
+ */
49
+ export interface RegistryToJSONSchemaParams extends ToJSONSchemaParams {
50
+ uri?: (id: string) => string;
51
+ }
52
+ export interface ProcessParams {
53
+ schemaPath: schemas.$ZodType[];
54
+ path: (string | number)[];
55
+ }
56
+ export interface Seen {
49
57
  /** JSON Schema result for this Zod schema */
50
58
  schema: JSONSchema.BaseSchema;
51
59
  /** A cached version of the schema that doesn't get overwritten during ref resolution */
@@ -60,31 +68,46 @@ interface Seen {
60
68
  /** JSON Schema property path for this schema */
61
69
  path?: (string | number)[] | undefined;
62
70
  }
63
- export declare class JSONSchemaGenerator {
71
+ export interface ToJSONSchemaContext {
72
+ processors: Record<string, Processor>;
64
73
  metadataRegistry: $ZodRegistry<Record<string, any>>;
65
- target: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0";
74
+ target: "draft-04" | "draft-07" | "draft-2020-12" | "openapi-3.0" | ({} & string);
66
75
  unrepresentable: "throw" | "any";
67
76
  override: (ctx: {
68
- zodSchema: schemas.$ZodTypes;
77
+ zodSchema: schemas.$ZodType;
69
78
  jsonSchema: JSONSchema.BaseSchema;
70
79
  path: (string | number)[];
71
80
  }) => void;
72
81
  io: "input" | "output";
73
82
  counter: number;
74
83
  seen: Map<schemas.$ZodType, Seen>;
75
- constructor(params?: JSONSchemaGeneratorParams);
76
- process(schema: schemas.$ZodType, _params?: ProcessParams): JSONSchema.BaseSchema;
77
- emit(schema: schemas.$ZodType, _params?: EmitParams): JSONSchema.BaseSchema;
78
- }
79
- interface ToJSONSchemaParams extends Omit<JSONSchemaGeneratorParams & EmitParams, "external"> {
84
+ cycles: "ref" | "throw";
85
+ reused: "ref" | "inline";
86
+ external?: {
87
+ registry: $ZodRegistry<{
88
+ id?: string | undefined;
89
+ }>;
90
+ uri?: ((id: string) => string) | undefined;
91
+ defs: Record<string, JSONSchema.BaseSchema>;
92
+ } | undefined;
80
93
  }
81
- interface RegistryToJSONSchemaParams extends Omit<JSONSchemaGeneratorParams & EmitParams, "external"> {
82
- uri?: (id: string) => string;
94
+ export declare function initializeContext(params: JSONSchemaGeneratorParams): ToJSONSchemaContext;
95
+ export declare function process<T extends schemas.$ZodType>(schema: T, ctx: ToJSONSchemaContext, _params?: ProcessParams): JSONSchema.BaseSchema;
96
+ export declare function extractDefs<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): void;
97
+ export declare function finalize<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): ZodStandardJSONSchemaPayload<T>;
98
+ export type ZodStandardSchemaWithJSON<T> = StandardSchemaWithJSONProps<core.input<T>, core.output<T>>;
99
+ export interface ZodStandardJSONSchemaPayload<T> extends JSONSchema.BaseSchema {
100
+ "~standard": ZodStandardSchemaWithJSON<T>;
83
101
  }
84
- export declare function toJSONSchema(schema: schemas.$ZodType, _params?: ToJSONSchemaParams): JSONSchema.BaseSchema;
85
- export declare function toJSONSchema(registry: $ZodRegistry<{
86
- id?: string | undefined;
87
- }>, _params?: RegistryToJSONSchemaParams): {
88
- schemas: Record<string, JSONSchema.BaseSchema>;
89
- };
102
+ /**
103
+ * Creates a toJSONSchema method for a schema instance.
104
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
105
+ */
106
+ export declare const createToJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, processors?: Record<string, Processor>) => (params?: ToJSONSchemaParams) => ZodStandardJSONSchemaPayload<T>;
107
+ /**
108
+ * Creates a toJSONSchema method for a schema instance.
109
+ * This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
110
+ */
111
+ type StandardJSONSchemaMethodParams = Parameters<StandardJSONSchemaV1["~standard"]["jsonSchema"]["input"]>[0];
112
+ export declare const createStandardJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, io: "input" | "output") => (params?: StandardJSONSchemaMethodParams) => JSONSchema.BaseSchema;
90
113
  export {};