@gabrielbryk/json-schema-to-zod 2.7.4 → 2.8.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/CHANGELOG.md +10 -1
- package/dist/cjs/generators/generateBundle.js +311 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/jsonSchemaToZod.js +96 -2
- package/dist/cjs/parsers/parseArray.js +34 -15
- package/dist/cjs/parsers/parseIfThenElse.js +2 -1
- package/dist/cjs/parsers/parseNumber.js +81 -39
- package/dist/cjs/parsers/parseObject.js +24 -0
- package/dist/cjs/parsers/parseSchema.js +23 -1
- package/dist/cjs/parsers/parseString.js +294 -54
- package/dist/cjs/utils/cycles.js +113 -0
- package/dist/cjs/utils/withMessage.js +4 -5
- package/dist/esm/Types.js +2 -1
- package/dist/esm/cli.js +12 -10
- package/dist/esm/generators/generateBundle.js +311 -0
- package/dist/esm/index.js +46 -28
- package/dist/esm/jsonSchemaToZod.js +105 -7
- package/dist/esm/parsers/parseAllOf.js +8 -5
- package/dist/esm/parsers/parseAnyOf.js +10 -6
- package/dist/esm/parsers/parseArray.js +47 -24
- package/dist/esm/parsers/parseBoolean.js +5 -1
- package/dist/esm/parsers/parseConst.js +5 -1
- package/dist/esm/parsers/parseDefault.js +7 -3
- package/dist/esm/parsers/parseEnum.js +5 -1
- package/dist/esm/parsers/parseIfThenElse.js +11 -6
- package/dist/esm/parsers/parseMultipleType.js +7 -3
- package/dist/esm/parsers/parseNot.js +8 -4
- package/dist/esm/parsers/parseNull.js +5 -1
- package/dist/esm/parsers/parseNullable.js +8 -4
- package/dist/esm/parsers/parseNumber.js +88 -42
- package/dist/esm/parsers/parseObject.js +52 -25
- package/dist/esm/parsers/parseOneOf.js +10 -6
- package/dist/esm/parsers/parseSchema.js +85 -59
- package/dist/esm/parsers/parseSimpleDiscriminatedOneOf.js +10 -6
- package/dist/esm/parsers/parseString.js +303 -59
- package/dist/esm/utils/anyOrUnknown.js +5 -1
- package/dist/esm/utils/cliTools.js +13 -7
- package/dist/esm/utils/cycles.js +113 -0
- package/dist/esm/utils/half.js +5 -1
- package/dist/esm/utils/jsdocs.js +8 -3
- package/dist/esm/utils/omit.js +5 -1
- package/dist/esm/utils/withMessage.js +8 -6
- package/dist/esm/zodToJsonSchema.js +4 -1
- package/dist/types/Types.d.ts +8 -0
- package/dist/types/generators/generateBundle.d.ts +57 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/parsers/parseString.d.ts +2 -2
- package/dist/types/utils/cycles.d.ts +7 -0
- package/dist/types/utils/jsdocs.d.ts +1 -1
- package/dist/types/utils/withMessage.d.ts +6 -1
- package/package.json +1 -1
|
@@ -1,32 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonSchemaToZod = void 0;
|
|
4
|
+
const parseSchema_js_1 = require("./parsers/parseSchema.js");
|
|
5
|
+
const jsdocs_js_1 = require("./utils/jsdocs.js");
|
|
6
|
+
const cycles_js_1 = require("./utils/cycles.js");
|
|
7
|
+
const jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest } = {}) => {
|
|
4
8
|
if (type && (!name || module !== "esm")) {
|
|
5
9
|
throw new Error("Option `type` requires `name` to be set and `module` to be `esm`");
|
|
6
10
|
}
|
|
7
|
-
const declarations = new Map();
|
|
8
11
|
const refNameByPointer = new Map();
|
|
9
12
|
const usedNames = new Set();
|
|
10
13
|
const exportRefs = rest.exportRefs ?? true;
|
|
11
14
|
const withMeta = rest.withMeta ?? true;
|
|
12
15
|
if (name)
|
|
13
16
|
usedNames.add(name);
|
|
14
|
-
|
|
17
|
+
// Pass 1: collect declarations/dependencies to detect cycles before emitting
|
|
18
|
+
const pass1 = {
|
|
19
|
+
module,
|
|
20
|
+
name,
|
|
21
|
+
path: [],
|
|
22
|
+
seen: new Map(),
|
|
23
|
+
declarations: new Map(),
|
|
24
|
+
dependencies: new Map(),
|
|
25
|
+
inProgress: new Set(),
|
|
26
|
+
refNameByPointer,
|
|
27
|
+
usedNames,
|
|
28
|
+
root: schema,
|
|
29
|
+
currentSchemaName: name,
|
|
30
|
+
...rest,
|
|
31
|
+
withMeta,
|
|
32
|
+
};
|
|
33
|
+
(0, parseSchema_js_1.parseSchema)(schema, pass1);
|
|
34
|
+
const names = Array.from(pass1.declarations.keys());
|
|
35
|
+
const cycleRefNames = (0, cycles_js_1.detectCycles)(names, pass1.dependencies);
|
|
36
|
+
const { componentByName } = (0, cycles_js_1.computeScc)(names, pass1.dependencies);
|
|
37
|
+
// Pass 2: generate with cycle awareness
|
|
38
|
+
const declarations = new Map();
|
|
39
|
+
const dependencies = new Map();
|
|
40
|
+
const parsedSchema = (0, parseSchema_js_1.parseSchema)(schema, {
|
|
15
41
|
module,
|
|
16
42
|
name,
|
|
17
43
|
path: [],
|
|
18
44
|
seen: new Map(),
|
|
19
45
|
declarations,
|
|
46
|
+
dependencies,
|
|
20
47
|
inProgress: new Set(),
|
|
21
48
|
refNameByPointer,
|
|
22
49
|
usedNames,
|
|
23
50
|
root: schema,
|
|
24
51
|
currentSchemaName: name,
|
|
52
|
+
cycleRefNames,
|
|
53
|
+
cycleComponentByName: componentByName,
|
|
25
54
|
...rest,
|
|
26
55
|
withMeta,
|
|
27
56
|
});
|
|
28
57
|
const declarationBlock = declarations.size
|
|
29
|
-
? Array.from(declarations.entries())
|
|
58
|
+
? orderDeclarations(Array.from(declarations.entries()), dependencies)
|
|
30
59
|
.map(([refName, value]) => {
|
|
31
60
|
const shouldExport = exportRefs && module === "esm";
|
|
32
61
|
const decl = `${shouldExport ? "export " : ""}const ${refName} = ${value}`;
|
|
@@ -35,7 +64,7 @@ export const jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest
|
|
|
35
64
|
.join("\n")
|
|
36
65
|
: "";
|
|
37
66
|
const jsdocs = rest.withJsdocs && typeof schema !== "boolean" && schema.description
|
|
38
|
-
? expandJsdocs(schema.description)
|
|
67
|
+
? (0, jsdocs_js_1.expandJsdocs)(schema.description)
|
|
39
68
|
: "";
|
|
40
69
|
const lines = [];
|
|
41
70
|
if (module === "cjs" && !noImport) {
|
|
@@ -72,3 +101,72 @@ export const jsonSchemaToZod = (schema, { module, name, type, noImport, ...rest
|
|
|
72
101
|
const shouldEndWithNewline = module === "esm" || module === "cjs";
|
|
73
102
|
return `${combined}${shouldEndWithNewline ? "\n" : ""}`;
|
|
74
103
|
};
|
|
104
|
+
exports.jsonSchemaToZod = jsonSchemaToZod;
|
|
105
|
+
function orderDeclarations(entries, dependencies) {
|
|
106
|
+
const valueByName = new Map(entries);
|
|
107
|
+
const depGraph = new Map();
|
|
108
|
+
// Seed with explicit dependencies recorded during parsing
|
|
109
|
+
for (const [from, set] of dependencies.entries()) {
|
|
110
|
+
const onlyKnown = new Set();
|
|
111
|
+
for (const dep of set) {
|
|
112
|
+
if (valueByName.has(dep) && dep !== from) {
|
|
113
|
+
onlyKnown.add(dep);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (onlyKnown.size)
|
|
117
|
+
depGraph.set(from, onlyKnown);
|
|
118
|
+
}
|
|
119
|
+
// Also infer deps by scanning declaration bodies for referenced names
|
|
120
|
+
const names = Array.from(valueByName.keys());
|
|
121
|
+
for (const [name, value] of entries) {
|
|
122
|
+
const deps = depGraph.get(name) ?? new Set();
|
|
123
|
+
for (const candidate of names) {
|
|
124
|
+
if (candidate === name)
|
|
125
|
+
continue;
|
|
126
|
+
const matcher = new RegExp(`\\b${candidate}\\b`);
|
|
127
|
+
if (matcher.test(value)) {
|
|
128
|
+
deps.add(candidate);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (deps.size)
|
|
132
|
+
depGraph.set(name, deps);
|
|
133
|
+
}
|
|
134
|
+
const ordered = [];
|
|
135
|
+
const perm = new Set();
|
|
136
|
+
const temp = new Set();
|
|
137
|
+
const visit = (name) => {
|
|
138
|
+
if (perm.has(name))
|
|
139
|
+
return;
|
|
140
|
+
if (temp.has(name)) {
|
|
141
|
+
// Cycle detected; break it but still include the node.
|
|
142
|
+
temp.delete(name);
|
|
143
|
+
perm.add(name);
|
|
144
|
+
ordered.push(name);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
temp.add(name);
|
|
148
|
+
const deps = depGraph.get(name);
|
|
149
|
+
if (deps) {
|
|
150
|
+
for (const dep of deps) {
|
|
151
|
+
if (valueByName.has(dep)) {
|
|
152
|
+
visit(dep);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
temp.delete(name);
|
|
157
|
+
perm.add(name);
|
|
158
|
+
ordered.push(name);
|
|
159
|
+
};
|
|
160
|
+
for (const name of valueByName.keys()) {
|
|
161
|
+
visit(name);
|
|
162
|
+
}
|
|
163
|
+
const unique = [];
|
|
164
|
+
const seen = new Set();
|
|
165
|
+
for (const name of ordered) {
|
|
166
|
+
if (!seen.has(name)) {
|
|
167
|
+
seen.add(name);
|
|
168
|
+
unique.push(name);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return unique.map((name) => [name, valueByName.get(name)]);
|
|
172
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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");
|
|
3
6
|
const originalIndex = Symbol("Original index");
|
|
4
7
|
const ensureOriginalIndex = (arr) => {
|
|
5
8
|
let newArr = [];
|
|
@@ -17,19 +20,19 @@ const ensureOriginalIndex = (arr) => {
|
|
|
17
20
|
}
|
|
18
21
|
return newArr;
|
|
19
22
|
};
|
|
20
|
-
|
|
23
|
+
function parseAllOf(schema, refs) {
|
|
21
24
|
if (schema.allOf.length === 0) {
|
|
22
25
|
return "z.never()";
|
|
23
26
|
}
|
|
24
27
|
else if (schema.allOf.length === 1) {
|
|
25
28
|
const item = schema.allOf[0];
|
|
26
|
-
return parseSchema(item, {
|
|
29
|
+
return (0, parseSchema_js_1.parseSchema)(item, {
|
|
27
30
|
...refs,
|
|
28
31
|
path: [...refs.path, "allOf", item[originalIndex]],
|
|
29
32
|
});
|
|
30
33
|
}
|
|
31
34
|
else {
|
|
32
|
-
const [left, right] = half(ensureOriginalIndex(schema.allOf));
|
|
35
|
+
const [left, right] = (0, half_js_1.half)(ensureOriginalIndex(schema.allOf));
|
|
33
36
|
return `z.intersection(${parseAllOf({ allOf: left }, refs)}, ${parseAllOf({
|
|
34
37
|
allOf: right,
|
|
35
38
|
}, refs)})`;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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) => {
|
|
4
7
|
return schema.anyOf.length
|
|
5
8
|
? schema.anyOf.length === 1
|
|
6
|
-
? parseSchema(schema.anyOf[0], {
|
|
9
|
+
? (0, parseSchema_js_1.parseSchema)(schema.anyOf[0], {
|
|
7
10
|
...refs,
|
|
8
11
|
path: [...refs.path, "anyOf", 0],
|
|
9
12
|
})
|
|
10
13
|
: `z.union([${schema.anyOf
|
|
11
|
-
.map((schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] }))
|
|
14
|
+
.map((schema, i) => (0, parseSchema_js_1.parseSchema)(schema, { ...refs, path: [...refs.path, "anyOf", i] }))
|
|
12
15
|
.join(", ")}])`
|
|
13
|
-
: anyOrUnknown(refs);
|
|
16
|
+
: (0, anyOrUnknown_js_1.anyOrUnknown)(refs);
|
|
14
17
|
};
|
|
18
|
+
exports.parseAnyOf = parseAnyOf;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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) => {
|
|
5
8
|
if (Array.isArray(schema.items)) {
|
|
6
|
-
let tuple = `z.tuple([${schema.items.map((v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
9
|
+
let tuple = `z.tuple([${schema.items.map((v, i) => (0, parseSchema_js_1.parseSchema)(v, { ...refs, path: [...refs.path, "items", i] }))}])`;
|
|
7
10
|
if (schema.contains) {
|
|
8
|
-
const containsSchema = parseSchema(schema.contains, {
|
|
11
|
+
const containsSchema = (0, parseSchema_js_1.parseSchema)(schema.contains, {
|
|
9
12
|
...refs,
|
|
10
13
|
path: [...refs.path, "contains"],
|
|
11
14
|
});
|
|
@@ -24,30 +27,49 @@ export const parseArray = (schema, refs) => {
|
|
|
24
27
|
return tuple;
|
|
25
28
|
}
|
|
26
29
|
let r = !schema.items
|
|
27
|
-
? `z.array(${anyOrUnknown(refs)})`
|
|
28
|
-
: `z.array(${parseSchema(schema.items, {
|
|
30
|
+
? `z.array(${(0, anyOrUnknown_js_1.anyOrUnknown)(refs)})`
|
|
31
|
+
: `z.array(${(0, parseSchema_js_1.parseSchema)(schema.items, {
|
|
29
32
|
...refs,
|
|
30
33
|
path: [...refs.path, "items"],
|
|
31
34
|
})})`;
|
|
32
|
-
r += withMessage(schema, "minItems", ({ json }) =>
|
|
33
|
-
`.min(${json}`,
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
")",
|
|
41
|
-
|
|
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
|
+
}));
|
|
42
47
|
if (schema.uniqueItems === true) {
|
|
43
|
-
r +=
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
})`;
|
|
48
70
|
}
|
|
49
71
|
if (schema.contains) {
|
|
50
|
-
const containsSchema = parseSchema(schema.contains, {
|
|
72
|
+
const containsSchema = (0, parseSchema_js_1.parseSchema)(schema.contains, {
|
|
51
73
|
...refs,
|
|
52
74
|
path: [...refs.path, "contains"],
|
|
53
75
|
});
|
|
@@ -65,3 +87,4 @@ export const parseArray = (schema, refs) => {
|
|
|
65
87
|
}
|
|
66
88
|
return r;
|
|
67
89
|
};
|
|
90
|
+
exports.parseArray = parseArray;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseConst = void 0;
|
|
4
|
+
const parseConst = (schema) => {
|
|
2
5
|
return `z.literal(${JSON.stringify(schema.const)})`;
|
|
3
6
|
};
|
|
7
|
+
exports.parseConst = parseConst;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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);
|
|
4
7
|
};
|
|
8
|
+
exports.parseDefault = parseDefault;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseEnum = void 0;
|
|
4
|
+
const parseEnum = (schema) => {
|
|
2
5
|
if (schema.enum.length === 0) {
|
|
3
6
|
return "z.never()";
|
|
4
7
|
}
|
|
@@ -15,3 +18,4 @@ export const parseEnum = (schema) => {
|
|
|
15
18
|
.join(", ")}])`;
|
|
16
19
|
}
|
|
17
20
|
};
|
|
21
|
+
exports.parseEnum = parseEnum;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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, {
|
|
5
8
|
...refs,
|
|
6
9
|
path: [...refs.path, "then"],
|
|
7
10
|
});
|
|
8
|
-
const $else = parseSchema(schema.else, {
|
|
11
|
+
const $else = (0, parseSchema_js_1.parseSchema)(schema.else, {
|
|
9
12
|
...refs,
|
|
10
13
|
path: [...refs.path, "else"],
|
|
11
14
|
});
|
|
@@ -14,7 +17,8 @@ export const parseIfThenElse = (schema, refs) => {
|
|
|
14
17
|
? ${$then}.safeParse(value)
|
|
15
18
|
: ${$else}.safeParse(value);
|
|
16
19
|
if (!result.success) {
|
|
17
|
-
result.error.
|
|
20
|
+
const issues = result.error.issues ?? result.error.errors ?? [];
|
|
21
|
+
issues.forEach((issue) => ctx.addIssue(issue))
|
|
18
22
|
}
|
|
19
23
|
})`;
|
|
20
24
|
// Store original if/then/else for JSON Schema round-trip
|
|
@@ -28,3 +32,4 @@ export const parseIfThenElse = (schema, refs) => {
|
|
|
28
32
|
}
|
|
29
33
|
return result;
|
|
30
34
|
};
|
|
35
|
+
exports.parseIfThenElse = parseIfThenElse;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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) => {
|
|
3
6
|
return `z.union([${schema.type
|
|
4
|
-
.map((type) => parseSchema({ ...schema, type }, { ...refs, withoutDefaults: true }))
|
|
7
|
+
.map((type) => (0, parseSchema_js_1.parseSchema)({ ...schema, type }, { ...refs, withoutDefaults: true }))
|
|
5
8
|
.join(", ")}])`;
|
|
6
9
|
};
|
|
10
|
+
exports.parseMultipleType = parseMultipleType;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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, {
|
|
5
8
|
...refs,
|
|
6
9
|
path: [...refs.path, "not"],
|
|
7
10
|
})}.safeParse(value).success, "Invalid input: Should NOT be valid against schema")`;
|
|
8
11
|
};
|
|
12
|
+
exports.parseNot = parseNot;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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");
|
|
3
6
|
/**
|
|
4
7
|
* For compatibility with open api 3.0 nullable
|
|
5
8
|
*/
|
|
6
|
-
|
|
7
|
-
return `${parseSchema(omit(schema, "nullable"), refs, true)}.nullable()`;
|
|
9
|
+
const parseNullable = (schema, refs) => {
|
|
10
|
+
return `${(0, parseSchema_js_1.parseSchema)((0, omit_js_1.omit)(schema, "nullable"), refs, true)}.nullable()`;
|
|
8
11
|
};
|
|
12
|
+
exports.parseNullable = parseNullable;
|
|
@@ -1,70 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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()";
|
|
4
19
|
if (schema.type === "integer") {
|
|
5
|
-
|
|
20
|
+
if (!mappedFormat) {
|
|
21
|
+
r += (0, withMessage_js_1.withMessage)(schema, "type", () => ({
|
|
22
|
+
opener: ".int(",
|
|
23
|
+
closer: ")",
|
|
24
|
+
messagePrefix: "{ error: ",
|
|
25
|
+
messageCloser: " })",
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
6
28
|
}
|
|
7
29
|
else {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
+
}
|
|
13
42
|
}
|
|
14
|
-
r += withMessage(schema, "multipleOf", ({ value, json }) => {
|
|
43
|
+
r += (0, withMessage_js_1.withMessage)(schema, "multipleOf", ({ value, json }) => {
|
|
15
44
|
if (value === 1) {
|
|
16
45
|
if (r.startsWith("z.number().int(")) {
|
|
17
46
|
return;
|
|
18
47
|
}
|
|
19
|
-
return
|
|
48
|
+
return {
|
|
49
|
+
opener: ".int(",
|
|
50
|
+
closer: ")",
|
|
51
|
+
messagePrefix: "{ error: ",
|
|
52
|
+
messageCloser: " })",
|
|
53
|
+
};
|
|
20
54
|
}
|
|
21
|
-
return
|
|
55
|
+
return {
|
|
56
|
+
opener: `.multipleOf(${json}`,
|
|
57
|
+
closer: ")",
|
|
58
|
+
messagePrefix: ", { error: ",
|
|
59
|
+
messageCloser: " })",
|
|
60
|
+
};
|
|
22
61
|
});
|
|
23
62
|
if (typeof schema.minimum === "number") {
|
|
24
63
|
if (schema.exclusiveMinimum === true) {
|
|
25
|
-
r += withMessage(schema, "minimum", ({ json }) =>
|
|
26
|
-
`.gt(${json}`,
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
64
|
+
r += (0, withMessage_js_1.withMessage)(schema, "minimum", ({ json }) => ({
|
|
65
|
+
opener: `.gt(${json}`,
|
|
66
|
+
closer: ")",
|
|
67
|
+
messagePrefix: ", { error: ",
|
|
68
|
+
messageCloser: " })",
|
|
69
|
+
}));
|
|
30
70
|
}
|
|
31
71
|
else {
|
|
32
|
-
r += withMessage(schema, "minimum", ({ json }) =>
|
|
33
|
-
`.gte(${json}`,
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
72
|
+
r += (0, withMessage_js_1.withMessage)(schema, "minimum", ({ json }) => ({
|
|
73
|
+
opener: `.gte(${json}`,
|
|
74
|
+
closer: ")",
|
|
75
|
+
messagePrefix: ", { error: ",
|
|
76
|
+
messageCloser: " })",
|
|
77
|
+
}));
|
|
37
78
|
}
|
|
38
79
|
}
|
|
39
80
|
else if (typeof schema.exclusiveMinimum === "number") {
|
|
40
|
-
r += withMessage(schema, "exclusiveMinimum", ({ json }) =>
|
|
41
|
-
`.gt(${json}`,
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
|
|
81
|
+
r += (0, withMessage_js_1.withMessage)(schema, "exclusiveMinimum", ({ json }) => ({
|
|
82
|
+
opener: `.gt(${json}`,
|
|
83
|
+
closer: ")",
|
|
84
|
+
messagePrefix: ", { error: ",
|
|
85
|
+
messageCloser: " })",
|
|
86
|
+
}));
|
|
45
87
|
}
|
|
46
88
|
if (typeof schema.maximum === "number") {
|
|
47
89
|
if (schema.exclusiveMaximum === true) {
|
|
48
|
-
r += withMessage(schema, "maximum", ({ json }) =>
|
|
49
|
-
`.lt(${json}`,
|
|
50
|
-
|
|
51
|
-
"
|
|
52
|
-
|
|
90
|
+
r += (0, withMessage_js_1.withMessage)(schema, "maximum", ({ json }) => ({
|
|
91
|
+
opener: `.lt(${json}`,
|
|
92
|
+
closer: ")",
|
|
93
|
+
messagePrefix: ", { error: ",
|
|
94
|
+
messageCloser: " })",
|
|
95
|
+
}));
|
|
53
96
|
}
|
|
54
97
|
else {
|
|
55
|
-
r += withMessage(schema, "maximum", ({ json }) =>
|
|
56
|
-
`.lte(${json}`,
|
|
57
|
-
|
|
58
|
-
"
|
|
59
|
-
|
|
98
|
+
r += (0, withMessage_js_1.withMessage)(schema, "maximum", ({ json }) => ({
|
|
99
|
+
opener: `.lte(${json}`,
|
|
100
|
+
closer: ")",
|
|
101
|
+
messagePrefix: ", { error: ",
|
|
102
|
+
messageCloser: " })",
|
|
103
|
+
}));
|
|
60
104
|
}
|
|
61
105
|
}
|
|
62
106
|
else if (typeof schema.exclusiveMaximum === "number") {
|
|
63
|
-
r += withMessage(schema, "exclusiveMaximum", ({ json }) =>
|
|
64
|
-
`.lt(${json}`,
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
|
|
107
|
+
r += (0, withMessage_js_1.withMessage)(schema, "exclusiveMaximum", ({ json }) => ({
|
|
108
|
+
opener: `.lt(${json}`,
|
|
109
|
+
closer: ")",
|
|
110
|
+
messagePrefix: ", { error: ",
|
|
111
|
+
messageCloser: " })",
|
|
112
|
+
}));
|
|
68
113
|
}
|
|
69
114
|
return r;
|
|
70
115
|
};
|
|
116
|
+
exports.parseNumber = parseNumber;
|