schema-truth 0.8.7 → 0.8.9

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.
@@ -1,4 +1,4 @@
1
- import { $ as itemsSchemaOf, A as wireArrayOnce, Bt as isNullable, C as isNullTextRead, Dt as zodErrorPart, Et as zodError, F as timePattern, G as renderDefinitions, H as emitOptionsOf, Ht as isPlainObject, I as deepEqualExpression, It as isComposite, J as discriminatedMembers, Jt as isShapelessMember, K as rootNameOf, Ln as UNSAFE_GENERATED_MARKER, M as wireNumericKind, Ot as hasCombinator, Pt as hasType, Rt as isDefaultOnlyMember, S as isNullMatchedFirst, T as numberWirePattern, Tt as zodBaseError, Ut as isPrimitive, V as definitionsOf, Vt as isObjectPrototypeProperty, W as planDocument, X as hasRootSelfReference, Yt as isStringWireLocation, Z as enumWithoutWrappedNull, _ as hoistNestedRefs, _t as issueMessageExpression, b as impliedWireType, c as refTarget, ct as foldAllOfMembers, d as parameterReaders, dt as regexLiteral, f as propertyEmitOptions, g as valueListLiteral, h as notPredicate, i as isEvaluatedKeyExpression, it as findCodeExtensionKeysInSchema, j as wireNullText, jt as hasFixedLength, k as unionPrimitiveTypes, kt as hasConditional, l as withPolymorphicBases, lt as isKeywordOnlyMember, m as isBigintFormat, mn as ISO_4217_CURRENCY_CODES, o as typeDeclaration, ot as constraintMessage, p as withoutParameterSerialization, q as schemaNamer, qt as isSchemaObject, r as evaluatedProperties, rn as DEFAULT_ROOT_NAME, s as sanitizeSchema, st as constraintPredicate, t as zodWrap, u as hasParameterSerialization, vt as makeSafeKey, w as isPrimitiveUnion, x as integerWirePattern, yt as normalizeTypes, zt as isNullTypeMember } from "./helper-1oDB5NHM.js";
1
+ import { $ as itemsSchemaOf, A as wireArrayOnce, Bt as isNullable, C as isNullTextRead, Dt as zodErrorPart, Et as zodError, F as timePattern, G as renderDefinitions, H as emitOptionsOf, Ht as isPlainObject, I as deepEqualExpression, It as isComposite, J as discriminatedMembers, Jt as isShapelessMember, K as rootNameOf, Ln as UNSAFE_GENERATED_MARKER, M as wireNumericKind, Ot as hasCombinator, Pt as hasType, Rt as isDefaultOnlyMember, S as isNullMatchedFirst, T as numberWirePattern, Tt as zodBaseError, Ut as isPrimitive, V as definitionsOf, Vt as isObjectPrototypeProperty, W as planDocument, X as hasRootSelfReference, Yt as isStringWireLocation, Z as enumWithoutWrappedNull, _ as hoistNestedRefs, _t as issueMessageExpression, b as impliedWireType, c as refTarget, ct as foldAllOfMembers, d as parameterReaders, dt as regexLiteral, f as propertyEmitOptions, g as valueListLiteral, h as notPredicate, i as isEvaluatedKeyExpression, it as findCodeExtensionKeysInSchema, j as wireNullText, jt as hasFixedLength, k as unionPrimitiveTypes, kt as hasConditional, l as withPolymorphicBases, lt as isKeywordOnlyMember, m as isBigintFormat, mn as ISO_4217_CURRENCY_CODES, o as typeDeclaration, ot as constraintMessage, p as withoutParameterSerialization, q as schemaNamer, qt as isSchemaObject, r as evaluatedProperties, rn as DEFAULT_ROOT_NAME, s as sanitizeSchema, st as constraintPredicate, t as zodWrap, u as hasParameterSerialization, vt as makeSafeKey, w as isPrimitiveUnion, x as integerWirePattern, yt as normalizeTypes, zt as isNullTypeMember } from "./helper-OCkRyx4U.js";
2
2
  // src/generator/zod/type.ts
3
3
  /** The TypeScript type declared beside a recursive Zod schema. */
4
4
  function type(schema, rootName = DEFAULT_ROOT_NAME) {
@@ -196,13 +196,13 @@ function _enum(schema) {
196
196
  const errorArg = zodErrorPart(errorMessage);
197
197
  const valueSchema = (v) => {
198
198
  if (Array.isArray(v)) return `z.tuple([${v.map(valueSchema).join(",")}])`;
199
- if (isPlainObject(v)) return `z.strictObject({${Object.entries(v).map(([k, val]) => `${makeSafeKey(k)}:${valueSchema(val)}`).join(",")}})`;
199
+ if (isPlainObject(v)) return `z.strictObject({${Object.entries(v).map(([k, member]) => `${makeSafeKey(k)}:${valueSchema(member)}`).join(",")}})`;
200
200
  return `z.literal(${lit(v, isBigint)})`;
201
201
  };
202
202
  const innerLit = (v) => valueSchema(v);
203
203
  const outerLit = (v) => isComposite(v) ? valueSchema(v) : `z.literal(${lit(v, isBigint)}${errorArg})`;
204
- const innerTuple = (arr) => `z.tuple([${arr.map(innerLit).join(",")}])`;
205
- const outerTuple = (arr) => `z.tuple([${arr.map(innerLit).join(",")}]${errorArg})`;
204
+ const innerTuple = (members) => `z.tuple([${members.map(innerLit).join(",")}])`;
205
+ const outerTuple = (members) => `z.tuple([${members.map(innerLit).join(",")}]${errorArg})`;
206
206
  if (hasType(schema, "number") || hasType(schema, "integer") || hasType(schema, "boolean")) return schema.enum.length > 1 ? `z.union([${schema.enum.map(innerLit).join(",")}]${errorArg})` : outerLit(schema.enum[0]);
207
207
  if (hasType(schema, "array")) {
208
208
  if (schema.enum.length === 1 && Array.isArray(schema.enum[0])) return outerTuple(schema.enum[0]);
@@ -280,7 +280,7 @@ function object(schema, rootName, isZod, options) {
280
280
  return `.refine((val)=>Object.entries(val).every(([key,value])=>!new RegExp(${JSON.stringify(pattern)}).test(key)||${z}.safeParse(value).success)${patternErrorArg})`;
281
281
  }).join("") : "";
282
282
  const dependentRequired = schema.dependentRequired ? Object.entries(schema.dependentRequired).map(([key, deps]) => {
283
- return `.refine((val)=>!('${key}' in val)||(${deps.map((d) => `'${d}' in val`).join("&&")})${depReqErrorArg})`;
283
+ return `.refine((val)=>!('${key}' in val)||(${deps.map((dependency) => `'${dependency}' in val`).join("&&")})${depReqErrorArg})`;
284
284
  }).join("") : "";
285
285
  const dependentSchemas = schema.dependentSchemas ? Object.entries(schema.dependentSchemas).map(([key, subSchema]) => {
286
286
  return `.refine((val)=>!('${key}' in val)||${zod(subSchema, rootName, isZod, options)}.safeParse(val).success${depSchErrorArg})`;
@@ -562,7 +562,7 @@ function zodNotPredicate(inner, parent) {
562
562
  * parsed issues does not type-check as a raw issue, while each member does.
563
563
  */
564
564
  function forwardIssue(message) {
565
- return ZOD_ISSUE_CODES.map((c, i) => `${i === 0 ? "" : "else "}if(issue.code==='${c}'){ctx.issues.push({...issue,input:issue.input,message:${message}})}`).join("");
565
+ return ZOD_ISSUE_CODES.map((code, i) => `${i === 0 ? "" : "else "}if(issue.code==='${code}'){ctx.issues.push({...issue,input:issue.input,message:${message}})}`).join("");
566
566
  }
567
567
  /** A message for the issues under the value (with a path); the root's own issues keep theirs. */
568
568
  function nestedIssueMessageWrap(inner, msg) {
@@ -678,7 +678,7 @@ function node(schema, rootName, isZod, options) {
678
678
  ...schema,
679
679
  nullable
680
680
  });
681
- const intersected = schemas.length === 1 ? schemas[0] : schemas.reduce((acc, s) => `z.intersection(${acc},${s})`);
681
+ const intersected = schemas.length === 1 && schemas[0] !== void 0 ? schemas[0] : schemas.reduce((acc, s) => `z.intersection(${acc},${s})`);
682
682
  const allOfMessage = schema["x-allOf-message"];
683
683
  const checked = objectKeysCheck(allOfMessage ? (() => {
684
684
  const branches = forwardIssue(issueMessageExpression(allOfMessage));
@@ -722,25 +722,25 @@ function node(schema, rootName, isZod, options) {
722
722
  }
723
723
  if (schema.properties) return readonly(zodWrap$1(object(schema, rootName, isZod, options), schema));
724
724
  const types = normalizeTypes(schema.type);
725
- const primitive = (t) => {
726
- if (t === "string") return string(schema);
727
- if (t === "number") return number(schema, { wire: isWireCoerced });
728
- if (t === "integer") return integer(schema, { wire: isWireCoerced });
729
- if (t !== "boolean") return void 0;
725
+ const primitive = (primitiveType) => {
726
+ if (primitiveType === "string") return string(schema);
727
+ if (primitiveType === "number") return number(schema, { wire: isWireCoerced });
728
+ if (primitiveType === "integer") return integer(schema, { wire: isWireCoerced });
729
+ if (primitiveType !== "boolean") return void 0;
730
730
  const errorMessage = schema["x-error-message"];
731
731
  const errorArg = errorMessage ? zodError(errorMessage) : "";
732
732
  if (isStringWireParam) return `z.stringbool(${errorArg ? `{error:${errorArg.replaceAll(/^{error:|}$/g, "")}}` : ""})`;
733
733
  return `z.${schema["x-coerce"] === true ? "coerce." : ""}boolean(${errorArg})`;
734
734
  };
735
735
  if (isPrimitiveUnion(types, isWireCoerced)) return zodWrap$1(`z.union([${unionPrimitiveTypes(types, isWireCoerced).map(primitive).filter((m) => m !== void 0).join(",")}])`, schema);
736
- for (const t of [
736
+ for (const primitiveType of [
737
737
  "string",
738
738
  "number",
739
739
  "integer",
740
740
  "boolean"
741
741
  ]) {
742
- if (!types.includes(t)) continue;
743
- const expression = primitive(t);
742
+ if (!types.includes(primitiveType)) continue;
743
+ const expression = primitive(primitiveType);
744
744
  if (expression !== void 0) return zodWrap$1(expression, schema);
745
745
  }
746
746
  if (types.includes("array")) {
@@ -754,13 +754,13 @@ function node(schema, rootName, isZod, options) {
754
754
  const fixedItemsError = zodErrorPart(minItemsMessage ?? maxItemsMessage);
755
755
  const uniqueMessage = schema["x-uniqueItems-message"];
756
756
  const uniqueError = zodErrorPart(uniqueMessage);
757
- if (schema.prefixItems?.length) {
757
+ if (schema.prefixItems !== void 0 && schema.prefixItems.length > 0) {
758
758
  const items = schema.prefixItems.map((s) => zod(s, rootName, isZod, options));
759
- const u = schema.unevaluatedItems;
759
+ const unevaluatedItems = schema.unevaluatedItems;
760
760
  const unevaluatedItemsMessage = schema["x-unevaluatedItems-message"];
761
761
  const unevaluatedItemsError = zodErrorPart(unevaluatedItemsMessage);
762
- const rest = isSchemaObject(u) ? zod(u, rootName, isZod, options) : void 0;
763
- const tupleExpr = `z.tuple(${rest ? `[${items.join(",")}],${rest}` : `[${items.join(",")}]`}${u === false && unevaluatedItemsMessage ? unevaluatedItemsError : baseError})`;
762
+ const rest = isSchemaObject(unevaluatedItems) ? zod(unevaluatedItems, rootName, isZod, options) : void 0;
763
+ const tupleExpr = `z.tuple(${rest ? `[${items.join(",")}],${rest}` : `[${items.join(",")}]`}${unevaluatedItems === false && unevaluatedItemsMessage ? unevaluatedItemsError : baseError})`;
764
764
  const prefixItemsMessage = schema["x-prefixItems-message"];
765
765
  return readonly(zodWrap$1(wireArray(prefixItemsMessage ? elementMessageWrap(tupleExpr, prefixItemsMessage) : tupleExpr), schema));
766
766
  }
@@ -771,9 +771,9 @@ function node(schema, rootName, isZod, options) {
771
771
  const base = itemsMessage ? elementMessageWrap(arrayBase, itemsMessage) : arrayBase;
772
772
  const unique = schema.uniqueItems === true ? `.refine((val)=>new Set(val).size===val.length${uniqueError})` : "";
773
773
  const containsChain = (() => {
774
- const c = schema.contains;
775
- if (!c) return "";
776
- const containsZod = zod(c, rootName, isZod, options);
774
+ const containsSchema = schema.contains;
775
+ if (!containsSchema) return "";
776
+ const containsZod = zod(containsSchema, rootName, isZod, options);
777
777
  const minC = schema.minContains;
778
778
  const maxC = schema.maxContains;
779
779
  const fallback = schema["x-contains-message"] ?? errorMessage;
@@ -821,7 +821,7 @@ function schemaToZod(input, options) {
821
821
  const schema = withPolymorphicBases(hoistNestedRefs(sanitizeSchema(input)));
822
822
  const exportType = options?.exportType ?? true;
823
823
  const baseOptions = emitOptionsOf(options);
824
- const codeExtensionsPresent = baseOptions.unsafeCodeExtensions === true && findCodeExtensionKeysInSchema(schema).length > 0;
824
+ const codeExtensionsPresent = baseOptions.unsafeCodeExtensions && findCodeExtensionKeysInSchema(schema).length > 0;
825
825
  const toName = schemaNamer(baseOptions.openapi);
826
826
  const plan = planDocument(schema, rootNameOf(schema, toName));
827
827
  const definitions = definitionsOf(baseOptions, plan.definitions);
package/dist/zod.js CHANGED
@@ -1 +1 @@
1
- export { t as schemaToZod } from "./zod-q9c0XhJO.js";
1
+ export { t as schemaToZod } from "./zod-04J51AMt.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schema-truth",
3
- "version": "0.8.7",
3
+ "version": "0.8.9",
4
4
  "description": "Generate Zod/Valibot/Effect/TypeBox/ArkType/Ajv/Yup schemas from JSON or YAML files",
5
5
  "keywords": [
6
6
  "ajv",