@gabrielbryk/json-schema-to-zod 2.10.1 → 2.11.0
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/AGENTS.md +44 -0
- package/CHANGELOG.md +28 -0
- package/README.md +6 -33
- package/check-types-lift.sh +23 -0
- package/check-types.sh +20 -0
- package/dist/{esm/cli.js → cli.js} +0 -6
- package/dist/{esm/core → core}/analyzeSchema.js +4 -5
- package/dist/core/emitZod.js +263 -0
- package/dist/{esm/generators → generators}/generateBundle.js +26 -13
- package/dist/{esm/index.js → index.js} +6 -0
- package/dist/jsonSchemaToZod.js +17 -0
- package/dist/parsers/parseAllOf.js +125 -0
- package/dist/parsers/parseAnyOf.js +28 -0
- package/dist/{esm/parsers → parsers}/parseArray.js +27 -11
- package/dist/parsers/parseBoolean.js +4 -0
- package/dist/parsers/parseConst.js +22 -0
- package/dist/parsers/parseEnum.js +35 -0
- package/dist/{esm/parsers → parsers}/parseIfThenElse.js +10 -6
- package/dist/parsers/parseMultipleType.js +10 -0
- package/dist/parsers/parseNot.js +14 -0
- package/dist/parsers/parseNull.js +4 -0
- package/dist/parsers/parseNullable.js +12 -0
- package/dist/{esm/parsers → parsers}/parseNumber.js +4 -1
- package/dist/{esm/parsers → parsers}/parseObject.js +167 -31
- package/dist/parsers/parseOneOf.js +365 -0
- package/dist/{esm/parsers → parsers}/parseSchema.js +55 -117
- package/dist/parsers/parseSimpleDiscriminatedOneOf.js +24 -0
- package/dist/{esm/parsers → parsers}/parseString.js +29 -18
- package/dist/types/Types.d.ts +32 -4
- package/dist/types/core/analyzeSchema.d.ts +3 -2
- package/dist/types/generators/generateBundle.d.ts +0 -2
- package/dist/types/index.d.ts +6 -0
- package/dist/types/parsers/parseAllOf.d.ts +2 -2
- package/dist/types/parsers/parseAnyOf.d.ts +2 -2
- package/dist/types/parsers/parseArray.d.ts +2 -2
- package/dist/types/parsers/parseBoolean.d.ts +2 -1
- package/dist/types/parsers/parseConst.d.ts +2 -2
- package/dist/types/parsers/parseDefault.d.ts +2 -2
- package/dist/types/parsers/parseEnum.d.ts +2 -2
- package/dist/types/parsers/parseIfThenElse.d.ts +2 -2
- package/dist/types/parsers/parseMultipleType.d.ts +2 -2
- package/dist/types/parsers/parseNot.d.ts +2 -2
- package/dist/types/parsers/parseNull.d.ts +2 -1
- package/dist/types/parsers/parseNullable.d.ts +2 -2
- package/dist/types/parsers/parseNumber.d.ts +2 -2
- package/dist/types/parsers/parseObject.d.ts +2 -2
- package/dist/types/parsers/parseOneOf.d.ts +2 -2
- package/dist/types/parsers/parseSchema.d.ts +2 -2
- package/dist/types/parsers/parseSimpleDiscriminatedOneOf.d.ts +2 -2
- package/dist/types/parsers/parseString.d.ts +2 -2
- package/dist/types/utils/anyOrUnknown.d.ts +5 -4
- package/dist/types/utils/esmEmitter.d.ts +29 -0
- package/dist/types/utils/extractInlineObject.d.ts +15 -0
- package/dist/types/utils/liftInlineObjects.d.ts +21 -0
- package/dist/types/utils/namingService.d.ts +21 -0
- package/dist/types/utils/resolveRef.d.ts +7 -0
- package/dist/types/utils/schemaRepresentation.d.ts +71 -0
- package/dist/utils/anyOrUnknown.js +13 -0
- package/dist/{esm/utils → utils}/buildRefRegistry.js +4 -0
- package/dist/utils/esmEmitter.js +87 -0
- package/dist/utils/extractInlineObject.js +119 -0
- package/dist/utils/liftInlineObjects.js +476 -0
- package/dist/utils/namingService.js +58 -0
- package/dist/utils/resolveRef.js +92 -0
- package/dist/utils/schemaRepresentation.js +569 -0
- package/docs/IMPROVEMENT-PLAN.md +243 -0
- package/docs/ZOD-V4-RECURSIVE-TYPE-LIMITATIONS.md +292 -0
- package/docs/proposals/bundle-refactor.md +1 -1
- package/docs/proposals/discriminated-union-with-default.md +248 -0
- package/docs/proposals/inline-object-lifting.md +77 -0
- package/eslint.config.js +4 -2
- package/jest.config.mjs +19 -0
- package/package.json +17 -20
- package/scripts/generateWorkflowSchema.ts +0 -1
- package/dist/cjs/Types.js +0 -2
- package/dist/cjs/cli.js +0 -70
- package/dist/cjs/core/analyzeSchema.js +0 -62
- package/dist/cjs/core/emitZod.js +0 -157
- package/dist/cjs/generators/generateBundle.js +0 -510
- package/dist/cjs/index.js +0 -50
- package/dist/cjs/jsonSchemaToZod.js +0 -10
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/parsers/parseAllOf.js +0 -46
- package/dist/cjs/parsers/parseAnyOf.js +0 -18
- package/dist/cjs/parsers/parseArray.js +0 -90
- package/dist/cjs/parsers/parseBoolean.js +0 -5
- package/dist/cjs/parsers/parseConst.js +0 -7
- package/dist/cjs/parsers/parseDefault.js +0 -8
- package/dist/cjs/parsers/parseEnum.js +0 -21
- package/dist/cjs/parsers/parseIfThenElse.js +0 -35
- package/dist/cjs/parsers/parseMultipleType.js +0 -10
- package/dist/cjs/parsers/parseNot.js +0 -12
- package/dist/cjs/parsers/parseNull.js +0 -5
- package/dist/cjs/parsers/parseNullable.js +0 -12
- package/dist/cjs/parsers/parseNumber.js +0 -116
- package/dist/cjs/parsers/parseObject.js +0 -318
- package/dist/cjs/parsers/parseOneOf.js +0 -53
- package/dist/cjs/parsers/parseSchema.js +0 -419
- package/dist/cjs/parsers/parseSimpleDiscriminatedOneOf.js +0 -21
- package/dist/cjs/parsers/parseString.js +0 -317
- package/dist/cjs/utils/anyOrUnknown.js +0 -14
- package/dist/cjs/utils/buildRefRegistry.js +0 -56
- package/dist/cjs/utils/cliTools.js +0 -108
- package/dist/cjs/utils/cycles.js +0 -113
- package/dist/cjs/utils/half.js +0 -7
- package/dist/cjs/utils/jsdocs.js +0 -20
- package/dist/cjs/utils/omit.js +0 -11
- package/dist/cjs/utils/resolveUri.js +0 -16
- package/dist/cjs/utils/withMessage.js +0 -21
- package/dist/cjs/zodToJsonSchema.js +0 -89
- package/dist/esm/core/emitZod.js +0 -153
- package/dist/esm/jsonSchemaToZod.js +0 -6
- package/dist/esm/package.json +0 -1
- package/dist/esm/parsers/parseAllOf.js +0 -43
- package/dist/esm/parsers/parseAnyOf.js +0 -14
- package/dist/esm/parsers/parseBoolean.js +0 -1
- package/dist/esm/parsers/parseConst.js +0 -3
- package/dist/esm/parsers/parseEnum.js +0 -17
- package/dist/esm/parsers/parseMultipleType.js +0 -6
- package/dist/esm/parsers/parseNot.js +0 -8
- package/dist/esm/parsers/parseNull.js +0 -1
- package/dist/esm/parsers/parseNullable.js +0 -8
- package/dist/esm/parsers/parseOneOf.js +0 -49
- package/dist/esm/parsers/parseSimpleDiscriminatedOneOf.js +0 -17
- package/dist/esm/utils/anyOrUnknown.js +0 -10
- package/jest.config.cjs +0 -4
- package/postcjs.cjs +0 -1
- package/postesm.cjs +0 -1
- /package/dist/{esm/Types.js → Types.js} +0 -0
- /package/dist/{esm/parsers → parsers}/parseDefault.js +0 -0
- /package/dist/{esm/utils → utils}/cliTools.js +0 -0
- /package/dist/{esm/utils → utils}/cycles.js +0 -0
- /package/dist/{esm/utils → utils}/half.js +0 -0
- /package/dist/{esm/utils → utils}/jsdocs.js +0 -0
- /package/dist/{esm/utils → utils}/omit.js +0 -0
- /package/dist/{esm/utils → utils}/resolveUri.js +0 -0
- /package/dist/{esm/utils → utils}/withMessage.js +0 -0
- /package/dist/{esm/zodToJsonSchema.js → zodToJsonSchema.js} +0 -0
package/dist/cjs/index.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./Types.js"), exports);
|
|
18
|
-
__exportStar(require("./core/analyzeSchema.js"), exports);
|
|
19
|
-
__exportStar(require("./core/emitZod.js"), exports);
|
|
20
|
-
__exportStar(require("./generators/generateBundle.js"), exports);
|
|
21
|
-
__exportStar(require("./jsonSchemaToZod.js"), exports);
|
|
22
|
-
__exportStar(require("./parsers/parseAllOf.js"), exports);
|
|
23
|
-
__exportStar(require("./parsers/parseAnyOf.js"), exports);
|
|
24
|
-
__exportStar(require("./parsers/parseArray.js"), exports);
|
|
25
|
-
__exportStar(require("./parsers/parseBoolean.js"), exports);
|
|
26
|
-
__exportStar(require("./parsers/parseConst.js"), exports);
|
|
27
|
-
__exportStar(require("./parsers/parseDefault.js"), exports);
|
|
28
|
-
__exportStar(require("./parsers/parseEnum.js"), exports);
|
|
29
|
-
__exportStar(require("./parsers/parseIfThenElse.js"), exports);
|
|
30
|
-
__exportStar(require("./parsers/parseMultipleType.js"), exports);
|
|
31
|
-
__exportStar(require("./parsers/parseNot.js"), exports);
|
|
32
|
-
__exportStar(require("./parsers/parseNull.js"), exports);
|
|
33
|
-
__exportStar(require("./parsers/parseNullable.js"), exports);
|
|
34
|
-
__exportStar(require("./parsers/parseNumber.js"), exports);
|
|
35
|
-
__exportStar(require("./parsers/parseObject.js"), exports);
|
|
36
|
-
__exportStar(require("./parsers/parseOneOf.js"), exports);
|
|
37
|
-
__exportStar(require("./parsers/parseSchema.js"), exports);
|
|
38
|
-
__exportStar(require("./parsers/parseSimpleDiscriminatedOneOf.js"), exports);
|
|
39
|
-
__exportStar(require("./parsers/parseString.js"), exports);
|
|
40
|
-
__exportStar(require("./utils/anyOrUnknown.js"), exports);
|
|
41
|
-
__exportStar(require("./utils/buildRefRegistry.js"), exports);
|
|
42
|
-
__exportStar(require("./utils/cycles.js"), exports);
|
|
43
|
-
__exportStar(require("./utils/half.js"), exports);
|
|
44
|
-
__exportStar(require("./utils/jsdocs.js"), exports);
|
|
45
|
-
__exportStar(require("./utils/omit.js"), exports);
|
|
46
|
-
__exportStar(require("./utils/resolveUri.js"), exports);
|
|
47
|
-
__exportStar(require("./utils/withMessage.js"), exports);
|
|
48
|
-
__exportStar(require("./zodToJsonSchema.js"), exports);
|
|
49
|
-
const jsonSchemaToZod_js_1 = require("./jsonSchemaToZod.js");
|
|
50
|
-
exports.default = jsonSchemaToZod_js_1.jsonSchemaToZod;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.jsonSchemaToZod = void 0;
|
|
4
|
-
const analyzeSchema_js_1 = require("./core/analyzeSchema.js");
|
|
5
|
-
const emitZod_js_1 = require("./core/emitZod.js");
|
|
6
|
-
const jsonSchemaToZod = (schema, options = {}) => {
|
|
7
|
-
const analysis = (0, analyzeSchema_js_1.analyzeSchema)(schema, options);
|
|
8
|
-
return (0, emitZod_js_1.emitZod)(analysis);
|
|
9
|
-
};
|
|
10
|
-
exports.jsonSchemaToZod = jsonSchemaToZod;
|
package/dist/cjs/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"commonjs"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseAllOf = parseAllOf;
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const half_js_1 = require("../utils/half.js");
|
|
6
|
-
const originalIndexKey = "__originalIndex";
|
|
7
|
-
const ensureOriginalIndex = (arr) => {
|
|
8
|
-
const newArr = [];
|
|
9
|
-
for (let i = 0; i < arr.length; i++) {
|
|
10
|
-
const item = arr[i];
|
|
11
|
-
if (typeof item === "boolean") {
|
|
12
|
-
newArr.push(item ? { [originalIndexKey]: i } : { [originalIndexKey]: i, not: {} });
|
|
13
|
-
}
|
|
14
|
-
else if (typeof item === "object" &&
|
|
15
|
-
item !== null &&
|
|
16
|
-
originalIndexKey in item) {
|
|
17
|
-
return arr;
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
newArr.push({ ...item, [originalIndexKey]: i });
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return newArr;
|
|
24
|
-
};
|
|
25
|
-
function parseAllOf(schema, refs) {
|
|
26
|
-
if (schema.allOf.length === 0) {
|
|
27
|
-
return "z.never()";
|
|
28
|
-
}
|
|
29
|
-
else if (schema.allOf.length === 1) {
|
|
30
|
-
const item = schema.allOf[0];
|
|
31
|
-
return (0, parseSchema_js_1.parseSchema)(item, {
|
|
32
|
-
...refs,
|
|
33
|
-
path: [
|
|
34
|
-
...refs.path,
|
|
35
|
-
"allOf",
|
|
36
|
-
item[originalIndexKey] ?? 0,
|
|
37
|
-
],
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
const [left, right] = (0, half_js_1.half)(ensureOriginalIndex(schema.allOf));
|
|
42
|
-
return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
|
|
43
|
-
allOf: right,
|
|
44
|
-
}, refs)})`;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseAnyOf = void 0;
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
6
|
-
const parseAnyOf = (schema, refs) => {
|
|
7
|
-
return schema.anyOf.length
|
|
8
|
-
? schema.anyOf.length === 1
|
|
9
|
-
? (0, parseSchema_js_1.parseSchema)(schema.anyOf[0], {
|
|
10
|
-
...refs,
|
|
11
|
-
path: [...refs.path, "anyOf", 0],
|
|
12
|
-
})
|
|
13
|
-
: `z.union([${schema.anyOf
|
|
14
|
-
.map((schema, i) => (0, parseSchema_js_1.parseSchema)(schema, { ...refs, path: [...refs.path, "anyOf", i] }))
|
|
15
|
-
.join(", ")}])`
|
|
16
|
-
: (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
17
|
-
};
|
|
18
|
-
exports.parseAnyOf = parseAnyOf;
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseArray = void 0;
|
|
4
|
-
const withMessage_js_1 = require("../utils/withMessage.js");
|
|
5
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
6
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
7
|
-
const parseArray = (schema, refs) => {
|
|
8
|
-
if (Array.isArray(schema.items)) {
|
|
9
|
-
let tuple = `z.tuple([${schema.items.map((v, i) => (0, parseSchema_js_1.parseSchema)(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
10
|
-
if (schema.contains) {
|
|
11
|
-
const containsSchema = (0, parseSchema_js_1.parseSchema)(schema.contains, {
|
|
12
|
-
...refs,
|
|
13
|
-
path: [...refs.path, "contains"],
|
|
14
|
-
});
|
|
15
|
-
const minContains = schema.minContains ?? (schema.contains ? 1 : undefined);
|
|
16
|
-
const maxContains = schema.maxContains;
|
|
17
|
-
tuple += `.superRefine((arr, ctx) => {
|
|
18
|
-
const matches = arr.filter((item) => ${containsSchema}.safeParse(item).success).length;
|
|
19
|
-
if (${minContains ?? 0} && matches < ${minContains ?? 0}) {
|
|
20
|
-
ctx.addIssue({ code: "custom", message: "Array contains too few matching items" });
|
|
21
|
-
}
|
|
22
|
-
if (${maxContains ?? "undefined"} !== undefined && matches > ${maxContains ?? "undefined"}) {
|
|
23
|
-
ctx.addIssue({ code: "custom", message: "Array contains too many matching items" });
|
|
24
|
-
}
|
|
25
|
-
})`;
|
|
26
|
-
}
|
|
27
|
-
return tuple;
|
|
28
|
-
}
|
|
29
|
-
let r = !schema.items
|
|
30
|
-
? `z.array(${(0, anyOrUnknown_js_1.anyOrUnknown)(refs)})`
|
|
31
|
-
: `z.array(${(0, parseSchema_js_1.parseSchema)(schema.items, {
|
|
32
|
-
...refs,
|
|
33
|
-
path: [...refs.path, "items"],
|
|
34
|
-
})})`;
|
|
35
|
-
r += (0, withMessage_js_1.withMessage)(schema, "minItems", ({ json }) => ({
|
|
36
|
-
opener: `.min(${json}`,
|
|
37
|
-
closer: ")",
|
|
38
|
-
messagePrefix: ", { error: ",
|
|
39
|
-
messageCloser: " })",
|
|
40
|
-
}));
|
|
41
|
-
r += (0, withMessage_js_1.withMessage)(schema, "maxItems", ({ json }) => ({
|
|
42
|
-
opener: `.max(${json}`,
|
|
43
|
-
closer: ")",
|
|
44
|
-
messagePrefix: ", { error: ",
|
|
45
|
-
messageCloser: " })",
|
|
46
|
-
}));
|
|
47
|
-
if (schema.uniqueItems === true) {
|
|
48
|
-
r += `.superRefine((arr, ctx) => {
|
|
49
|
-
const seen = new Set();
|
|
50
|
-
for (const [index, value] of arr.entries()) {
|
|
51
|
-
let key;
|
|
52
|
-
if (value && typeof value === "object") {
|
|
53
|
-
try {
|
|
54
|
-
key = JSON.stringify(value);
|
|
55
|
-
} catch {
|
|
56
|
-
key = String(value);
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
key = JSON.stringify(value);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (seen.has(key)) {
|
|
63
|
-
ctx.addIssue({ code: "custom", message: "Array items must be unique", path: [index] });
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
seen.add(key);
|
|
68
|
-
}
|
|
69
|
-
})`;
|
|
70
|
-
}
|
|
71
|
-
if (schema.contains) {
|
|
72
|
-
const containsSchema = (0, parseSchema_js_1.parseSchema)(schema.contains, {
|
|
73
|
-
...refs,
|
|
74
|
-
path: [...refs.path, "contains"],
|
|
75
|
-
});
|
|
76
|
-
const minContains = schema.minContains ?? (schema.contains ? 1 : undefined);
|
|
77
|
-
const maxContains = schema.maxContains;
|
|
78
|
-
r += `.superRefine((arr, ctx) => {
|
|
79
|
-
const matches = arr.filter((item) => ${containsSchema}.safeParse(item).success).length;
|
|
80
|
-
if (${minContains ?? 0} && matches < ${minContains ?? 0}) {
|
|
81
|
-
ctx.addIssue({ code: "custom", message: "Array contains too few matching items" });
|
|
82
|
-
}
|
|
83
|
-
if (${maxContains ?? "undefined"} !== undefined && matches > ${maxContains ?? "undefined"}) {
|
|
84
|
-
ctx.addIssue({ code: "custom", message: "Array contains too many matching items" });
|
|
85
|
-
}
|
|
86
|
-
})`;
|
|
87
|
-
}
|
|
88
|
-
return r;
|
|
89
|
-
};
|
|
90
|
-
exports.parseArray = parseArray;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseDefault = void 0;
|
|
4
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
5
|
-
const parseDefault = (_schema, refs) => {
|
|
6
|
-
return (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
7
|
-
};
|
|
8
|
-
exports.parseDefault = parseDefault;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseEnum = void 0;
|
|
4
|
-
const parseEnum = (schema) => {
|
|
5
|
-
if (schema.enum.length === 0) {
|
|
6
|
-
return "z.never()";
|
|
7
|
-
}
|
|
8
|
-
else if (schema.enum.length === 1) {
|
|
9
|
-
// union does not work when there is only one element
|
|
10
|
-
return `z.literal(${JSON.stringify(schema.enum[0])})`;
|
|
11
|
-
}
|
|
12
|
-
else if (schema.enum.every((x) => typeof x === "string")) {
|
|
13
|
-
return `z.enum([${schema.enum.map((x) => JSON.stringify(x))}])`;
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return `z.union([${schema.enum
|
|
17
|
-
.map((x) => `z.literal(${JSON.stringify(x)})`)
|
|
18
|
-
.join(", ")}])`;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
exports.parseEnum = parseEnum;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseIfThenElse = void 0;
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const parseIfThenElse = (schema, refs) => {
|
|
6
|
-
const $if = (0, parseSchema_js_1.parseSchema)(schema.if, { ...refs, path: [...refs.path, "if"] });
|
|
7
|
-
const $then = (0, parseSchema_js_1.parseSchema)(schema.then, {
|
|
8
|
-
...refs,
|
|
9
|
-
path: [...refs.path, "then"],
|
|
10
|
-
});
|
|
11
|
-
const $else = (0, parseSchema_js_1.parseSchema)(schema.else, {
|
|
12
|
-
...refs,
|
|
13
|
-
path: [...refs.path, "else"],
|
|
14
|
-
});
|
|
15
|
-
let result = `z.union([${$then}, ${$else}]).superRefine((value,ctx) => {
|
|
16
|
-
const result = ${$if}.safeParse(value).success
|
|
17
|
-
? ${$then}.safeParse(value)
|
|
18
|
-
: ${$else}.safeParse(value);
|
|
19
|
-
if (!result.success) {
|
|
20
|
-
const issues = result.error.issues;
|
|
21
|
-
issues.forEach((issue) => ctx.addIssue({ ...issue }))
|
|
22
|
-
}
|
|
23
|
-
})`;
|
|
24
|
-
// Store original if/then/else for JSON Schema round-trip
|
|
25
|
-
if (refs.preserveJsonSchemaForRoundTrip) {
|
|
26
|
-
const conditionalMeta = JSON.stringify({
|
|
27
|
-
if: schema.if,
|
|
28
|
-
then: schema.then,
|
|
29
|
-
else: schema.else,
|
|
30
|
-
});
|
|
31
|
-
result += `.meta({ __jsonSchema: { conditional: ${conditionalMeta} } })`;
|
|
32
|
-
}
|
|
33
|
-
return result;
|
|
34
|
-
};
|
|
35
|
-
exports.parseIfThenElse = parseIfThenElse;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseMultipleType = void 0;
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const parseMultipleType = (schema, refs) => {
|
|
6
|
-
return `z.union([${schema.type
|
|
7
|
-
.map((type) => (0, parseSchema_js_1.parseSchema)({ ...schema, type }, { ...refs, withoutDefaults: true }))
|
|
8
|
-
.join(", ")}])`;
|
|
9
|
-
};
|
|
10
|
-
exports.parseMultipleType = parseMultipleType;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseNot = void 0;
|
|
4
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
5
|
-
const anyOrUnknown_js_1 = require("../utils/anyOrUnknown.js");
|
|
6
|
-
const parseNot = (schema, refs) => {
|
|
7
|
-
return `${(0, anyOrUnknown_js_1.anyOrUnknown)(refs)}.refine((value) => !${(0, parseSchema_js_1.parseSchema)(schema.not, {
|
|
8
|
-
...refs,
|
|
9
|
-
path: [...refs.path, "not"],
|
|
10
|
-
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
|
|
11
|
-
};
|
|
12
|
-
exports.parseNot = parseNot;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseNullable = void 0;
|
|
4
|
-
const omit_js_1 = require("../utils/omit.js");
|
|
5
|
-
const parseSchema_js_1 = require("./parseSchema.js");
|
|
6
|
-
/**
|
|
7
|
-
* For compatibility with open api 3.0 nullable
|
|
8
|
-
*/
|
|
9
|
-
const parseNullable = (schema, refs) => {
|
|
10
|
-
return `${(0, parseSchema_js_1.parseSchema)((0, omit_js_1.omit)(schema, "nullable"), refs, true)}.nullable()`;
|
|
11
|
-
};
|
|
12
|
-
exports.parseNullable = parseNullable;
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseNumber = void 0;
|
|
4
|
-
const withMessage_js_1 = require("../utils/withMessage.js");
|
|
5
|
-
const parseNumber = (schema) => {
|
|
6
|
-
const formatError = schema.errorMessage?.format;
|
|
7
|
-
const numericFormatMap = {
|
|
8
|
-
int32: "z.int32",
|
|
9
|
-
uint32: "z.uint32",
|
|
10
|
-
float32: "z.float32",
|
|
11
|
-
float64: "z.float64",
|
|
12
|
-
safeint: "z.safeint",
|
|
13
|
-
int64: "z.int64",
|
|
14
|
-
uint64: "z.uint64",
|
|
15
|
-
};
|
|
16
|
-
const mappedFormat = schema.format && numericFormatMap[schema.format] ? numericFormatMap[schema.format] : undefined;
|
|
17
|
-
const formatParams = formatError !== undefined ? `{ error: ${JSON.stringify(formatError)} }` : "";
|
|
18
|
-
let r = mappedFormat ? `${mappedFormat}(${formatParams})` : "z.number()";
|
|
19
|
-
if (schema.type === "integer") {
|
|
20
|
-
if (!mappedFormat) {
|
|
21
|
-
r += (0, withMessage_js_1.withMessage)(schema, "type", () => ({
|
|
22
|
-
opener: ".int(",
|
|
23
|
-
closer: ")",
|
|
24
|
-
messagePrefix: "{ error: ",
|
|
25
|
-
messageCloser: " })",
|
|
26
|
-
}));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
if (!mappedFormat) {
|
|
31
|
-
r += (0, withMessage_js_1.withMessage)(schema, "format", ({ value }) => {
|
|
32
|
-
if (value === "int64") {
|
|
33
|
-
return {
|
|
34
|
-
opener: ".int(",
|
|
35
|
-
closer: ")",
|
|
36
|
-
messagePrefix: "{ error: ",
|
|
37
|
-
messageCloser: " })",
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
r += (0, withMessage_js_1.withMessage)(schema, "multipleOf", ({ value, json }) => {
|
|
44
|
-
if (value === 1) {
|
|
45
|
-
if (r.startsWith("z.number().int(")) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
opener: ".int(",
|
|
50
|
-
closer: ")",
|
|
51
|
-
messagePrefix: "{ error: ",
|
|
52
|
-
messageCloser: " })",
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
opener: `.multipleOf(${json}`,
|
|
57
|
-
closer: ")",
|
|
58
|
-
messagePrefix: ", { error: ",
|
|
59
|
-
messageCloser: " })",
|
|
60
|
-
};
|
|
61
|
-
});
|
|
62
|
-
if (typeof schema.minimum === "number") {
|
|
63
|
-
if (schema.exclusiveMinimum === true) {
|
|
64
|
-
r += (0, withMessage_js_1.withMessage)(schema, "minimum", ({ json }) => ({
|
|
65
|
-
opener: `.gt(${json}`,
|
|
66
|
-
closer: ")",
|
|
67
|
-
messagePrefix: ", { error: ",
|
|
68
|
-
messageCloser: " })",
|
|
69
|
-
}));
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
r += (0, withMessage_js_1.withMessage)(schema, "minimum", ({ json }) => ({
|
|
73
|
-
opener: `.gte(${json}`,
|
|
74
|
-
closer: ")",
|
|
75
|
-
messagePrefix: ", { error: ",
|
|
76
|
-
messageCloser: " })",
|
|
77
|
-
}));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
else if (typeof schema.exclusiveMinimum === "number") {
|
|
81
|
-
r += (0, withMessage_js_1.withMessage)(schema, "exclusiveMinimum", ({ json }) => ({
|
|
82
|
-
opener: `.gt(${json}`,
|
|
83
|
-
closer: ")",
|
|
84
|
-
messagePrefix: ", { error: ",
|
|
85
|
-
messageCloser: " })",
|
|
86
|
-
}));
|
|
87
|
-
}
|
|
88
|
-
if (typeof schema.maximum === "number") {
|
|
89
|
-
if (schema.exclusiveMaximum === true) {
|
|
90
|
-
r += (0, withMessage_js_1.withMessage)(schema, "maximum", ({ json }) => ({
|
|
91
|
-
opener: `.lt(${json}`,
|
|
92
|
-
closer: ")",
|
|
93
|
-
messagePrefix: ", { error: ",
|
|
94
|
-
messageCloser: " })",
|
|
95
|
-
}));
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
r += (0, withMessage_js_1.withMessage)(schema, "maximum", ({ json }) => ({
|
|
99
|
-
opener: `.lte(${json}`,
|
|
100
|
-
closer: ")",
|
|
101
|
-
messagePrefix: ", { error: ",
|
|
102
|
-
messageCloser: " })",
|
|
103
|
-
}));
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else if (typeof schema.exclusiveMaximum === "number") {
|
|
107
|
-
r += (0, withMessage_js_1.withMessage)(schema, "exclusiveMaximum", ({ json }) => ({
|
|
108
|
-
opener: `.lt(${json}`,
|
|
109
|
-
closer: ")",
|
|
110
|
-
messagePrefix: ", { error: ",
|
|
111
|
-
messageCloser: " })",
|
|
112
|
-
}));
|
|
113
|
-
}
|
|
114
|
-
return r;
|
|
115
|
-
};
|
|
116
|
-
exports.parseNumber = parseNumber;
|