schema-truth 0.8.1 → 0.8.3
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/README.md +1 -1
- package/dist/{ajv-DS9gtNZC.js → ajv-DejgT2o1.js} +8 -14
- package/dist/ajv.d.ts +2 -4
- package/dist/ajv.js +1 -1
- package/dist/{arktype-CRZ8vXZ5.js → arktype-CzTltPwP.js} +94 -55
- package/dist/arktype.d.ts +3 -5
- package/dist/arktype.js +1 -1
- package/dist/cli/ajv.js +3 -4
- package/dist/cli/arktype.js +3 -4
- package/dist/cli/effect.js +3 -4
- package/dist/cli/typebox.js +3 -4
- package/dist/cli/valibot.js +3 -4
- package/dist/cli/yup.js +3 -4
- package/dist/cli/zod.js +3 -4
- package/dist/{cli-eAY8LqwP.js → cli-71Np9i5L.js} +6 -9
- package/dist/{effect-BkWovUi3.js → effect-Dj8qARFi.js} +70 -44
- package/dist/effect.d.ts +3 -5
- package/dist/effect.js +1 -1
- package/dist/{helper-CbeedY35.js → helper-WSNbEQKr.js} +1398 -52
- package/dist/{index-G7UqNQ4w.d.ts → index-DLVjyrKu.d.ts} +0 -14
- package/dist/{index-DYzAccyT.d.ts → index-DU2Nd8eI.d.ts} +2 -6
- package/dist/index.d.ts +11 -20
- package/dist/index.js +10 -10
- package/dist/{parser-CyJ2auea.js → parser-RBrkz7ZL.js} +2 -4
- package/dist/{typebox-wYeJqy7T.js → typebox-RAJnIZwI.js} +60 -47
- package/dist/typebox.d.ts +3 -5
- package/dist/typebox.js +1 -1
- package/dist/{valibot-DQUEDYKt.js → valibot-CLUhG6tp.js} +66 -45
- package/dist/valibot.d.ts +3 -5
- package/dist/valibot.js +1 -1
- package/dist/{yup-BMpa9Co1.js → yup-DfE_ofEu.js} +9 -15
- package/dist/yup.d.ts +3 -5
- package/dist/yup.js +1 -1
- package/dist/{zod-Dq9LtqPL.js → zod-BhYAQ3iW.js} +84 -56
- package/dist/zod.d.ts +3 -5
- package/dist/zod.js +1 -1
- package/package.json +5 -5
- package/dist/utils-Bky98UmP.js +0 -1146
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { At as
|
|
2
|
-
|
|
3
|
-
//#region src/generator/zod/type.ts
|
|
1
|
+
import { A as wireArrayOnce, At as hasFixedLength, Bt as isObjectPrototypeProperty, C as isNullTextRead, Dt as hasCombinator, Et as zodErrorPart, F as timePattern, Ft as isComposite, G as renderDefinitions, H as emitOptionsOf, Ht as isPrimitive, I as deepEqualExpression, In as UNSAFE_GENERATED_MARKER, J as discriminatedMembers, Jt as isStringWireLocation, K as rootNameOf, Kt as isSchemaObject, Lt as isDefaultOnlyMember, M as wireNumericKind, Nt as hasType, Ot as hasConditional, Q as itemsSchemaOf, Rt as isNullTypeMember, S as isNullMatchedFirst, T as numberWirePattern, Tt as zodError, V as definitionsOf, Vt as isPlainObject, W as planDocument, X as hasRootSelfReference, _ as hoistNestedRefs, _t as makeSafeKey, at as constraintMessage, b as impliedWireType, c as refTarget, ct as isKeywordOnlyMember, d as parameterReaders, f as propertyEmitOptions, g as valueListLiteral, gt as issueMessageExpression, h as notPredicate, i as isEvaluatedKeyExpression, j as wireNullText, k as unionPrimitiveTypes, l as withPolymorphicBases, m as isBigintFormat, nn as DEFAULT_ROOT_NAME, o as typeDeclaration, ot as constraintPredicate, p as withoutParameterSerialization, pn as ISO_4217_CURRENCY_CODES, q as schemaNamer, qt as isShapelessMember, r as evaluatedProperties, rt as findCodeExtensionKeysInSchema, s as sanitizeSchema, st as foldAllOfMembers, t as zodWrap, u as hasParameterSerialization, ut as regexLiteral, vt as normalizeTypes, w as isPrimitiveUnion, wt as zodBaseError, x as integerWirePattern, zt as isNullable } from "./helper-WSNbEQKr.js";
|
|
2
|
+
// src/generator/zod/type.ts
|
|
4
3
|
/** The TypeScript type declared beside a recursive Zod schema. */
|
|
5
4
|
function type(schema, rootName = DEFAULT_ROOT_NAME) {
|
|
6
5
|
return typeDeclaration(schema, {
|
|
@@ -8,8 +7,7 @@ function type(schema, rootName = DEFAULT_ROOT_NAME) {
|
|
|
8
7
|
readonly: false
|
|
9
8
|
}, rootName);
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
//#region src/generator/zod/number.ts
|
|
10
|
+
// src/generator/zod/number.ts
|
|
13
11
|
/**
|
|
14
12
|
* Wire text checked against the integer or JSON number grammar, then read with `Number`.
|
|
15
13
|
* `z.coerce.number()` uses `Number` on its own, which accepts `""`, `" "` and `"0x10"`.
|
|
@@ -73,8 +71,7 @@ function number(schema, options) {
|
|
|
73
71
|
if (isCoerce && (isFloat32 || isFloat64)) return `z.coerce.number(${baseErrorArg}).pipe(${innerChain})`;
|
|
74
72
|
return innerChain;
|
|
75
73
|
}
|
|
76
|
-
|
|
77
|
-
//#region src/generator/zod/integer.ts
|
|
74
|
+
// src/generator/zod/integer.ts
|
|
78
75
|
/** Whether a node is a 64-bit integer: a value beyond 2^53 that Zod reads as a bigint. */
|
|
79
76
|
function isInt64(schema) {
|
|
80
77
|
return schema.format === "int64" || schema.format === "uint64";
|
|
@@ -165,8 +162,7 @@ function integer(schema, options) {
|
|
|
165
162
|
if (is64) return `${int64Input(readsText, baseErrorArg)}.pipe(${innerChain})`;
|
|
166
163
|
return innerChain;
|
|
167
164
|
}
|
|
168
|
-
|
|
169
|
-
//#region src/generator/zod/enum.ts
|
|
165
|
+
// src/generator/zod/enum.ts
|
|
170
166
|
/**
|
|
171
167
|
* Generates a Zod enum schema. String enums map to `z.enum`, number/integer/
|
|
172
168
|
* boolean enums to `z.union([z.literal(...), ...])`, array enums to `z.tuple`
|
|
@@ -217,8 +213,7 @@ function _enum(schema) {
|
|
|
217
213
|
if (schema.enum.length > 1) return `z.union([${schema.enum.map(innerLit).join(",")}]${errorArg})`;
|
|
218
214
|
return outerLit(schema.enum[0]);
|
|
219
215
|
}
|
|
220
|
-
|
|
221
|
-
//#region src/generator/zod/object.ts
|
|
216
|
+
// src/generator/zod/object.ts
|
|
222
217
|
/**
|
|
223
218
|
* A parameter object answers an `Object.prototype` name it was not sent with the inherited
|
|
224
219
|
* member. Copied onto a null-prototype object, it holds only what the request carried.
|
|
@@ -250,18 +245,22 @@ function object(schema, rootName, isZod, options) {
|
|
|
250
245
|
const depReqErrorArg = depReqMessage ? `,${zodError(depReqMessage)}` : errorArg;
|
|
251
246
|
const depSchMessage = schema["x-dependentSchemas-message"];
|
|
252
247
|
const depSchErrorArg = depSchMessage ? `,${zodError(depSchMessage)}` : errorArg;
|
|
253
|
-
if (typeof schema.additionalProperties === "object") return `${`z.record(z.string(),${zod(schema.additionalProperties, rootName, isZod, options)})`}${schema.propertyNames?.pattern ? `.refine((val)=>Object.keys(val).every((key)=>new RegExp(${JSON.stringify(schema.propertyNames.pattern)}).test(key))${propNamesErrorArg})` : schema.propertyNames?.enum ? `.refine((val)=>Object.keys(val).every((key)=>${JSON.stringify(schema.propertyNames.enum)}.includes(key))${propNamesErrorArg})` : ""}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
248
|
+
if (typeof schema.additionalProperties === "object" && schema.properties === void 0 && schema.patternProperties === void 0) return `${`z.record(z.string(),${zod(schema.additionalProperties, rootName, isZod, options)})`}${schema.propertyNames?.pattern ? `.refine((val)=>Object.keys(val).every((key)=>new RegExp(${JSON.stringify(schema.propertyNames.pattern)}).test(key))${propNamesErrorArg})` : schema.propertyNames?.enum ? `.refine((val)=>Object.keys(val).every((key)=>${JSON.stringify(schema.propertyNames.enum)}.includes(key))${propNamesErrorArg})` : ""}`;
|
|
249
|
+
const properties = schema.properties ?? {};
|
|
250
|
+
const undeclaredRequired = (schema.required ?? []).filter((key) => !Object.hasOwn(properties, key));
|
|
251
|
+
const unevaluated = schema.unevaluatedProperties;
|
|
252
|
+
const closed = schema.additionalProperties === false || unevaluated === false;
|
|
253
|
+
const closedByCheck = closed && schema.patternProperties !== void 0;
|
|
254
|
+
const extraSchema = typeof schema.additionalProperties === "object" ? schema.additionalProperties : typeof unevaluated === "object" ? unevaluated : void 0;
|
|
255
|
+
const extraMessage = typeof schema.additionalProperties === "object" ? schema["x-additionalProperties-message"] : schema["x-unevaluatedProperties-message"];
|
|
257
256
|
if (!schema.properties) {
|
|
258
257
|
if (schema.additionalProperties === true) return "z.any()";
|
|
259
|
-
return "z.object({})";
|
|
258
|
+
if (!(undeclaredRequired.length > 0 || schema.patternProperties !== void 0 || extraSchema !== void 0 || closed)) return "z.object({})";
|
|
260
259
|
}
|
|
261
|
-
const
|
|
262
|
-
const objectType = schema.additionalProperties === true ? "looseObject" :
|
|
260
|
+
const readsUndeclaredKeys = hasConditional(schema) || schema.patternProperties !== void 0 || schema.propertyNames !== void 0 || schema.minProperties !== void 0 || schema.maxProperties !== void 0 || schema.dependentRequired !== void 0 || schema.dependentSchemas !== void 0 || undeclaredRequired.length > 0 || extraSchema !== void 0 || closedByCheck;
|
|
261
|
+
const objectType = schema.additionalProperties === true ? "looseObject" : closed && !closedByCheck ? "strictObject" : readsUndeclaredKeys ? "looseObject" : "object";
|
|
263
262
|
const required = new Set(schema.required);
|
|
264
|
-
const props = Object.entries(
|
|
263
|
+
const props = Object.entries(properties).map(([key, propSchema]) => {
|
|
265
264
|
const parsed = zod(propSchema, rootName, isZod, propertyEmitOptions(key, propSchema, options) ?? options);
|
|
266
265
|
if (!parsed) return null;
|
|
267
266
|
return `${makeSafeKey(key)}:${parsed}${required.has(key) ? "" : ".exactOptional()"}`;
|
|
@@ -270,17 +269,21 @@ function object(schema, rootName, isZod, options) {
|
|
|
270
269
|
const objectParams = objectType === "strictObject" && strictMessage ? `,{error:(issue)=>issue.code==='unrecognized_keys'?${JSON.stringify(strictMessage)}:${errorMessage ? JSON.stringify(errorMessage) : "undefined"}}` : "";
|
|
271
270
|
const rawBase = `z.${objectType}({${props.join(",")}}${objectParams})`;
|
|
272
271
|
const propsMessage = schema["x-properties-message"];
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
272
|
+
const partialBase = propsMessage ? nestedIssueMessageWrap(rawBase, propsMessage) : rawBase;
|
|
273
|
+
const minProperties = typeof schema.minProperties === "number" ? `.refine((val)=>Object.keys(val).length>=${schema.minProperties}${minErrorArg})` : "";
|
|
274
|
+
const maxProperties = typeof schema.maxProperties === "number" ? `.refine((val)=>Object.keys(val).length<=${schema.maxProperties}${maxErrorArg})` : "";
|
|
275
|
+
const propertyNames = schema.propertyNames?.pattern ? `.refine((val)=>Object.keys(val).every((key)=>new RegExp(${JSON.stringify(schema.propertyNames.pattern)}).test(key))${propNamesErrorArg})` : schema.propertyNames?.enum ? `.refine((val)=>Object.keys(val).every((key)=>${JSON.stringify(schema.propertyNames.enum)}.includes(key))${propNamesErrorArg})` : "";
|
|
276
|
+
const patternProperties = schema.patternProperties ? Object.entries(schema.patternProperties).map(([pattern, propSchema]) => {
|
|
277
277
|
const z = zod(propSchema, rootName, isZod, options);
|
|
278
278
|
return `.refine((val)=>Object.entries(val).every(([key,value])=>!new RegExp(${JSON.stringify(pattern)}).test(key)||${z}.safeParse(value).success)${patternErrorArg})`;
|
|
279
|
-
}).join("") : ""
|
|
279
|
+
}).join("") : "";
|
|
280
|
+
const dependentRequired = schema.dependentRequired ? Object.entries(schema.dependentRequired).map(([key, deps]) => {
|
|
280
281
|
return `.refine((val)=>!('${key}' in val)||(${deps.map((d) => `'${d}' in val`).join("&&")})${depReqErrorArg})`;
|
|
281
|
-
}).join("") : ""
|
|
282
|
+
}).join("") : "";
|
|
283
|
+
const dependentSchemas = schema.dependentSchemas ? Object.entries(schema.dependentSchemas).map(([key, subSchema]) => {
|
|
282
284
|
return `.refine((val)=>!('${key}' in val)||${zod(subSchema, rootName, isZod, options)}.safeParse(val).success${depSchErrorArg})`;
|
|
283
|
-
}).join("") : ""
|
|
285
|
+
}).join("") : "";
|
|
286
|
+
const ifThenElse = (() => {
|
|
284
287
|
if (!schema.if) return "";
|
|
285
288
|
const ifS = zod(schema.if, rootName, isZod, options);
|
|
286
289
|
const thenS = schema.then ? zod(schema.then, rootName, isZod, options) : void 0;
|
|
@@ -299,12 +302,17 @@ function object(schema, rootName, isZod, options) {
|
|
|
299
302
|
parts.push(`.refine((val)=>${ifS}.safeParse(val).success||${elseS}.safeParse(val).success${arg})`);
|
|
300
303
|
}
|
|
301
304
|
return parts.join("");
|
|
302
|
-
})()
|
|
303
|
-
const
|
|
305
|
+
})();
|
|
306
|
+
const requiredPresent = undeclaredRequired.length > 0 ? `.refine((val)=>${JSON.stringify(undeclaredRequired)}.every((key)=>key in val)${errorArg})` : "";
|
|
307
|
+
const isEvaluated = isEvaluatedKeyExpression({
|
|
308
|
+
names: Object.keys(properties),
|
|
309
|
+
patterns: Object.keys(schema.patternProperties ?? {})
|
|
310
|
+
}, "key");
|
|
311
|
+
const objectExpr = `${partialBase}${minProperties}${maxProperties}${propertyNames}${patternProperties}${requiredPresent}${closedByCheck ? `.refine((val)=>Object.keys(val).every((key)=>${isEvaluated})${zodErrorPart(strictMessage ?? errorMessage)})` : extraSchema !== void 0 ? `.refine((val)=>Object.entries(val).every(([key,value])=>${isEvaluated}||${zod(extraSchema, rootName, isZod, options)}.safeParse(value).success)${zodErrorPart(extraMessage ?? errorMessage)})` : ""}${dependentRequired}${dependentSchemas}${ifThenElse}`;
|
|
312
|
+
const hasInheritedName = Object.keys(properties).some(isObjectPrototypeProperty);
|
|
304
313
|
return isStringWireLocation(options?.paramIn) && hasInheritedName ? `z.preprocess(${OWN_PROPERTIES_PREPROCESS},${objectExpr})` : objectExpr;
|
|
305
314
|
}
|
|
306
|
-
|
|
307
|
-
//#region src/generator/zod/parameter.ts
|
|
315
|
+
// src/generator/zod/parameter.ts
|
|
308
316
|
/**
|
|
309
317
|
* Wraps the root expression of a parameter's schema in what reads the parameter's
|
|
310
318
|
* serialization: `allowEmptyValue`, a JSON `content`, or its `style` / `explode`.
|
|
@@ -314,8 +322,7 @@ function object(schema, rootName, isZod, options) {
|
|
|
314
322
|
function parameter(schema, inner, options) {
|
|
315
323
|
return parameterReaders(schema, options, "val").reduceRight((acc, step) => `z.preprocess(${step},${acc})`, inner);
|
|
316
324
|
}
|
|
317
|
-
|
|
318
|
-
//#region src/generator/zod/string.ts
|
|
325
|
+
// src/generator/zod/string.ts
|
|
319
326
|
/** The formats Zod checks with a top-level validator, keyed by the lower-cased format. */
|
|
320
327
|
const FORMAT_STRING = {
|
|
321
328
|
email: "email()",
|
|
@@ -515,8 +522,7 @@ function string(schema) {
|
|
|
515
522
|
if (transforms !== "") return `z.string(${baseErrorArg})${transforms}.pipe(${formatBase}${restChecks})`;
|
|
516
523
|
return `${formatBase}${restChecks}`;
|
|
517
524
|
}
|
|
518
|
-
|
|
519
|
-
//#region src/generator/zod/zod.ts
|
|
525
|
+
// src/generator/zod/zod.ts
|
|
520
526
|
/**
|
|
521
527
|
* Zod v4 issue codes, in the order `zod/v4/core/errors.d.ts` declares them. The
|
|
522
528
|
* `x-allOf-message` wrapper matches each in turn and replaces its message.
|
|
@@ -546,8 +552,19 @@ function zodNotPredicate(inner, parent) {
|
|
|
546
552
|
if (Array.isArray(inner.enum)) return `(val) => !${valueListLiteral(inner.enum, bigint)}.includes(val)`;
|
|
547
553
|
if (inner.oneOf || inner.anyOf || inner.allOf) return `(val) => !${zod(inner)}.safeParse(val).success`;
|
|
548
554
|
}
|
|
555
|
+
/**
|
|
556
|
+
* Re-push `issue` into `ctx` with `message`. Each issue code is narrowed first: the union of
|
|
557
|
+
* parsed issues does not type-check as a raw issue, while each member does.
|
|
558
|
+
*/
|
|
559
|
+
function forwardIssue(message) {
|
|
560
|
+
return ZOD_ISSUE_CODES.map((c, i) => `${i === 0 ? "" : "else "}if(issue.code==='${c}'){ctx.issues.push({...issue,input:issue.input,message:${message}})}`).join("");
|
|
561
|
+
}
|
|
562
|
+
/** A message for the issues under the value (with a path); the root's own issues keep theirs. */
|
|
563
|
+
function nestedIssueMessageWrap(inner, msg) {
|
|
564
|
+
return `(()=>{const Schema=${inner};return z.unknown().check((ctx)=>{const result=Schema.safeParse(ctx.value);if(!result.success){for(const issue of result.error.issues){${forwardIssue(`issue.path.length>0?${issueMessageExpression(msg)}:issue.message`)}}}}).pipe(Schema)})()`;
|
|
565
|
+
}
|
|
549
566
|
function elementMessageWrap(inner, msg) {
|
|
550
|
-
return
|
|
567
|
+
return nestedIssueMessageWrap(inner, msg);
|
|
551
568
|
}
|
|
552
569
|
/**
|
|
553
570
|
* Generate Zod schema code from JSON Schema.
|
|
@@ -568,19 +585,26 @@ function zod(schema, rootName = DEFAULT_ROOT_NAME, isZod = false, options) {
|
|
|
568
585
|
function keywordConstraint(schema) {
|
|
569
586
|
return `z.any().refine((val) => ${constraintPredicate(schema, "val")}${zodErrorPart(constraintMessage(schema))})`;
|
|
570
587
|
}
|
|
588
|
+
/** Push a key's issues (`result`, for the value `v` under key `k`) under that key. */
|
|
589
|
+
function forwardKeyIssues(message) {
|
|
590
|
+
return `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)}})}`;
|
|
591
|
+
}
|
|
571
592
|
/**
|
|
572
|
-
* An `allOf` with `unevaluatedProperties` beside it: the members'
|
|
573
|
-
* do not declare, so the keys
|
|
574
|
-
* parsed
|
|
593
|
+
* An `allOf` with `patternProperties` or `unevaluatedProperties` beside it: the members'
|
|
594
|
+
* objects strip what they do not declare, so the keys are checked on the input before it
|
|
595
|
+
* is parsed — a key a pattern matches against that pattern's schema, a key no member
|
|
596
|
+
* evaluates against `unevaluatedProperties`. `inner` as given when there is nothing to check.
|
|
575
597
|
*/
|
|
576
|
-
function
|
|
598
|
+
function objectKeysCheck(inner, schema, rootName, isZod, options) {
|
|
599
|
+
const patterns = Object.entries(schema.patternProperties ?? {});
|
|
577
600
|
const unevaluated = schema.unevaluatedProperties;
|
|
578
|
-
|
|
579
|
-
const evaluated = evaluatedProperties(schema, options?.definitions);
|
|
580
|
-
if (evaluated === void 0) return inner;
|
|
581
|
-
const
|
|
601
|
+
const unevaluatedSchema = typeof unevaluated === "object" ? unevaluated : void 0;
|
|
602
|
+
const evaluated = unevaluated === false || unevaluatedSchema !== void 0 ? evaluatedProperties(schema, options?.definitions) : void 0;
|
|
603
|
+
if (patterns.length === 0 && evaluated === void 0) return inner;
|
|
604
|
+
const patternMessage = schema["x-patternProperties-message"];
|
|
605
|
+
const patternChecks = patterns.map(([pattern, propSchema]) => `if(new RegExp(${JSON.stringify(pattern)}).test(k)){const result=${zod(propSchema, rootName, isZod, options)}.safeParse(v);if(!result.success){${forwardKeyIssues(patternMessage)}}}`).join("");
|
|
582
606
|
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
|
|
607
|
+
return `(()=>{const Schema=${inner};return z.unknown().check((ctx)=>{const o=ctx.value;if(typeof o!=='object'||o===null||Array.isArray(o))return;for(const [k,v] of Object.entries(o)){${patternChecks}${evaluated === void 0 ? "" : `if(!(${isEvaluatedKeyExpression(evaluated, "k")})){${unevaluatedSchema === void 0 ? `ctx.issues.push({code:'custom',input:o,path:[k],message:${message === void 0 ? "`Unrecognized key: \"${k}\"`" : JSON.stringify(message)}})` : `const result=${zod(unevaluatedSchema, rootName, isZod, options)}.safeParse(v);if(!result.success){${forwardKeyIssues(message)}}`}}`}}}).pipe(Schema)})()`;
|
|
584
608
|
}
|
|
585
609
|
function node(schema, rootName, isZod, options) {
|
|
586
610
|
const isStringWireParam = isStringWireLocation(options?.paramIn);
|
|
@@ -618,11 +642,18 @@ function node(schema, rootName, isZod, options) {
|
|
|
618
642
|
}, rootName, isZod, options);
|
|
619
643
|
if (schema.oneOf) {
|
|
620
644
|
if (schema.oneOf.length === 0) return zodWrap$1("z.any()", schema);
|
|
621
|
-
const schemas = schema.oneOf.map((s) => zod(s, rootName, isZod, options));
|
|
622
|
-
const oneOfMessage = schema["x-oneOf-message"];
|
|
623
|
-
const errorPart = zodErrorPart(oneOfMessage);
|
|
624
645
|
const discriminator = schema.discriminator?.propertyName;
|
|
625
646
|
const hasRefOrAllOf = schema.oneOf.some((s) => s.$ref !== void 0 || s.allOf !== void 0);
|
|
647
|
+
const discriminated = hasRefOrAllOf ? discriminatedMembers(schema, schema.oneOf) : void 0;
|
|
648
|
+
const schemas = schema.oneOf.map((s, i) => {
|
|
649
|
+
const member = zod(s, rootName, isZod, options);
|
|
650
|
+
const values = discriminated?.members[i]?.values;
|
|
651
|
+
if (discriminated === void 0 || values === void 0) return member;
|
|
652
|
+
const literal = values.length === 1 ? `z.literal(${JSON.stringify(values[0])})` : `z.enum(${JSON.stringify(values)})`;
|
|
653
|
+
return `z.intersection(${member},z.object({${makeSafeKey(discriminated.propertyName)}:${literal}}))`;
|
|
654
|
+
});
|
|
655
|
+
const oneOfMessage = schema["x-oneOf-message"];
|
|
656
|
+
const errorPart = zodErrorPart(oneOfMessage);
|
|
626
657
|
return zodWrap$1(discriminator && !hasRefOrAllOf ? `z.discriminatedUnion('${discriminator}',[${schemas.join(",")}]${errorPart})` : isWireCoerced ? `z.union([${schemas.join(",")}]${errorPart})` : `z.xor([${schemas.join(",")}]${errorPart})`, schema);
|
|
627
658
|
}
|
|
628
659
|
if (schema.anyOf) {
|
|
@@ -643,9 +674,8 @@ function node(schema, rootName, isZod, options) {
|
|
|
643
674
|
});
|
|
644
675
|
const intersected = schemas.length === 1 ? schemas[0] : schemas.reduce((acc, s) => `z.intersection(${acc},${s})`);
|
|
645
676
|
const allOfMessage = schema["x-allOf-message"];
|
|
646
|
-
const checked =
|
|
647
|
-
const
|
|
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("");
|
|
677
|
+
const checked = objectKeysCheck(allOfMessage ? (() => {
|
|
678
|
+
const branches = forwardIssue(issueMessageExpression(allOfMessage));
|
|
649
679
|
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)})()`;
|
|
650
680
|
})() : intersected, schema, rootName, isZod, options);
|
|
651
681
|
const mergedDefault = defaultValue !== void 0 ? defaultValue : schema.default;
|
|
@@ -672,7 +702,7 @@ function node(schema, rootName, isZod, options) {
|
|
|
672
702
|
const value = schema.const;
|
|
673
703
|
const constMessage = schema["x-const-message"] ?? schema["x-error-message"];
|
|
674
704
|
const errorPart = zodErrorPart(constMessage);
|
|
675
|
-
const literal = typeof value === "number" && isInt64(schema) ? `z.literal(${value}n${errorPart})` : isPrimitive(value) ? `z.literal(${JSON.stringify(value)}${errorPart})` : `z.custom<${JSON.stringify(value)}>()`;
|
|
705
|
+
const literal = typeof value === "number" && isInt64(schema) ? `z.literal(${value}n${errorPart})` : isPrimitive(value) ? `z.literal(${JSON.stringify(value)}${errorPart})` : `z.custom<${JSON.stringify(value)}>((input) => ${deepEqualExpression(value, "input")}${errorPart})`;
|
|
676
706
|
const input = literalInput([value]);
|
|
677
707
|
return zodWrap$1(input ? `${input}.pipe(${literal})` : literal, schema);
|
|
678
708
|
}
|
|
@@ -779,14 +809,13 @@ function node(schema, rootName, isZod, options) {
|
|
|
779
809
|
if (typeof schema.additionalProperties === "object") return readonly(zodWrap$1(object(schema, rootName, isZod, options), schema));
|
|
780
810
|
return zodWrap$1("z.any()", schema);
|
|
781
811
|
}
|
|
782
|
-
|
|
783
|
-
//#region src/generator/zod/index.ts
|
|
812
|
+
// src/generator/zod/index.ts
|
|
784
813
|
/**
|
|
785
814
|
* A whole JSON Schema document as a Zod module: imports, `$defs` in dependency order,
|
|
786
815
|
* the root export, and the inferred type.
|
|
787
816
|
*/
|
|
788
817
|
function schemaToZod(input, options) {
|
|
789
|
-
const schema = withPolymorphicBases(hoistNestedRefs(input));
|
|
818
|
+
const schema = withPolymorphicBases(hoistNestedRefs(sanitizeSchema(input)));
|
|
790
819
|
const exportType = options?.exportType ?? true;
|
|
791
820
|
const baseOptions = emitOptionsOf(options);
|
|
792
821
|
const codeExtensionsPresent = baseOptions.unsafeCodeExtensions === true && findCodeExtensionKeysInSchema(schema).length > 0;
|
|
@@ -812,5 +841,4 @@ function schemaToZod(input, options) {
|
|
|
812
841
|
...exportType ? [`export type ${rootName} = z.infer<typeof ${rootName}>`] : []
|
|
813
842
|
].filter(Boolean).join("\n\n");
|
|
814
843
|
}
|
|
815
|
-
|
|
816
|
-
export { zod as n, schemaToZod as t };
|
|
844
|
+
export { zod as i, forwardIssue as n, nestedIssueMessageWrap as r, schemaToZod as t };
|
package/dist/zod.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { r as JSONSchema } from "./index-
|
|
2
|
-
import { n as GenerateOptions } from "./index-
|
|
3
|
-
//#region src/generator/zod/index.d.ts
|
|
1
|
+
import { r as JSONSchema } from "./index-DLVjyrKu.js";
|
|
2
|
+
import { n as GenerateOptions } from "./index-DU2Nd8eI.js";
|
|
4
3
|
/**
|
|
5
4
|
* A whole JSON Schema document as a Zod module: imports, `$defs` in dependency order,
|
|
6
5
|
* the root export, and the inferred type.
|
|
7
6
|
*/
|
|
8
|
-
export declare function schemaToZod(input: JSONSchema, options?: GenerateOptions): string;
|
|
9
|
-
//#endregion
|
|
7
|
+
export declare function schemaToZod(input: JSONSchema, options?: GenerateOptions): string;
|
package/dist/zod.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as schemaToZod } from "./zod-
|
|
1
|
+
export { t as schemaToZod } from "./zod-BhYAQ3iW.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "schema-truth",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"description": "Generate Zod/Valibot/Effect/TypeBox/ArkType/Ajv/Yup schemas from JSON or YAML files",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ajv",
|
|
@@ -104,11 +104,11 @@
|
|
|
104
104
|
},
|
|
105
105
|
"dependencies": {
|
|
106
106
|
"@apidevtools/json-schema-ref-parser": "^16.0.5",
|
|
107
|
-
"@effect/platform-node": "4.
|
|
108
|
-
"effect": "4.
|
|
109
|
-
"oxfmt": "^0.
|
|
107
|
+
"@effect/platform-node": "4.1.1",
|
|
108
|
+
"effect": "4.1.1",
|
|
109
|
+
"oxfmt": "^0.75.0"
|
|
110
110
|
},
|
|
111
111
|
"devDependencies": {
|
|
112
|
-
"@types/node": "26.6.
|
|
112
|
+
"@types/node": "26.6.6"
|
|
113
113
|
}
|
|
114
114
|
}
|