schema-truth 0.7.0 → 0.8.1
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/dist/{ajv-DTP2FXRr.js → ajv-DS9gtNZC.js} +1 -1
- package/dist/ajv.d.ts +1 -1
- package/dist/ajv.js +1 -1
- package/dist/{arktype-BdvvaBQX.js → arktype-CRZ8vXZ5.js} +94 -37
- package/dist/arktype.d.ts +3 -3
- package/dist/arktype.js +1 -1
- package/dist/cli/ajv.js +2 -2
- package/dist/cli/arktype.js +2 -2
- package/dist/cli/effect.js +2 -2
- package/dist/cli/typebox.js +2 -2
- package/dist/cli/valibot.js +2 -2
- package/dist/cli/yup.js +2 -2
- package/dist/cli/zod.js +2 -2
- package/dist/{cli-CL2mSark.js → cli-eAY8LqwP.js} +2 -2
- package/dist/{effect-mUknRPv_.js → effect-BkWovUi3.js} +52 -19
- package/dist/effect.d.ts +3 -3
- package/dist/effect.js +1 -1
- package/dist/{helper-Cfvb5UDq.js → helper-CbeedY35.js} +721 -38
- package/dist/{index-C2L8Ty0-.d.ts → index-DYzAccyT.d.ts} +29 -25
- package/dist/{index-DbV3dEmN.d.ts → index-G7UqNQ4w.d.ts} +54 -2
- package/dist/index.d.ts +54 -25
- package/dist/index.js +9 -9
- package/dist/{typebox-BdWJv3ND.js → typebox-wYeJqy7T.js} +33 -14
- package/dist/typebox.d.ts +3 -3
- package/dist/typebox.js +1 -1
- package/dist/{utils-WNBq1HR0.js → utils-Bky98UmP.js} +160 -31
- package/dist/{valibot-CaJIynCH.js → valibot-DQUEDYKt.js} +64 -23
- package/dist/valibot.d.ts +3 -3
- package/dist/valibot.js +1 -1
- package/dist/{yup-CM2TSFig.js → yup-BMpa9Co1.js} +2 -2
- package/dist/yup.d.ts +2 -2
- package/dist/yup.js +1 -1
- package/dist/{zod-BsXLZfwK.js → zod-Dq9LtqPL.js} +130 -91
- package/dist/zod.d.ts +3 -3
- package/dist/zod.js +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as isSchemaObject, D as isComposite, F as isPrimitive, H as isStringWireLocation, M as isNullable, N as isObjectPrototypeProperty,
|
|
2
|
-
import { A as
|
|
1
|
+
import { At as UNSAFE_GENERATED_MARKER, B as isSchemaObject, D as isComposite, F as isPrimitive, H as isStringWireLocation, M as isNullable, N as isObjectPrototypeProperty, P as isPlainObject, S as hasFixedLength, T as hasType, V as isShapelessMember, X as DEFAULT_ROOT_NAME, b as hasConditional, c as normalizeTypes, g as zodErrorPart, h as zodError, j as isNullTypeMember, k as isDefaultOnlyMember, m as zodBaseError, o as issueMessageExpression, s as makeSafeKey, st as ISO_4217_CURRENCY_CODES, y as hasCombinator } from "./utils-Bky98UmP.js";
|
|
2
|
+
import { A as wireNullText, B as emitOptionsOf, C as isPrimitiveUnion, G as schemaNamer, H as planDocument, O as unionPrimitiveTypes, P as timePattern, Q as findCodeExtensionKeysInSchema, S as isNullTextRead, U as renderDefinitions, W as rootNameOf, Y as itemsSchemaOf, at as regexLiteral, b as integerWirePattern, c as withPolymorphicBases, d as propertyEmitOptions, et as constraintMessage, f as withoutParameterSerialization, g as hoistNestedRefs, h as valueListLiteral, i as isEvaluatedKeyExpression, j as wireNumericKind, k as wireArrayOnce, l as hasParameterSerialization, m as notPredicate, nt as foldAllOfMembers, o as typeDeclaration, p as isBigintFormat, q as hasRootSelfReference, r as evaluatedProperties, rt as isKeywordOnlyMember, s as refTarget, t as zodWrap, tt as constraintPredicate, u as parameterReaders, w as numberWirePattern, x as isNullMatchedFirst, y as impliedWireType, z as definitionsOf } from "./helper-CbeedY35.js";
|
|
3
3
|
//#region src/generator/zod/type.ts
|
|
4
4
|
/** The TypeScript type declared beside a recursive Zod schema. */
|
|
5
5
|
function type(schema, rootName = DEFAULT_ROOT_NAME) {
|
|
@@ -9,6 +9,71 @@ function type(schema, rootName = DEFAULT_ROOT_NAME) {
|
|
|
9
9
|
}, rootName);
|
|
10
10
|
}
|
|
11
11
|
//#endregion
|
|
12
|
+
//#region src/generator/zod/number.ts
|
|
13
|
+
/**
|
|
14
|
+
* Wire text checked against the integer or JSON number grammar, then read with `Number`.
|
|
15
|
+
* `z.coerce.number()` uses `Number` on its own, which accepts `""`, `" "` and `"0x10"`.
|
|
16
|
+
*/
|
|
17
|
+
function zodWireNumber(kind, errorArg = "") {
|
|
18
|
+
return `z.string().regex(${kind === "integer" ? integerWirePattern() : numberWirePattern()}${errorArg === "" ? "" : `,${errorArg}`}).transform((val)=>Number(val))`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Generates a Zod schema for number types (number / float32 / float64), with
|
|
22
|
+
* min/max/multipleOf constraints and `x-*-message` vendor extensions translated
|
|
23
|
+
* to Zod v4 `{error: "msg"}` parameters.
|
|
24
|
+
*/
|
|
25
|
+
function number(schema, options) {
|
|
26
|
+
const isWire = options?.wire === true;
|
|
27
|
+
const errorMessage = schema["x-error-message"];
|
|
28
|
+
const requiredMessage = schema["x-required-message"];
|
|
29
|
+
const isCoerce = schema["x-coerce"] === true && !isWire;
|
|
30
|
+
const baseErrorArg = zodBaseError(errorMessage, isCoerce || isWire ? void 0 : requiredMessage);
|
|
31
|
+
const isFloat32 = schema.format === "float" || schema.format === "float32";
|
|
32
|
+
const isFloat64 = schema.format === "float64";
|
|
33
|
+
const base = isFloat32 ? `z.float32(${baseErrorArg})` : isFloat64 ? `z.float64(${baseErrorArg})` : isCoerce ? `z.coerce.number(${baseErrorArg})` : `z.number(${baseErrorArg})`;
|
|
34
|
+
const minimumMessage = schema["x-minimum-message"];
|
|
35
|
+
const exclusiveMinMessage = schema["x-exclusiveMinimum-message"];
|
|
36
|
+
const minErrorArg = minimumMessage ? zodError(minimumMessage) : "";
|
|
37
|
+
const minErrorPart = minErrorArg ? `,${minErrorArg}` : "";
|
|
38
|
+
const exMinErrorArg = exclusiveMinMessage ? zodError(exclusiveMinMessage) : "";
|
|
39
|
+
const exMinErrorPart = exMinErrorArg ? `,${exMinErrorArg}` : "";
|
|
40
|
+
const minimum = (() => {
|
|
41
|
+
if (schema.minimum !== void 0) {
|
|
42
|
+
if (schema.minimum === 0 && schema.exclusiveMinimum === true) return `.positive(${exMinErrorArg})`;
|
|
43
|
+
if (schema.minimum === 0 && schema.exclusiveMinimum === false) return `.nonnegative(${minErrorArg})`;
|
|
44
|
+
if (schema.exclusiveMinimum === true) return `.gt(${schema.minimum}${exMinErrorPart})`;
|
|
45
|
+
return `.min(${schema.minimum}${minErrorPart})`;
|
|
46
|
+
}
|
|
47
|
+
if (typeof schema.exclusiveMinimum === "number") return `.gt(${schema.exclusiveMinimum}${exMinErrorPart})`;
|
|
48
|
+
})();
|
|
49
|
+
const maximumMessage = schema["x-maximum-message"];
|
|
50
|
+
const exclusiveMaxMessage = schema["x-exclusiveMaximum-message"];
|
|
51
|
+
const maxErrorArg = maximumMessage ? zodError(maximumMessage) : "";
|
|
52
|
+
const maxErrorPart = maxErrorArg ? `,${maxErrorArg}` : "";
|
|
53
|
+
const exMaxErrorArg = exclusiveMaxMessage ? zodError(exclusiveMaxMessage) : "";
|
|
54
|
+
const exMaxErrorPart = exMaxErrorArg ? `,${exMaxErrorArg}` : "";
|
|
55
|
+
const maximum = (() => {
|
|
56
|
+
if (schema.maximum !== void 0) {
|
|
57
|
+
if (schema.maximum === 0 && schema.exclusiveMaximum === true) return `.negative(${exMaxErrorArg})`;
|
|
58
|
+
if (schema.maximum === 0 && schema.exclusiveMaximum === false) return `.nonpositive(${maxErrorArg})`;
|
|
59
|
+
if (schema.exclusiveMaximum === true) return `.lt(${schema.maximum}${exMaxErrorPart})`;
|
|
60
|
+
return `.max(${schema.maximum}${maxErrorPart})`;
|
|
61
|
+
}
|
|
62
|
+
if (typeof schema.exclusiveMaximum === "number") return `.lt(${schema.exclusiveMaximum}${exMaxErrorPart})`;
|
|
63
|
+
})();
|
|
64
|
+
const multipleOfMessage = schema["x-multipleOf-message"];
|
|
65
|
+
const multipleOfErrorArg = multipleOfMessage ? `,${zodError(multipleOfMessage)}` : baseErrorArg ? `,${baseErrorArg}` : "";
|
|
66
|
+
const innerChain = [
|
|
67
|
+
base,
|
|
68
|
+
minimum,
|
|
69
|
+
maximum,
|
|
70
|
+
schema.multipleOf !== void 0 ? `.multipleOf(${schema.multipleOf}${multipleOfErrorArg})` : void 0
|
|
71
|
+
].filter((v) => v !== void 0).join("");
|
|
72
|
+
if (isWire) return `${zodWireNumber("number", baseErrorArg)}.pipe(${innerChain})`;
|
|
73
|
+
if (isCoerce && (isFloat32 || isFloat64)) return `z.coerce.number(${baseErrorArg}).pipe(${innerChain})`;
|
|
74
|
+
return innerChain;
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
12
77
|
//#region src/generator/zod/integer.ts
|
|
13
78
|
/** Whether a node is a 64-bit integer: a value beyond 2^53 that Zod reads as a bigint. */
|
|
14
79
|
function isInt64(schema) {
|
|
@@ -29,15 +94,17 @@ function int64Input(isWire, baseErrorArg = "") {
|
|
|
29
94
|
* min/max/multipleOf constraints and `x-*-message` vendor extensions translated
|
|
30
95
|
* to Zod v4 `{error: "msg"}` parameters.
|
|
31
96
|
*/
|
|
32
|
-
function integer(schema) {
|
|
97
|
+
function integer(schema, options) {
|
|
98
|
+
const isWire = options?.wire === true;
|
|
33
99
|
const errorMessage = schema["x-error-message"];
|
|
34
100
|
const requiredMessage = schema["x-required-message"];
|
|
35
|
-
const isCoerce = schema["x-coerce"] === true;
|
|
36
|
-
const
|
|
101
|
+
const isCoerce = schema["x-coerce"] === true && !isWire;
|
|
102
|
+
const readsText = isCoerce || isWire;
|
|
103
|
+
const baseErrorArg = zodBaseError(errorMessage, readsText ? void 0 : requiredMessage);
|
|
37
104
|
const isBigint = schema.format === "bigint";
|
|
38
105
|
const isInt32 = schema.format === "int32" || schema.format === "uint32";
|
|
39
106
|
const is64 = isInt64(schema);
|
|
40
|
-
const bigintBase =
|
|
107
|
+
const bigintBase = readsText && isBigint;
|
|
41
108
|
const numberPipe = isCoerce && isInt32;
|
|
42
109
|
const numberChain = isCoerce && !isBigint && !is64 && !isInt32;
|
|
43
110
|
const base = bigintBase ? `z.coerce.bigint(${baseErrorArg})` : isInt32 ? `z.${schema.format === "uint32" ? "uint32" : "int32"}(${baseErrorArg})` : is64 ? `z.${schema.format === "uint64" ? "uint64" : "int64"}(${baseErrorArg})` : isBigint ? `z.bigint(${baseErrorArg})` : `z.int(${baseErrorArg})`;
|
|
@@ -77,22 +144,25 @@ function integer(schema) {
|
|
|
77
144
|
const multipleOfMessage = schema["x-multipleOf-message"] ?? errorMessage;
|
|
78
145
|
const multipleOfErrorArg = zodErrorPart(multipleOfMessage);
|
|
79
146
|
const multipleOf = typeof schema.multipleOf === "number" ? `.multipleOf(${lit(schema.multipleOf)}${multipleOfErrorArg})` : void 0;
|
|
147
|
+
const constraints = [
|
|
148
|
+
minimum,
|
|
149
|
+
maximum,
|
|
150
|
+
multipleOf
|
|
151
|
+
].filter((v) => v !== void 0).join("");
|
|
80
152
|
const innerChain = [
|
|
81
153
|
base,
|
|
82
154
|
minimum,
|
|
83
155
|
maximum,
|
|
84
156
|
multipleOf
|
|
85
157
|
].filter((v) => v !== void 0).join("");
|
|
86
|
-
if (
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
multipleOf
|
|
91
|
-
].filter((v) => v !== void 0).join("");
|
|
92
|
-
return `z.coerce.number(${baseErrorArg}).int(${errorMessage ? zodError(errorMessage) : ""})${constraints}`;
|
|
158
|
+
if (isWire && !isBigint && !is64) {
|
|
159
|
+
const intErrorArg = errorMessage ? zodError(errorMessage) : "";
|
|
160
|
+
const inner = isInt32 ? innerChain : `z.number(${baseErrorArg}).int(${intErrorArg})${constraints}`;
|
|
161
|
+
return `${zodWireNumber("integer", baseErrorArg)}.pipe(${inner})`;
|
|
93
162
|
}
|
|
163
|
+
if (numberChain) return `z.coerce.number(${baseErrorArg}).int(${errorMessage ? zodError(errorMessage) : ""})${constraints}`;
|
|
94
164
|
if (numberPipe) return `z.coerce.number(${baseErrorArg}).pipe(${innerChain})`;
|
|
95
|
-
if (is64) return `${int64Input(
|
|
165
|
+
if (is64) return `${int64Input(readsText, baseErrorArg)}.pipe(${innerChain})`;
|
|
96
166
|
return innerChain;
|
|
97
167
|
}
|
|
98
168
|
//#endregion
|
|
@@ -148,62 +218,6 @@ function _enum(schema) {
|
|
|
148
218
|
return outerLit(schema.enum[0]);
|
|
149
219
|
}
|
|
150
220
|
//#endregion
|
|
151
|
-
//#region src/generator/zod/number.ts
|
|
152
|
-
/**
|
|
153
|
-
* Generates a Zod schema for number types (number / float32 / float64), with
|
|
154
|
-
* min/max/multipleOf constraints and `x-*-message` vendor extensions translated
|
|
155
|
-
* to Zod v4 `{error: "msg"}` parameters.
|
|
156
|
-
*/
|
|
157
|
-
function number(schema) {
|
|
158
|
-
const errorMessage = schema["x-error-message"];
|
|
159
|
-
const requiredMessage = schema["x-required-message"];
|
|
160
|
-
const isCoerce = schema["x-coerce"] === true;
|
|
161
|
-
const baseErrorArg = zodBaseError(errorMessage, isCoerce ? void 0 : requiredMessage);
|
|
162
|
-
const isFloat32 = schema.format === "float" || schema.format === "float32";
|
|
163
|
-
const isFloat64 = schema.format === "float64";
|
|
164
|
-
const wirePipe = isCoerce && (isFloat32 || isFloat64);
|
|
165
|
-
const base = isCoerce && !isFloat32 && !isFloat64 ? `z.coerce.number(${baseErrorArg})` : isFloat32 ? `z.float32(${baseErrorArg})` : isFloat64 ? `z.float64(${baseErrorArg})` : `z.number(${baseErrorArg})`;
|
|
166
|
-
const minimumMessage = schema["x-minimum-message"];
|
|
167
|
-
const exclusiveMinMessage = schema["x-exclusiveMinimum-message"];
|
|
168
|
-
const minErrorArg = minimumMessage ? zodError(minimumMessage) : "";
|
|
169
|
-
const minErrorPart = minErrorArg ? `,${minErrorArg}` : "";
|
|
170
|
-
const exMinErrorArg = exclusiveMinMessage ? zodError(exclusiveMinMessage) : "";
|
|
171
|
-
const exMinErrorPart = exMinErrorArg ? `,${exMinErrorArg}` : "";
|
|
172
|
-
const minimum = (() => {
|
|
173
|
-
if (schema.minimum !== void 0) {
|
|
174
|
-
if (schema.minimum === 0 && schema.exclusiveMinimum === true) return `.positive(${exMinErrorArg})`;
|
|
175
|
-
if (schema.minimum === 0 && schema.exclusiveMinimum === false) return `.nonnegative(${minErrorArg})`;
|
|
176
|
-
if (schema.exclusiveMinimum === true) return `.gt(${schema.minimum}${exMinErrorPart})`;
|
|
177
|
-
return `.min(${schema.minimum}${minErrorPart})`;
|
|
178
|
-
}
|
|
179
|
-
if (typeof schema.exclusiveMinimum === "number") return `.gt(${schema.exclusiveMinimum}${exMinErrorPart})`;
|
|
180
|
-
})();
|
|
181
|
-
const maximumMessage = schema["x-maximum-message"];
|
|
182
|
-
const exclusiveMaxMessage = schema["x-exclusiveMaximum-message"];
|
|
183
|
-
const maxErrorArg = maximumMessage ? zodError(maximumMessage) : "";
|
|
184
|
-
const maxErrorPart = maxErrorArg ? `,${maxErrorArg}` : "";
|
|
185
|
-
const exMaxErrorArg = exclusiveMaxMessage ? zodError(exclusiveMaxMessage) : "";
|
|
186
|
-
const exMaxErrorPart = exMaxErrorArg ? `,${exMaxErrorArg}` : "";
|
|
187
|
-
const maximum = (() => {
|
|
188
|
-
if (schema.maximum !== void 0) {
|
|
189
|
-
if (schema.maximum === 0 && schema.exclusiveMaximum === true) return `.negative(${exMaxErrorArg})`;
|
|
190
|
-
if (schema.maximum === 0 && schema.exclusiveMaximum === false) return `.nonpositive(${maxErrorArg})`;
|
|
191
|
-
if (schema.exclusiveMaximum === true) return `.lt(${schema.maximum}${exMaxErrorPart})`;
|
|
192
|
-
return `.max(${schema.maximum}${maxErrorPart})`;
|
|
193
|
-
}
|
|
194
|
-
if (typeof schema.exclusiveMaximum === "number") return `.lt(${schema.exclusiveMaximum}${exMaxErrorPart})`;
|
|
195
|
-
})();
|
|
196
|
-
const multipleOfMessage = schema["x-multipleOf-message"];
|
|
197
|
-
const multipleOfErrorArg = multipleOfMessage ? `,${zodError(multipleOfMessage)}` : baseErrorArg ? `,${baseErrorArg}` : "";
|
|
198
|
-
const innerChain = [
|
|
199
|
-
base,
|
|
200
|
-
minimum,
|
|
201
|
-
maximum,
|
|
202
|
-
schema.multipleOf !== void 0 ? `.multipleOf(${schema.multipleOf}${multipleOfErrorArg})` : void 0
|
|
203
|
-
].filter((v) => v !== void 0).join("");
|
|
204
|
-
return wirePipe ? `z.coerce.number(${baseErrorArg}).pipe(${innerChain})` : innerChain;
|
|
205
|
-
}
|
|
206
|
-
//#endregion
|
|
207
221
|
//#region src/generator/zod/object.ts
|
|
208
222
|
/**
|
|
209
223
|
* A parameter object answers an `Object.prototype` name it was not sent with the inherited
|
|
@@ -248,7 +262,7 @@ function object(schema, rootName, isZod, options) {
|
|
|
248
262
|
const objectType = schema.additionalProperties === true ? "looseObject" : schema.additionalProperties === false || schema.unevaluatedProperties === false ? "strictObject" : conditionalKeysReferenced ? "looseObject" : "object";
|
|
249
263
|
const required = new Set(schema.required);
|
|
250
264
|
const props = Object.entries(schema.properties).map(([key, propSchema]) => {
|
|
251
|
-
const parsed = zod(propSchema, rootName, isZod, options);
|
|
265
|
+
const parsed = zod(propSchema, rootName, isZod, propertyEmitOptions(key, propSchema, options) ?? options);
|
|
252
266
|
if (!parsed) return null;
|
|
253
267
|
return `${makeSafeKey(key)}:${parsed}${required.has(key) ? "" : ".exactOptional()"}`;
|
|
254
268
|
}).filter((v) => v !== null);
|
|
@@ -524,11 +538,12 @@ const ZOD_ISSUE_CODES = [
|
|
|
524
538
|
* The `not` predicate of an inner schema, as the source of a `(val) => ...` arrow, or
|
|
525
539
|
* `undefined` when the inner schema has none this generator can express.
|
|
526
540
|
*/
|
|
527
|
-
function zodNotPredicate(inner) {
|
|
541
|
+
function zodNotPredicate(inner, parent) {
|
|
528
542
|
if (inner.$ref) return void 0;
|
|
529
|
-
const
|
|
543
|
+
const bigint = isBigintFormat(parent);
|
|
544
|
+
const shared = notPredicate(inner, "val", "val", bigint);
|
|
530
545
|
if (shared !== void 0) return shared;
|
|
531
|
-
if (Array.isArray(inner.enum)) return `(val) => !${
|
|
546
|
+
if (Array.isArray(inner.enum)) return `(val) => !${valueListLiteral(inner.enum, bigint)}.includes(val)`;
|
|
532
547
|
if (inner.oneOf || inner.anyOf || inner.allOf) return `(val) => !${zod(inner)}.safeParse(val).success`;
|
|
533
548
|
}
|
|
534
549
|
function elementMessageWrap(inner, msg) {
|
|
@@ -549,21 +564,38 @@ function zod(schema, rootName = DEFAULT_ROOT_NAME, isZod = false, options) {
|
|
|
549
564
|
const nullFirst = isNullMatchedFirst(schema) ? expression : `${expression}.nullable()`;
|
|
550
565
|
return `z.preprocess(${wireNullText("val")},${nullFirst})`;
|
|
551
566
|
}
|
|
567
|
+
/** A keyword-only `allOf` member: the keywords apply when the value has their type. */
|
|
568
|
+
function keywordConstraint(schema) {
|
|
569
|
+
return `z.any().refine((val) => ${constraintPredicate(schema, "val")}${zodErrorPart(constraintMessage(schema))})`;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* An `allOf` with `unevaluatedProperties` beside it: the members' objects strip what they
|
|
573
|
+
* do not declare, so the keys no member evaluates are checked on the input before it is
|
|
574
|
+
* parsed. `inner` as given when the keyword is absent or every key is evaluated.
|
|
575
|
+
*/
|
|
576
|
+
function unevaluatedPropertiesCheck(inner, schema, rootName, isZod, options) {
|
|
577
|
+
const unevaluated = schema.unevaluatedProperties;
|
|
578
|
+
if (unevaluated !== false && (typeof unevaluated !== "object" || unevaluated === null)) return inner;
|
|
579
|
+
const evaluated = evaluatedProperties(schema, options?.definitions);
|
|
580
|
+
if (evaluated === void 0) return inner;
|
|
581
|
+
const isEvaluated = isEvaluatedKeyExpression(evaluated, "k");
|
|
582
|
+
const message = schema["x-unevaluatedProperties-message"];
|
|
583
|
+
return `(()=>{const Schema=${inner};return z.unknown().check((ctx)=>{const o=ctx.value;if(typeof o!=='object'||o===null||Array.isArray(o))return;${unevaluated === false ? `for(const k of Object.keys(o)){if(${isEvaluated})continue;ctx.issues.push({code:'custom',input:o,path:[k],message:${message === void 0 ? "`Unrecognized key: \"${k}\"`" : JSON.stringify(message)}})}` : `const Unevaluated=${zod(unevaluated, rootName, isZod, options)};for(const [k,v] of Object.entries(o)){if(${isEvaluated})continue;const result=Unevaluated.safeParse(v);if(!result.success){for(const issue of result.error.issues){ctx.issues.push({code:'custom',input:v,path:[k,...issue.path],message:${message === void 0 ? "issue.message" : JSON.stringify(message)}})}}}`}}).pipe(Schema)})()`;
|
|
584
|
+
}
|
|
552
585
|
function node(schema, rootName, isZod, options) {
|
|
553
586
|
const isStringWireParam = isStringWireLocation(options?.paramIn);
|
|
554
587
|
const isWireCoerced = isStringWireParam && schema["x-coerce"] !== false;
|
|
555
|
-
const withForcedCoerce = (s) => isStringWireParam && s["x-coerce"] !== false ? {
|
|
556
|
-
...s,
|
|
557
|
-
"x-coerce": true
|
|
558
|
-
} : s;
|
|
559
588
|
const codeExtOpts = options?.unsafeCodeExtensions === true ? { unsafeCodeExtensions: true } : {};
|
|
560
|
-
const zodWrap$1 = (zodStr, s) => zodWrap(zodStr, s,
|
|
589
|
+
const zodWrap$1 = (zodStr, s) => zodWrap(zodStr, s, {
|
|
590
|
+
...codeExtOpts,
|
|
591
|
+
...options?.definitions !== void 0 && { definitions: options.definitions }
|
|
592
|
+
});
|
|
561
593
|
const readonly = (v) => options?.readonly ? `${v}.readonly()` : v;
|
|
562
594
|
const wireArray = (v) => isStringWireParam ? `z.preprocess(${wireArrayOnce("val")},${v})` : v;
|
|
563
595
|
const literalInput = (values) => {
|
|
564
596
|
if (values.every((v) => typeof v === "number")) {
|
|
565
597
|
if (isInt64(schema)) return int64Input(isWireCoerced);
|
|
566
|
-
if (isWireCoerced) return
|
|
598
|
+
if (isWireCoerced) return zodWireNumber(wireNumericKind(schema, values));
|
|
567
599
|
}
|
|
568
600
|
if (isWireCoerced && values.every((v) => typeof v === "boolean")) return "z.stringbool()";
|
|
569
601
|
};
|
|
@@ -604,20 +636,20 @@ function node(schema, rootName, isZod, options) {
|
|
|
604
636
|
if (schema.allOf.length === 0) return zodWrap$1("z.any()", schema);
|
|
605
637
|
const nullable = isNullable(schema) || schema.allOf.some(isNullTypeMember);
|
|
606
638
|
const defaultValue = schema.allOf.find(isDefaultOnlyMember)?.default;
|
|
607
|
-
const schemas = schema.allOf.filter((s) => !isShapelessMember(s)).map((s) => zod(s, rootName, isZod, options));
|
|
639
|
+
const schemas = foldAllOfMembers(schema.allOf.filter((s) => !isShapelessMember(s)), isWireCoerced).map((s) => isKeywordOnlyMember(s) ? keywordConstraint(s) : zod(s, rootName, isZod, options));
|
|
608
640
|
if (schemas.length === 0) return zodWrap$1("z.any()", {
|
|
609
641
|
...schema,
|
|
610
642
|
nullable
|
|
611
643
|
});
|
|
612
644
|
const intersected = schemas.length === 1 ? schemas[0] : schemas.reduce((acc, s) => `z.intersection(${acc},${s})`);
|
|
613
645
|
const allOfMessage = schema["x-allOf-message"];
|
|
614
|
-
const
|
|
646
|
+
const checked = unevaluatedPropertiesCheck(allOfMessage ? (() => {
|
|
615
647
|
const msgExpr = issueMessageExpression(allOfMessage);
|
|
616
648
|
const branches = ZOD_ISSUE_CODES.map((c, i) => `${i === 0 ? "" : "else "}if(issue.code==='${c}'){ctx.issues.push({...issue,input:issue.input,message:${msgExpr}})}`).join("");
|
|
617
649
|
return `(()=>{const Schema=${intersected};return z.unknown().check((ctx)=>{const result=Schema.safeParse(ctx.value);if(!result.success){for(const issue of result.error.issues){${branches}}}}).pipe(Schema)})()`;
|
|
618
|
-
})() : intersected;
|
|
650
|
+
})() : intersected, schema, rootName, isZod, options);
|
|
619
651
|
const mergedDefault = defaultValue !== void 0 ? defaultValue : schema.default;
|
|
620
|
-
return zodWrap$1(
|
|
652
|
+
return zodWrap$1(checked, {
|
|
621
653
|
...schema,
|
|
622
654
|
nullable,
|
|
623
655
|
...mergedDefault !== void 0 && { default: mergedDefault }
|
|
@@ -628,7 +660,7 @@ function node(schema, rootName, isZod, options) {
|
|
|
628
660
|
const errorPart = zodErrorPart(notMessage);
|
|
629
661
|
const { not: inner, ...withoutNot } = schema;
|
|
630
662
|
if (typeof inner !== "object" || inner === null) return zodWrap$1("z.any()", schema);
|
|
631
|
-
const predicate = inner.$ref ? `(val) => !${ref(inner, "", false)}.safeParse(val).success` : zodNotPredicate(inner);
|
|
663
|
+
const predicate = inner.$ref ? `(val) => !${ref(inner, "", false)}.safeParse(val).success` : zodNotPredicate(inner, withoutNot);
|
|
632
664
|
if (withoutNot.type !== void 0) {
|
|
633
665
|
const base = zod(withoutNot, rootName, isZod, options);
|
|
634
666
|
return predicate ? `${base}.refine(${predicate}${errorPart})` : base;
|
|
@@ -659,8 +691,8 @@ function node(schema, rootName, isZod, options) {
|
|
|
659
691
|
const types = normalizeTypes(schema.type);
|
|
660
692
|
const primitive = (t) => {
|
|
661
693
|
if (t === "string") return string(schema);
|
|
662
|
-
if (t === "number") return number(
|
|
663
|
-
if (t === "integer") return integer(
|
|
694
|
+
if (t === "number") return number(schema, { wire: isWireCoerced });
|
|
695
|
+
if (t === "integer") return integer(schema, { wire: isWireCoerced });
|
|
664
696
|
if (t !== "boolean") return void 0;
|
|
665
697
|
const errorMessage = schema["x-error-message"];
|
|
666
698
|
const errorArg = errorMessage ? zodError(errorMessage) : "";
|
|
@@ -744,6 +776,7 @@ function node(schema, rootName, isZod, options) {
|
|
|
744
776
|
const errorMessage = schema["x-error-message"];
|
|
745
777
|
return zodWrap$1(`z.null(${errorMessage ? zodError(errorMessage) : ""})`, schema);
|
|
746
778
|
}
|
|
779
|
+
if (typeof schema.additionalProperties === "object") return readonly(zodWrap$1(object(schema, rootName, isZod, options), schema));
|
|
747
780
|
return zodWrap$1("z.any()", schema);
|
|
748
781
|
}
|
|
749
782
|
//#endregion
|
|
@@ -752,12 +785,18 @@ function node(schema, rootName, isZod, options) {
|
|
|
752
785
|
* A whole JSON Schema document as a Zod module: imports, `$defs` in dependency order,
|
|
753
786
|
* the root export, and the inferred type.
|
|
754
787
|
*/
|
|
755
|
-
function schemaToZod(
|
|
788
|
+
function schemaToZod(input, options) {
|
|
789
|
+
const schema = withPolymorphicBases(hoistNestedRefs(input));
|
|
756
790
|
const exportType = options?.exportType ?? true;
|
|
757
|
-
const
|
|
758
|
-
const codeExtensionsPresent =
|
|
759
|
-
const toName = schemaNamer(
|
|
791
|
+
const baseOptions = emitOptionsOf(options);
|
|
792
|
+
const codeExtensionsPresent = baseOptions.unsafeCodeExtensions === true && findCodeExtensionKeysInSchema(schema).length > 0;
|
|
793
|
+
const toName = schemaNamer(baseOptions.openapi);
|
|
760
794
|
const plan = planDocument(schema, rootNameOf(schema, toName));
|
|
795
|
+
const definitions = definitionsOf(baseOptions, plan.definitions);
|
|
796
|
+
const emitOptions = definitions === void 0 ? baseOptions : {
|
|
797
|
+
...baseOptions,
|
|
798
|
+
definitions
|
|
799
|
+
};
|
|
761
800
|
const { rootName } = plan;
|
|
762
801
|
const needsTypeDef = plan.hasDefinitions || hasRootSelfReference(schema);
|
|
763
802
|
const typeDefsCode = needsTypeDef ? [`type _${rootName} = ${type(plan.root, rootName)}`, renderDefinitions(plan, toName, (def, name) => `type _${name} = ${type(def, name)}`)].filter(Boolean).join("\n\n") : "";
|
package/dist/zod.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { r as JSONSchema } from "./index-
|
|
2
|
-
import { n as GenerateOptions } from "./index-
|
|
1
|
+
import { r as JSONSchema } from "./index-G7UqNQ4w.js";
|
|
2
|
+
import { n as GenerateOptions } from "./index-DYzAccyT.js";
|
|
3
3
|
//#region src/generator/zod/index.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* A whole JSON Schema document as a Zod module: imports, `$defs` in dependency order,
|
|
6
6
|
* the root export, and the inferred type.
|
|
7
7
|
*/
|
|
8
|
-
export declare function schemaToZod(
|
|
8
|
+
export declare function schemaToZod(input: JSONSchema, options?: GenerateOptions): string;
|
|
9
9
|
//#endregion
|
package/dist/zod.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as schemaToZod } from "./zod-
|
|
1
|
+
export { t as schemaToZod } from "./zod-Dq9LtqPL.js";
|