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.
- package/package.json +1 -1
- package/src/v4/classic/schemas.ts +97 -5
- package/src/v4/classic/tests/json.test.ts +4 -3
- package/src/v4/classic/tests/standard-schema.test.ts +77 -0
- package/src/v4/classic/tests/to-json-schema-methods.test.ts +438 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +66 -30
- package/src/v4/core/index.ts +2 -0
- package/src/v4/core/json-schema-generator.ts +124 -0
- package/src/v4/core/json-schema-processors.ts +630 -0
- package/src/v4/core/schemas.ts +8 -13
- package/src/v4/core/standard-schema.ts +114 -19
- package/src/v4/core/to-json-schema.ts +373 -827
- package/src/v4/mini/tests/standard-schema.test.ts +17 -0
- package/v4/classic/schemas.cjs +48 -0
- package/v4/classic/schemas.d.cts +35 -0
- package/v4/classic/schemas.d.ts +35 -0
- package/v4/classic/schemas.js +48 -0
- package/v4/core/index.cjs +5 -1
- package/v4/core/index.d.cts +2 -0
- package/v4/core/index.d.ts +2 -0
- package/v4/core/index.js +2 -0
- package/v4/core/json-schema-generator.cjs +99 -0
- package/v4/core/json-schema-generator.d.cts +64 -0
- package/v4/core/json-schema-generator.d.ts +64 -0
- package/v4/core/json-schema-generator.js +95 -0
- package/v4/core/json-schema-processors.cjs +617 -0
- package/v4/core/json-schema-processors.d.cts +49 -0
- package/v4/core/json-schema-processors.d.ts +49 -0
- package/v4/core/json-schema-processors.js +574 -0
- package/v4/core/schemas.cjs +0 -10
- package/v4/core/schemas.d.cts +4 -1
- package/v4/core/schemas.d.ts +4 -1
- package/v4/core/schemas.js +0 -10
- package/v4/core/standard-schema.d.cts +90 -19
- package/v4/core/standard-schema.d.ts +90 -19
- package/v4/core/to-json-schema.cjs +302 -793
- package/v4/core/to-json-schema.d.cts +56 -33
- package/v4/core/to-json-schema.d.ts +56 -33
- package/v4/core/to-json-schema.js +296 -791
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JSONSchemaGenerator = void 0;
|
|
4
|
+
const json_schema_processors_js_1 = require("./json-schema-processors.cjs");
|
|
5
|
+
const to_json_schema_js_1 = require("./to-json-schema.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Legacy class-based interface for JSON Schema generation.
|
|
8
|
+
* This class wraps the new functional implementation to provide backward compatibility.
|
|
9
|
+
*
|
|
10
|
+
* @deprecated Use the `toJSONSchema` function instead for new code.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Legacy usage (still supported)
|
|
15
|
+
* const gen = new JSONSchemaGenerator({ target: "draft-07" });
|
|
16
|
+
* gen.process(schema);
|
|
17
|
+
* const result = gen.emit(schema);
|
|
18
|
+
*
|
|
19
|
+
* // Preferred modern usage
|
|
20
|
+
* const result = toJSONSchema(schema, { target: "draft-07" });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
class JSONSchemaGenerator {
|
|
24
|
+
/** @deprecated Access via ctx instead */
|
|
25
|
+
get metadataRegistry() {
|
|
26
|
+
return this.ctx.metadataRegistry;
|
|
27
|
+
}
|
|
28
|
+
/** @deprecated Access via ctx instead */
|
|
29
|
+
get target() {
|
|
30
|
+
return this.ctx.target;
|
|
31
|
+
}
|
|
32
|
+
/** @deprecated Access via ctx instead */
|
|
33
|
+
get unrepresentable() {
|
|
34
|
+
return this.ctx.unrepresentable;
|
|
35
|
+
}
|
|
36
|
+
/** @deprecated Access via ctx instead */
|
|
37
|
+
get override() {
|
|
38
|
+
return this.ctx.override;
|
|
39
|
+
}
|
|
40
|
+
/** @deprecated Access via ctx instead */
|
|
41
|
+
get io() {
|
|
42
|
+
return this.ctx.io;
|
|
43
|
+
}
|
|
44
|
+
/** @deprecated Access via ctx instead */
|
|
45
|
+
get counter() {
|
|
46
|
+
return this.ctx.counter;
|
|
47
|
+
}
|
|
48
|
+
set counter(value) {
|
|
49
|
+
this.ctx.counter = value;
|
|
50
|
+
}
|
|
51
|
+
/** @deprecated Access via ctx instead */
|
|
52
|
+
get seen() {
|
|
53
|
+
return this.ctx.seen;
|
|
54
|
+
}
|
|
55
|
+
constructor(params) {
|
|
56
|
+
// Normalize target for internal context
|
|
57
|
+
let normalizedTarget = params?.target ?? "draft-2020-12";
|
|
58
|
+
if (normalizedTarget === "draft-4")
|
|
59
|
+
normalizedTarget = "draft-04";
|
|
60
|
+
if (normalizedTarget === "draft-7")
|
|
61
|
+
normalizedTarget = "draft-07";
|
|
62
|
+
this.ctx = (0, to_json_schema_js_1.initializeContext)({
|
|
63
|
+
processors: json_schema_processors_js_1.allProcessors,
|
|
64
|
+
target: normalizedTarget,
|
|
65
|
+
...(params?.metadata && { metadata: params.metadata }),
|
|
66
|
+
...(params?.unrepresentable && { unrepresentable: params.unrepresentable }),
|
|
67
|
+
...(params?.override && { override: params.override }),
|
|
68
|
+
...(params?.io && { io: params.io }),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Process a schema to prepare it for JSON Schema generation.
|
|
73
|
+
* This must be called before emit().
|
|
74
|
+
*/
|
|
75
|
+
process(schema, _params = { path: [], schemaPath: [] }) {
|
|
76
|
+
return (0, to_json_schema_js_1.process)(schema, this.ctx, _params);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Emit the final JSON Schema after processing.
|
|
80
|
+
* Must call process() first.
|
|
81
|
+
*/
|
|
82
|
+
emit(schema, _params) {
|
|
83
|
+
// Apply emit params to the context
|
|
84
|
+
if (_params) {
|
|
85
|
+
if (_params.cycles)
|
|
86
|
+
this.ctx.cycles = _params.cycles;
|
|
87
|
+
if (_params.reused)
|
|
88
|
+
this.ctx.reused = _params.reused;
|
|
89
|
+
if (_params.external)
|
|
90
|
+
this.ctx.external = _params.external;
|
|
91
|
+
}
|
|
92
|
+
(0, to_json_schema_js_1.extractDefs)(this.ctx, schema);
|
|
93
|
+
const result = (0, to_json_schema_js_1.finalize)(this.ctx, schema);
|
|
94
|
+
// Strip ~standard property to match old implementation's return type
|
|
95
|
+
const { "~standard": _, ...plainResult } = result;
|
|
96
|
+
return plainResult;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.JSONSchemaGenerator = JSONSchemaGenerator;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type * as JSONSchema from "./json-schema.cjs";
|
|
2
|
+
import type * as schemas from "./schemas.cjs";
|
|
3
|
+
import { type JSONSchemaGeneratorParams, type ProcessParams } from "./to-json-schema.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Parameters for the emit method of JSONSchemaGenerator.
|
|
6
|
+
* @deprecated Use toJSONSchema function instead
|
|
7
|
+
*/
|
|
8
|
+
export type EmitParams = Pick<JSONSchemaGeneratorParams, "cycles" | "reused" | "external">;
|
|
9
|
+
/**
|
|
10
|
+
* Parameters for JSONSchemaGenerator constructor.
|
|
11
|
+
* @deprecated Use toJSONSchema function instead
|
|
12
|
+
*/
|
|
13
|
+
type JSONSchemaGeneratorConstructorParams = Pick<JSONSchemaGeneratorParams, "metadata" | "target" | "unrepresentable" | "override" | "io">;
|
|
14
|
+
/**
|
|
15
|
+
* Legacy class-based interface for JSON Schema generation.
|
|
16
|
+
* This class wraps the new functional implementation to provide backward compatibility.
|
|
17
|
+
*
|
|
18
|
+
* @deprecated Use the `toJSONSchema` function instead for new code.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Legacy usage (still supported)
|
|
23
|
+
* const gen = new JSONSchemaGenerator({ target: "draft-07" });
|
|
24
|
+
* gen.process(schema);
|
|
25
|
+
* const result = gen.emit(schema);
|
|
26
|
+
*
|
|
27
|
+
* // Preferred modern usage
|
|
28
|
+
* const result = toJSONSchema(schema, { target: "draft-07" });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class JSONSchemaGenerator {
|
|
32
|
+
private ctx;
|
|
33
|
+
/** @deprecated Access via ctx instead */
|
|
34
|
+
get metadataRegistry(): import("./registries.js").$ZodRegistry<Record<string, any>, schemas.$ZodType<unknown, unknown, schemas.$ZodTypeInternals<unknown, unknown>>>;
|
|
35
|
+
/** @deprecated Access via ctx instead */
|
|
36
|
+
get target(): ({} & string) | "draft-2020-12" | "draft-07" | "openapi-3.0" | "draft-04";
|
|
37
|
+
/** @deprecated Access via ctx instead */
|
|
38
|
+
get unrepresentable(): "any" | "throw";
|
|
39
|
+
/** @deprecated Access via ctx instead */
|
|
40
|
+
get override(): (ctx: {
|
|
41
|
+
zodSchema: schemas.$ZodType;
|
|
42
|
+
jsonSchema: JSONSchema.BaseSchema;
|
|
43
|
+
path: (string | number)[];
|
|
44
|
+
}) => void;
|
|
45
|
+
/** @deprecated Access via ctx instead */
|
|
46
|
+
get io(): "input" | "output";
|
|
47
|
+
/** @deprecated Access via ctx instead */
|
|
48
|
+
get counter(): number;
|
|
49
|
+
set counter(value: number);
|
|
50
|
+
/** @deprecated Access via ctx instead */
|
|
51
|
+
get seen(): Map<schemas.$ZodType<unknown, unknown, schemas.$ZodTypeInternals<unknown, unknown>>, import("./to-json-schema.js").Seen>;
|
|
52
|
+
constructor(params?: JSONSchemaGeneratorConstructorParams);
|
|
53
|
+
/**
|
|
54
|
+
* Process a schema to prepare it for JSON Schema generation.
|
|
55
|
+
* This must be called before emit().
|
|
56
|
+
*/
|
|
57
|
+
process(schema: schemas.$ZodType, _params?: ProcessParams): JSONSchema.BaseSchema;
|
|
58
|
+
/**
|
|
59
|
+
* Emit the final JSON Schema after processing.
|
|
60
|
+
* Must call process() first.
|
|
61
|
+
*/
|
|
62
|
+
emit(schema: schemas.$ZodType, _params?: EmitParams): JSONSchema.BaseSchema;
|
|
63
|
+
}
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type * as JSONSchema from "./json-schema.js";
|
|
2
|
+
import type * as schemas from "./schemas.js";
|
|
3
|
+
import { type JSONSchemaGeneratorParams, type ProcessParams } from "./to-json-schema.js";
|
|
4
|
+
/**
|
|
5
|
+
* Parameters for the emit method of JSONSchemaGenerator.
|
|
6
|
+
* @deprecated Use toJSONSchema function instead
|
|
7
|
+
*/
|
|
8
|
+
export type EmitParams = Pick<JSONSchemaGeneratorParams, "cycles" | "reused" | "external">;
|
|
9
|
+
/**
|
|
10
|
+
* Parameters for JSONSchemaGenerator constructor.
|
|
11
|
+
* @deprecated Use toJSONSchema function instead
|
|
12
|
+
*/
|
|
13
|
+
type JSONSchemaGeneratorConstructorParams = Pick<JSONSchemaGeneratorParams, "metadata" | "target" | "unrepresentable" | "override" | "io">;
|
|
14
|
+
/**
|
|
15
|
+
* Legacy class-based interface for JSON Schema generation.
|
|
16
|
+
* This class wraps the new functional implementation to provide backward compatibility.
|
|
17
|
+
*
|
|
18
|
+
* @deprecated Use the `toJSONSchema` function instead for new code.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Legacy usage (still supported)
|
|
23
|
+
* const gen = new JSONSchemaGenerator({ target: "draft-07" });
|
|
24
|
+
* gen.process(schema);
|
|
25
|
+
* const result = gen.emit(schema);
|
|
26
|
+
*
|
|
27
|
+
* // Preferred modern usage
|
|
28
|
+
* const result = toJSONSchema(schema, { target: "draft-07" });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class JSONSchemaGenerator {
|
|
32
|
+
private ctx;
|
|
33
|
+
/** @deprecated Access via ctx instead */
|
|
34
|
+
get metadataRegistry(): import("./registries.js").$ZodRegistry<Record<string, any>, schemas.$ZodType<unknown, unknown, schemas.$ZodTypeInternals<unknown, unknown>>>;
|
|
35
|
+
/** @deprecated Access via ctx instead */
|
|
36
|
+
get target(): ({} & string) | "draft-2020-12" | "draft-07" | "openapi-3.0" | "draft-04";
|
|
37
|
+
/** @deprecated Access via ctx instead */
|
|
38
|
+
get unrepresentable(): "any" | "throw";
|
|
39
|
+
/** @deprecated Access via ctx instead */
|
|
40
|
+
get override(): (ctx: {
|
|
41
|
+
zodSchema: schemas.$ZodType;
|
|
42
|
+
jsonSchema: JSONSchema.BaseSchema;
|
|
43
|
+
path: (string | number)[];
|
|
44
|
+
}) => void;
|
|
45
|
+
/** @deprecated Access via ctx instead */
|
|
46
|
+
get io(): "input" | "output";
|
|
47
|
+
/** @deprecated Access via ctx instead */
|
|
48
|
+
get counter(): number;
|
|
49
|
+
set counter(value: number);
|
|
50
|
+
/** @deprecated Access via ctx instead */
|
|
51
|
+
get seen(): Map<schemas.$ZodType<unknown, unknown, schemas.$ZodTypeInternals<unknown, unknown>>, import("./to-json-schema.js").Seen>;
|
|
52
|
+
constructor(params?: JSONSchemaGeneratorConstructorParams);
|
|
53
|
+
/**
|
|
54
|
+
* Process a schema to prepare it for JSON Schema generation.
|
|
55
|
+
* This must be called before emit().
|
|
56
|
+
*/
|
|
57
|
+
process(schema: schemas.$ZodType, _params?: ProcessParams): JSONSchema.BaseSchema;
|
|
58
|
+
/**
|
|
59
|
+
* Emit the final JSON Schema after processing.
|
|
60
|
+
* Must call process() first.
|
|
61
|
+
*/
|
|
62
|
+
emit(schema: schemas.$ZodType, _params?: EmitParams): JSONSchema.BaseSchema;
|
|
63
|
+
}
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { allProcessors } from "./json-schema-processors.js";
|
|
2
|
+
import { extractDefs, finalize, initializeContext, process, } from "./to-json-schema.js";
|
|
3
|
+
/**
|
|
4
|
+
* Legacy class-based interface for JSON Schema generation.
|
|
5
|
+
* This class wraps the new functional implementation to provide backward compatibility.
|
|
6
|
+
*
|
|
7
|
+
* @deprecated Use the `toJSONSchema` function instead for new code.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Legacy usage (still supported)
|
|
12
|
+
* const gen = new JSONSchemaGenerator({ target: "draft-07" });
|
|
13
|
+
* gen.process(schema);
|
|
14
|
+
* const result = gen.emit(schema);
|
|
15
|
+
*
|
|
16
|
+
* // Preferred modern usage
|
|
17
|
+
* const result = toJSONSchema(schema, { target: "draft-07" });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class JSONSchemaGenerator {
|
|
21
|
+
/** @deprecated Access via ctx instead */
|
|
22
|
+
get metadataRegistry() {
|
|
23
|
+
return this.ctx.metadataRegistry;
|
|
24
|
+
}
|
|
25
|
+
/** @deprecated Access via ctx instead */
|
|
26
|
+
get target() {
|
|
27
|
+
return this.ctx.target;
|
|
28
|
+
}
|
|
29
|
+
/** @deprecated Access via ctx instead */
|
|
30
|
+
get unrepresentable() {
|
|
31
|
+
return this.ctx.unrepresentable;
|
|
32
|
+
}
|
|
33
|
+
/** @deprecated Access via ctx instead */
|
|
34
|
+
get override() {
|
|
35
|
+
return this.ctx.override;
|
|
36
|
+
}
|
|
37
|
+
/** @deprecated Access via ctx instead */
|
|
38
|
+
get io() {
|
|
39
|
+
return this.ctx.io;
|
|
40
|
+
}
|
|
41
|
+
/** @deprecated Access via ctx instead */
|
|
42
|
+
get counter() {
|
|
43
|
+
return this.ctx.counter;
|
|
44
|
+
}
|
|
45
|
+
set counter(value) {
|
|
46
|
+
this.ctx.counter = value;
|
|
47
|
+
}
|
|
48
|
+
/** @deprecated Access via ctx instead */
|
|
49
|
+
get seen() {
|
|
50
|
+
return this.ctx.seen;
|
|
51
|
+
}
|
|
52
|
+
constructor(params) {
|
|
53
|
+
// Normalize target for internal context
|
|
54
|
+
let normalizedTarget = params?.target ?? "draft-2020-12";
|
|
55
|
+
if (normalizedTarget === "draft-4")
|
|
56
|
+
normalizedTarget = "draft-04";
|
|
57
|
+
if (normalizedTarget === "draft-7")
|
|
58
|
+
normalizedTarget = "draft-07";
|
|
59
|
+
this.ctx = initializeContext({
|
|
60
|
+
processors: allProcessors,
|
|
61
|
+
target: normalizedTarget,
|
|
62
|
+
...(params?.metadata && { metadata: params.metadata }),
|
|
63
|
+
...(params?.unrepresentable && { unrepresentable: params.unrepresentable }),
|
|
64
|
+
...(params?.override && { override: params.override }),
|
|
65
|
+
...(params?.io && { io: params.io }),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Process a schema to prepare it for JSON Schema generation.
|
|
70
|
+
* This must be called before emit().
|
|
71
|
+
*/
|
|
72
|
+
process(schema, _params = { path: [], schemaPath: [] }) {
|
|
73
|
+
return process(schema, this.ctx, _params);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Emit the final JSON Schema after processing.
|
|
77
|
+
* Must call process() first.
|
|
78
|
+
*/
|
|
79
|
+
emit(schema, _params) {
|
|
80
|
+
// Apply emit params to the context
|
|
81
|
+
if (_params) {
|
|
82
|
+
if (_params.cycles)
|
|
83
|
+
this.ctx.cycles = _params.cycles;
|
|
84
|
+
if (_params.reused)
|
|
85
|
+
this.ctx.reused = _params.reused;
|
|
86
|
+
if (_params.external)
|
|
87
|
+
this.ctx.external = _params.external;
|
|
88
|
+
}
|
|
89
|
+
extractDefs(this.ctx, schema);
|
|
90
|
+
const result = finalize(this.ctx, schema);
|
|
91
|
+
// Strip ~standard property to match old implementation's return type
|
|
92
|
+
const { "~standard": _, ...plainResult } = result;
|
|
93
|
+
return plainResult;
|
|
94
|
+
}
|
|
95
|
+
}
|