zod 4.5.4 → 4.6.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/compile.cjs +19 -0
- package/index.cjs +19 -0
- package/locales/index.cjs +19 -0
- package/mini/index.cjs +19 -0
- package/package.json +8 -7
- package/src/v3/helpers/errorUtil.ts +1 -1
- package/src/v4/classic/checks.ts +0 -1
- package/src/v4/classic/external.ts +2 -0
- package/src/v4/classic/from-json-schema.ts +248 -25
- package/src/v4/classic/schemas.ts +255 -164
- package/src/v4/classic/tests/assignability.test.ts +59 -9
- package/src/v4/classic/tests/bigint.test.ts +6 -0
- package/src/v4/classic/tests/continuability.test.ts +2 -2
- package/src/v4/classic/tests/cyclic-data.test.ts +274 -0
- package/src/v4/classic/tests/discriminated-unions.test.ts +112 -6
- package/src/v4/classic/tests/enum.test.ts +9 -0
- package/src/v4/classic/tests/error.test.ts +130 -1
- package/src/v4/classic/tests/firstparty.test.ts +4 -0
- package/src/v4/classic/tests/from-json-schema.test.ts +157 -0
- package/src/v4/classic/tests/iban.test.ts +81 -0
- package/src/v4/classic/tests/instance-footprint.test.ts +18 -2
- package/src/v4/classic/tests/instanceof.test.ts +27 -7
- package/src/v4/classic/tests/issue-schema.test.ts +88 -56
- package/src/v4/classic/tests/object.test.ts +56 -0
- package/src/v4/classic/tests/optin-ladder.test.ts +3 -2
- package/src/v4/classic/tests/properties.test.ts +225 -0
- package/src/v4/classic/tests/recursive-types.test.ts +46 -1
- package/src/v4/classic/tests/standard-schema.test.ts +19 -0
- package/src/v4/classic/tests/string.test.ts +25 -0
- package/src/v4/classic/tests/template-literal.test.ts +33 -1
- package/src/v4/classic/tests/to-json-schema.test.ts +147 -3
- package/src/v4/classic/tests/validate.test.ts +569 -0
- package/src/v4/core/api.ts +30 -6
- package/src/v4/core/checks.ts +1 -101
- package/src/v4/core/compile.ts +194 -82
- package/src/v4/core/doc.ts +6 -2
- package/src/v4/core/errors.ts +3 -6
- package/src/v4/core/json-schema-generator.ts +2 -2
- package/src/v4/core/json-schema-processors.ts +226 -68
- package/src/v4/core/memoizer.ts +78 -28
- package/src/v4/core/parse.ts +37 -16
- package/src/v4/core/regexes.ts +10 -4
- package/src/v4/core/schemas.ts +350 -113
- package/src/v4/core/tests/cached.test.ts +54 -0
- package/src/v4/core/tests/compile-generative.test.ts +445 -0
- package/src/v4/core/tests/compile.test.ts +222 -2
- package/src/v4/core/tests/email-regex.test.ts +140 -0
- package/src/v4/core/tests/locales/tg.test.ts +85 -0
- package/src/v4/core/tests/polyfill-collision.test.ts +46 -0
- package/src/v4/core/to-json-schema.ts +10 -8
- package/src/v4/core/util.ts +222 -147
- package/src/v4/core/versions.ts +2 -2
- package/src/v4/core/visit.ts +11 -0
- package/src/v4/locales/ar.ts +1 -0
- package/src/v4/locales/az.ts +1 -0
- package/src/v4/locales/be.ts +1 -0
- package/src/v4/locales/bg.ts +1 -0
- package/src/v4/locales/bn.ts +1 -0
- package/src/v4/locales/ca.ts +1 -0
- package/src/v4/locales/ckb.ts +1 -0
- package/src/v4/locales/cs.ts +1 -0
- package/src/v4/locales/da.ts +1 -0
- package/src/v4/locales/de.ts +1 -0
- package/src/v4/locales/el.ts +1 -0
- package/src/v4/locales/en.ts +1 -0
- package/src/v4/locales/eo.ts +1 -0
- package/src/v4/locales/es.ts +1 -0
- package/src/v4/locales/fa.ts +1 -0
- package/src/v4/locales/fi.ts +1 -0
- package/src/v4/locales/fr-CA.ts +1 -0
- package/src/v4/locales/fr.ts +1 -0
- package/src/v4/locales/gu.ts +1 -0
- package/src/v4/locales/he.ts +1 -0
- package/src/v4/locales/hi.ts +1 -0
- package/src/v4/locales/hr.ts +1 -0
- package/src/v4/locales/hu.ts +1 -0
- package/src/v4/locales/hy.ts +1 -0
- package/src/v4/locales/id.ts +1 -0
- package/src/v4/locales/index.ts +1 -0
- package/src/v4/locales/is.ts +1 -0
- package/src/v4/locales/it.ts +1 -0
- package/src/v4/locales/ja.ts +1 -0
- package/src/v4/locales/ka.ts +1 -0
- package/src/v4/locales/km.ts +1 -0
- package/src/v4/locales/kn.ts +1 -0
- package/src/v4/locales/ko.ts +1 -0
- package/src/v4/locales/lt.ts +1 -0
- package/src/v4/locales/mk.ts +1 -0
- package/src/v4/locales/ms.ts +1 -0
- package/src/v4/locales/ne.ts +1 -0
- package/src/v4/locales/nl.ts +1 -0
- package/src/v4/locales/nn.ts +1 -0
- package/src/v4/locales/no.ts +1 -0
- package/src/v4/locales/ota.ts +1 -0
- package/src/v4/locales/pl.ts +1 -0
- package/src/v4/locales/ps.ts +1 -0
- package/src/v4/locales/pt-BR.ts +1 -0
- package/src/v4/locales/pt.ts +1 -0
- package/src/v4/locales/ro.ts +1 -0
- package/src/v4/locales/ru.ts +1 -0
- package/src/v4/locales/sk.ts +1 -0
- package/src/v4/locales/sl.ts +1 -0
- package/src/v4/locales/sv.ts +1 -0
- package/src/v4/locales/ta.ts +1 -0
- package/src/v4/locales/tg.ts +134 -0
- package/src/v4/locales/th.ts +1 -0
- package/src/v4/locales/tk.ts +1 -0
- package/src/v4/locales/tr.ts +1 -0
- package/src/v4/locales/uk.ts +1 -0
- package/src/v4/locales/ur.ts +1 -0
- package/src/v4/locales/uz.ts +1 -0
- package/src/v4/locales/vi.ts +1 -0
- package/src/v4/locales/yo.ts +1 -0
- package/src/v4/locales/zh-CN.ts +1 -0
- package/src/v4/locales/zh-TW.ts +1 -0
- package/src/v4/mini/checks.ts +0 -1
- package/src/v4/mini/external.ts +2 -0
- package/src/v4/mini/schemas.ts +38 -2
- package/src/v4/mini/tests/checks.test.ts +1 -1
- package/src/v4/mini/tests/computed.test.ts +11 -17
- package/src/v4/mini/tests/index.test.ts +8 -0
- package/src/v4/mini/tests/recursive-types.test.ts +16 -0
- package/src/v4/mini/tests/string.test.ts +16 -0
- package/v3/ZodError.cjs +19 -0
- package/v3/errors.cjs +19 -0
- package/v3/external.cjs +19 -0
- package/v3/helpers/enumUtil.cjs +19 -0
- package/v3/helpers/errorUtil.cjs +20 -1
- package/v3/helpers/errorUtil.js +1 -1
- package/v3/helpers/parseUtil.cjs +19 -0
- package/v3/helpers/partialUtil.cjs +19 -0
- package/v3/helpers/typeAliases.cjs +19 -0
- package/v3/helpers/util.cjs +19 -0
- package/v3/index.cjs +19 -0
- package/v3/standard-schema.cjs +19 -0
- package/v3/types.cjs +19 -0
- package/v4/classic/checks.cjs +20 -2
- package/v4/classic/checks.d.cts +1 -1
- package/v4/classic/checks.d.ts +1 -1
- package/v4/classic/checks.js +1 -1
- package/v4/classic/coerce.cjs +19 -0
- package/v4/classic/compat.cjs +19 -0
- package/v4/classic/deep-partial.cjs +19 -0
- package/v4/classic/errors.cjs +19 -0
- package/v4/classic/external.cjs +22 -1
- package/v4/classic/external.d.cts +1 -1
- package/v4/classic/external.d.ts +1 -1
- package/v4/classic/external.js +1 -1
- package/v4/classic/from-json-schema.cjs +264 -24
- package/v4/classic/from-json-schema.js +245 -24
- package/v4/classic/in-out.cjs +19 -0
- package/v4/classic/index.cjs +19 -0
- package/v4/classic/iso.cjs +19 -0
- package/v4/classic/parse.cjs +19 -0
- package/v4/classic/schemas.cjs +100 -31
- package/v4/classic/schemas.d.cts +17 -1
- package/v4/classic/schemas.d.ts +17 -1
- package/v4/classic/schemas.js +77 -29
- package/v4/core/api.cjs +37 -2
- package/v4/core/api.d.cts +6 -4
- package/v4/core/api.d.ts +6 -4
- package/v4/core/api.js +17 -2
- package/v4/core/checks.cjs +19 -96
- package/v4/core/checks.d.cts +1 -1
- package/v4/core/checks.d.ts +1 -1
- package/v4/core/checks.js +0 -96
- package/v4/core/compile.cjs +165 -66
- package/v4/core/compile.d.cts +18 -4
- package/v4/core/compile.d.ts +18 -4
- package/v4/core/compile.js +146 -67
- package/v4/core/core.cjs +19 -0
- package/v4/core/doc.cjs +26 -2
- package/v4/core/doc.js +7 -2
- package/v4/core/errors.cjs +21 -5
- package/v4/core/errors.js +2 -5
- package/v4/core/index.cjs +19 -0
- package/v4/core/json-schema-generator.cjs +20 -1
- package/v4/core/json-schema-generator.js +2 -2
- package/v4/core/json-schema-processors.cjs +215 -63
- package/v4/core/json-schema-processors.d.cts +17 -0
- package/v4/core/json-schema-processors.d.ts +17 -0
- package/v4/core/json-schema-processors.js +195 -64
- package/v4/core/json-schema.cjs +19 -0
- package/v4/core/memoizer.cjs +114 -23
- package/v4/core/memoizer.js +72 -23
- package/v4/core/parse.cjs +56 -16
- package/v4/core/parse.js +37 -16
- package/v4/core/regexes.cjs +29 -6
- package/v4/core/regexes.d.cts +2 -0
- package/v4/core/regexes.d.ts +2 -0
- package/v4/core/regexes.js +8 -4
- package/v4/core/registries.cjs +19 -0
- package/v4/core/schemas.cjs +322 -95
- package/v4/core/schemas.d.cts +42 -7
- package/v4/core/schemas.d.ts +42 -7
- package/v4/core/schemas.js +301 -94
- package/v4/core/standard-schema.cjs +19 -0
- package/v4/core/to-json-schema.cjs +29 -7
- package/v4/core/to-json-schema.d.cts +7 -3
- package/v4/core/to-json-schema.d.ts +7 -3
- package/v4/core/to-json-schema.js +8 -7
- package/v4/core/util.cjs +181 -143
- package/v4/core/util.d.cts +25 -4
- package/v4/core/util.d.ts +25 -4
- package/v4/core/util.js +160 -143
- package/v4/core/versions.cjs +21 -2
- package/v4/core/versions.d.cts +1 -1
- package/v4/core/versions.d.ts +1 -1
- package/v4/core/versions.js +2 -2
- package/v4/core/visit.cjs +31 -0
- package/v4/core/visit.js +12 -0
- package/v4/index.cjs +19 -0
- package/v4/locales/ar.cjs +1 -0
- package/v4/locales/ar.js +1 -0
- package/v4/locales/az.cjs +1 -0
- package/v4/locales/az.js +1 -0
- package/v4/locales/be.cjs +1 -0
- package/v4/locales/be.js +1 -0
- package/v4/locales/bg.cjs +1 -0
- package/v4/locales/bg.js +1 -0
- package/v4/locales/bn.cjs +1 -0
- package/v4/locales/bn.js +1 -0
- package/v4/locales/ca.cjs +1 -0
- package/v4/locales/ca.js +1 -0
- package/v4/locales/ckb.cjs +1 -0
- package/v4/locales/ckb.js +1 -0
- package/v4/locales/cs.cjs +1 -0
- package/v4/locales/cs.js +1 -0
- package/v4/locales/da.cjs +1 -0
- package/v4/locales/da.js +1 -0
- package/v4/locales/de.cjs +1 -0
- package/v4/locales/de.js +1 -0
- package/v4/locales/el.cjs +1 -0
- package/v4/locales/el.js +1 -0
- package/v4/locales/en.cjs +1 -0
- package/v4/locales/en.js +1 -0
- package/v4/locales/eo.cjs +1 -0
- package/v4/locales/eo.js +1 -0
- package/v4/locales/es.cjs +1 -0
- package/v4/locales/es.js +1 -0
- package/v4/locales/fa.cjs +1 -0
- package/v4/locales/fa.js +1 -0
- package/v4/locales/fi.cjs +1 -0
- package/v4/locales/fi.js +1 -0
- package/v4/locales/fr-CA.cjs +1 -0
- package/v4/locales/fr-CA.js +1 -0
- package/v4/locales/fr.cjs +1 -0
- package/v4/locales/fr.js +1 -0
- package/v4/locales/gu.cjs +1 -0
- package/v4/locales/gu.js +1 -0
- package/v4/locales/he.cjs +1 -0
- package/v4/locales/he.js +1 -0
- package/v4/locales/hi.cjs +1 -0
- package/v4/locales/hi.js +1 -0
- package/v4/locales/hr.cjs +1 -0
- package/v4/locales/hr.js +1 -0
- package/v4/locales/hu.cjs +1 -0
- package/v4/locales/hu.js +1 -0
- package/v4/locales/hy.cjs +1 -0
- package/v4/locales/hy.js +1 -0
- package/v4/locales/id.cjs +1 -0
- package/v4/locales/id.js +1 -0
- package/v4/locales/index.cjs +22 -1
- package/v4/locales/index.d.cts +1 -0
- package/v4/locales/index.d.ts +1 -0
- package/v4/locales/index.js +1 -0
- package/v4/locales/is.cjs +1 -0
- package/v4/locales/is.js +1 -0
- package/v4/locales/it.cjs +1 -0
- package/v4/locales/it.js +1 -0
- package/v4/locales/ja.cjs +1 -0
- package/v4/locales/ja.js +1 -0
- package/v4/locales/ka.cjs +1 -0
- package/v4/locales/ka.js +1 -0
- package/v4/locales/km.cjs +1 -0
- package/v4/locales/km.js +1 -0
- package/v4/locales/kn.cjs +1 -0
- package/v4/locales/kn.js +1 -0
- package/v4/locales/ko.cjs +1 -0
- package/v4/locales/ko.js +1 -0
- package/v4/locales/lt.cjs +1 -0
- package/v4/locales/lt.js +1 -0
- package/v4/locales/mk.cjs +1 -0
- package/v4/locales/mk.js +1 -0
- package/v4/locales/ms.cjs +1 -0
- package/v4/locales/ms.js +1 -0
- package/v4/locales/ne.cjs +1 -0
- package/v4/locales/ne.js +1 -0
- package/v4/locales/nl.cjs +1 -0
- package/v4/locales/nl.js +1 -0
- package/v4/locales/nn.cjs +1 -0
- package/v4/locales/nn.js +1 -0
- package/v4/locales/no.cjs +1 -0
- package/v4/locales/no.js +1 -0
- package/v4/locales/ota.cjs +1 -0
- package/v4/locales/ota.js +1 -0
- package/v4/locales/pl.cjs +1 -0
- package/v4/locales/pl.js +1 -0
- package/v4/locales/ps.cjs +1 -0
- package/v4/locales/ps.js +1 -0
- package/v4/locales/pt-BR.cjs +1 -0
- package/v4/locales/pt-BR.js +1 -0
- package/v4/locales/pt.cjs +1 -0
- package/v4/locales/pt.js +1 -0
- package/v4/locales/ro.cjs +1 -0
- package/v4/locales/ro.js +1 -0
- package/v4/locales/ru.cjs +1 -0
- package/v4/locales/ru.js +1 -0
- package/v4/locales/sk.cjs +1 -0
- package/v4/locales/sk.js +1 -0
- package/v4/locales/sl.cjs +1 -0
- package/v4/locales/sl.js +1 -0
- package/v4/locales/sv.cjs +1 -0
- package/v4/locales/sv.js +1 -0
- package/v4/locales/ta.cjs +1 -0
- package/v4/locales/ta.js +1 -0
- package/v4/locales/tg.cjs +143 -0
- package/v4/locales/tg.d.cts +5 -0
- package/v4/locales/tg.d.ts +4 -0
- package/v4/locales/tg.js +116 -0
- package/v4/locales/th.cjs +1 -0
- package/v4/locales/th.js +1 -0
- package/v4/locales/tk.cjs +1 -0
- package/v4/locales/tk.js +1 -0
- package/v4/locales/tr.cjs +1 -0
- package/v4/locales/tr.js +1 -0
- package/v4/locales/uk.cjs +1 -0
- package/v4/locales/uk.js +1 -0
- package/v4/locales/ur.cjs +1 -0
- package/v4/locales/ur.js +1 -0
- package/v4/locales/uz.cjs +1 -0
- package/v4/locales/uz.js +1 -0
- package/v4/locales/vi.cjs +1 -0
- package/v4/locales/vi.js +1 -0
- package/v4/locales/yo.cjs +1 -0
- package/v4/locales/yo.js +1 -0
- package/v4/locales/zh-CN.cjs +1 -0
- package/v4/locales/zh-CN.js +1 -0
- package/v4/locales/zh-TW.cjs +1 -0
- package/v4/locales/zh-TW.js +1 -0
- package/v4/mini/checks.cjs +20 -2
- package/v4/mini/checks.d.cts +1 -1
- package/v4/mini/checks.d.ts +1 -1
- package/v4/mini/checks.js +1 -1
- package/v4/mini/coerce.cjs +19 -0
- package/v4/mini/deep-partial.cjs +19 -0
- package/v4/mini/external.cjs +22 -1
- package/v4/mini/external.d.cts +1 -1
- package/v4/mini/external.d.ts +1 -1
- package/v4/mini/external.js +1 -1
- package/v4/mini/in-out.cjs +19 -0
- package/v4/mini/index.cjs +19 -0
- package/v4/mini/iso.cjs +19 -0
- package/v4/mini/parse.cjs +19 -0
- package/v4/mini/schemas.cjs +42 -4
- package/v4/mini/schemas.d.cts +8 -0
- package/v4/mini/schemas.d.ts +8 -0
- package/v4/mini/schemas.js +19 -2
- package/v4-mini/index.cjs +19 -0
package/v4/core/schemas.cjs
CHANGED
|
@@ -23,8 +23,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.$
|
|
27
|
-
exports.$ZodCustom = exports.$ZodLazy = exports.$ZodPromise = exports.$ZodFunction = exports.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodPreprocess = exports.$ZodCodec = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodExactOptional = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = exports.$ZodMap = exports.$ZodRecord = exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = void 0;
|
|
26
|
+
exports.$ZodObject = exports.$ZodArray = exports.$ZodDate = exports.$ZodVoid = exports.$ZodNever = exports.$ZodUnknown = exports.$ZodAny = exports.$ZodNull = exports.$ZodUndefined = exports.$ZodSymbol = exports.$ZodBigIntFormat = exports.$ZodBigInt = exports.$ZodBoolean = exports.$ZodNumberFormat = exports.$ZodNumber = exports.$ZodCustomStringFormat = exports.$ZodJWT = exports.$ZodIBAN = exports.$ZodCreditCard = exports.$ZodE164 = exports.$ZodBase64URL = exports.base64urlCharset = exports.$ZodBase64 = exports.base64Charset = exports.$ZodCIDRv6 = exports.$ZodCIDRv4 = exports.$ZodMAC = exports.$ZodIPv6 = exports.$ZodIPv4 = exports.$ZodISODuration = exports.$ZodISOTime = exports.$ZodISODate = exports.$ZodISODateTime = exports.$ZodKSUID = exports.$ZodXID = exports.$ZodULID = exports.$ZodCUID2 = exports.$ZodCUID = exports.$ZodNanoID = exports.$ZodEmoji = exports.$ZodURL = exports.URL_UNPARSEABLE = exports.URL_BAD_FORMAT = exports.$ZodEmail = exports.$ZodUUID = exports.$ZodGUID = exports.$ZodStringFormat = exports.$ZodString = exports.clone = exports.$ZodType = void 0;
|
|
27
|
+
exports.$ZodProperties = exports.$ZodCustom = exports.$ZodLazy = exports.$ZodPromise = exports.$ZodFunction = exports.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodPreprocess = exports.$ZodCodec = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodExactOptional = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = exports.$ZodMap = exports.$ZodRecord = exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = exports.$ZodXor = exports.$ZodUnion = exports.$ZodObjectJIT = void 0;
|
|
28
28
|
exports.standardProps = standardProps;
|
|
29
29
|
exports.parseURLObject = parseURLObject;
|
|
30
30
|
exports.stripTabAndNewline = stripTabAndNewline;
|
|
@@ -35,6 +35,7 @@ exports.isValidCIDRv6 = isValidCIDRv6;
|
|
|
35
35
|
exports.isValidBase64 = isValidBase64;
|
|
36
36
|
exports.isValidBase64URL = isValidBase64URL;
|
|
37
37
|
exports.isValidCreditCard = isValidCreditCard;
|
|
38
|
+
exports.isValidIBAN = isValidIBAN;
|
|
38
39
|
exports.isValidJWT = isValidJWT;
|
|
39
40
|
exports.getDiscriminatedOption = getDiscriminatedOption;
|
|
40
41
|
exports.mergeValues = mergeValues;
|
|
@@ -168,16 +169,24 @@ exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
|
|
|
168
169
|
},
|
|
169
170
|
});
|
|
170
171
|
/** The Standard Schema surface for `inst`. Shared so wrappers can extend it without forcing it. */
|
|
171
|
-
|
|
172
|
+
// a Standard Schema result only reports issues, so a failure finalizes them straight off the raw payload: no ZodError, and no lazy result to read through
|
|
173
|
+
const toStandardResult = (r, ctx) => r.issues.length ? { issues: r.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())) } : { value: r.value };
|
|
174
|
+
async function validateAsync(inst, value) {
|
|
175
|
+
const ctx = { async: true };
|
|
176
|
+
return toStandardResult((await inst._zod.run({ value, issues: [] }, ctx)), ctx);
|
|
177
|
+
}
|
|
172
178
|
function standardProps(inst) {
|
|
173
179
|
return {
|
|
174
180
|
validate: (value) => {
|
|
181
|
+
const ctx = { async: false };
|
|
175
182
|
try {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return (0, parse_js_1.safeParseAsync)(inst, value).then(toStandardResult);
|
|
183
|
+
const r = inst._zod.run({ value, issues: [] }, ctx);
|
|
184
|
+
if (!(r instanceof Promise))
|
|
185
|
+
return toStandardResult(r, ctx);
|
|
180
186
|
}
|
|
187
|
+
catch (_) { }
|
|
188
|
+
// async function so a synchronously throwing check rejects instead of escaping validate
|
|
189
|
+
return validateAsync(inst, value);
|
|
181
190
|
},
|
|
182
191
|
vendor: "zod",
|
|
183
192
|
version: 1,
|
|
@@ -187,7 +196,8 @@ var util_js_1 = require("./util.cjs");
|
|
|
187
196
|
Object.defineProperty(exports, "clone", { enumerable: true, get: function () { return util_js_1.clone; } });
|
|
188
197
|
exports.$ZodString = core.$constructor("$ZodString", (inst, def) => {
|
|
189
198
|
exports.$ZodType.init(inst, def);
|
|
190
|
-
|
|
199
|
+
// a format's own pattern, else unbounded; a template literal derives the check-aware form itself
|
|
200
|
+
inst._zod.pattern = def.pattern ?? regexes.anyString;
|
|
191
201
|
inst._zod.parse = (payload, _) => {
|
|
192
202
|
if (def.coerce)
|
|
193
203
|
try {
|
|
@@ -373,13 +383,6 @@ exports.$ZodKSUID = core.$constructor("$ZodKSUID", (inst, def) => {
|
|
|
373
383
|
exports.$ZodISODateTime = core.$constructor("$ZodISODateTime", (inst, def) => {
|
|
374
384
|
def.pattern ?? (def.pattern = regexes.datetime(def));
|
|
375
385
|
exports.$ZodStringFormat.init(inst, def);
|
|
376
|
-
// these two drop the offset or seconds `date-time` requires — on the bag not the def, since `z.string().check(...)` lands the format on a different schema
|
|
377
|
-
if (def.local || def.precision === -1) {
|
|
378
|
-
inst._zod.bag.laxFormat = true;
|
|
379
|
-
inst._zod.onattach.push((s) => {
|
|
380
|
-
s._zod.bag.laxFormat = true;
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
386
|
});
|
|
384
387
|
exports.$ZodISODate = core.$constructor("$ZodISODate", (inst, def) => {
|
|
385
388
|
def.pattern ?? (def.pattern = regexes.date);
|
|
@@ -396,7 +399,6 @@ exports.$ZodISODuration = core.$constructor("$ZodISODuration", (inst, def) => {
|
|
|
396
399
|
exports.$ZodIPv4 = core.$constructor("$ZodIPv4", (inst, def) => {
|
|
397
400
|
def.pattern ?? (def.pattern = regexes.ipv4);
|
|
398
401
|
exports.$ZodStringFormat.init(inst, def);
|
|
399
|
-
inst._zod.bag.format = `ipv4`;
|
|
400
402
|
});
|
|
401
403
|
/** An IPv6 address is written with hex digits, colons and dots, and nothing else. The guard is what makes the check below an IPv6 check: `new URL("http://[...]")` parses an authority, not an address, so `@` and `\` re-delimit it and `"::@1\\"` validates against the host `0.0.0.1`. The URL parser also deletes ASCII tab, LF and CR rather than failing, which is how `"::1\n"` validated as `::1`. */
|
|
402
404
|
const ipv6Alphabet = /^[0-9a-fA-F:.]+$/;
|
|
@@ -415,7 +417,6 @@ function isValidIPv6(value) {
|
|
|
415
417
|
exports.$ZodIPv6 = core.$constructor("$ZodIPv6", (inst, def) => {
|
|
416
418
|
def.pattern ?? (def.pattern = regexes.ipv6);
|
|
417
419
|
exports.$ZodStringFormat.init(inst, def);
|
|
418
|
-
inst._zod.bag.format = `ipv6`;
|
|
419
420
|
inst._zod.check = (payload) => {
|
|
420
421
|
if (!isValidIPv6(payload.value)) {
|
|
421
422
|
payload.issues.push({
|
|
@@ -431,7 +432,6 @@ exports.$ZodIPv6 = core.$constructor("$ZodIPv6", (inst, def) => {
|
|
|
431
432
|
exports.$ZodMAC = core.$constructor("$ZodMAC", (inst, def) => {
|
|
432
433
|
def.pattern ?? (def.pattern = regexes.mac(def.delimiter));
|
|
433
434
|
exports.$ZodStringFormat.init(inst, def);
|
|
434
|
-
inst._zod.bag.format = `mac`;
|
|
435
435
|
});
|
|
436
436
|
exports.$ZodCIDRv4 = core.$constructor("$ZodCIDRv4", (inst, def) => {
|
|
437
437
|
def.pattern ?? (def.pattern = regexes.cidrv4);
|
|
@@ -484,10 +484,11 @@ function isValidBase64(data) {
|
|
|
484
484
|
return false;
|
|
485
485
|
}
|
|
486
486
|
}
|
|
487
|
+
// lax on purpose: the quantified regexes.base64 overflows the regex stack on multi-MB input and its leading ^$| alternation leaks through template-literal composition; isValidBase64 enforces length and padding
|
|
488
|
+
exports.base64Charset = /^[0-9a-zA-Z+/]*={0,2}$/;
|
|
487
489
|
exports.$ZodBase64 = core.$constructor("$ZodBase64", (inst, def) => {
|
|
488
|
-
def.pattern ?? (def.pattern =
|
|
490
|
+
def.pattern ?? (def.pattern = exports.base64Charset);
|
|
489
491
|
exports.$ZodStringFormat.init(inst, def);
|
|
490
|
-
inst._zod.bag.contentEncoding = "base64";
|
|
491
492
|
inst._zod.check = (payload) => {
|
|
492
493
|
if (isValidBase64(payload.value))
|
|
493
494
|
return;
|
|
@@ -500,18 +501,19 @@ exports.$ZodBase64 = core.$constructor("$ZodBase64", (inst, def) => {
|
|
|
500
501
|
});
|
|
501
502
|
};
|
|
502
503
|
});
|
|
503
|
-
//////////////////////////////
|
|
504
|
+
////////////////////////////// ZodBase64URL //////////////////////////////
|
|
505
|
+
// lax on purpose: the quantified regexes.base64url overflows the regex stack on multi-MB input; isValidBase64 enforces length on the padded string
|
|
506
|
+
exports.base64urlCharset = /^[A-Za-z0-9_-]*$/;
|
|
504
507
|
function isValidBase64URL(data) {
|
|
505
|
-
if (!
|
|
508
|
+
if (!exports.base64urlCharset.test(data))
|
|
506
509
|
return false;
|
|
507
510
|
const base64 = data.replace(/[-_]/g, (c) => (c === "-" ? "+" : "/"));
|
|
508
511
|
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
509
512
|
return isValidBase64(padded);
|
|
510
513
|
}
|
|
511
514
|
exports.$ZodBase64URL = core.$constructor("$ZodBase64URL", (inst, def) => {
|
|
512
|
-
def.pattern ?? (def.pattern =
|
|
515
|
+
def.pattern ?? (def.pattern = exports.base64urlCharset);
|
|
513
516
|
exports.$ZodStringFormat.init(inst, def);
|
|
514
|
-
inst._zod.bag.contentEncoding = "base64url";
|
|
515
517
|
inst._zod.check = (payload) => {
|
|
516
518
|
if (isValidBase64URL(payload.value))
|
|
517
519
|
return;
|
|
@@ -536,7 +538,7 @@ function isLuhnAlgo(digits) {
|
|
|
536
538
|
let bit = 1;
|
|
537
539
|
let sum = 0;
|
|
538
540
|
while (length) {
|
|
539
|
-
const value =
|
|
541
|
+
const value = digits.charCodeAt(--length) - 48;
|
|
540
542
|
bit ^= 1;
|
|
541
543
|
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
542
544
|
}
|
|
@@ -563,6 +565,42 @@ exports.$ZodCreditCard = core.$constructor("$ZodCreditCard", (inst, def) => {
|
|
|
563
565
|
});
|
|
564
566
|
};
|
|
565
567
|
});
|
|
568
|
+
////////////////////////////// ZodIBAN //////////////////////////////
|
|
569
|
+
// iso 7064 mod 97-10 checksum without BigInt
|
|
570
|
+
function isIso7064Mod97(iban) {
|
|
571
|
+
let remainder = 0;
|
|
572
|
+
const len = iban.length;
|
|
573
|
+
for (let i = 4; i < len; i++) {
|
|
574
|
+
const code = iban.charCodeAt(i);
|
|
575
|
+
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
576
|
+
}
|
|
577
|
+
for (let i = 0; i < 4; i++) {
|
|
578
|
+
const code = iban.charCodeAt(i);
|
|
579
|
+
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
580
|
+
}
|
|
581
|
+
return remainder === 1;
|
|
582
|
+
}
|
|
583
|
+
function isValidIBAN(input) {
|
|
584
|
+
if (!regexes.iban.test(input))
|
|
585
|
+
return false;
|
|
586
|
+
return isIso7064Mod97(input);
|
|
587
|
+
}
|
|
588
|
+
exports.$ZodIBAN = core.$constructor("$ZodIBAN", (inst, def) => {
|
|
589
|
+
// shape only — checksum is not expressible as a pattern
|
|
590
|
+
def.pattern ?? (def.pattern = regexes.iban);
|
|
591
|
+
exports.$ZodStringFormat.init(inst, def);
|
|
592
|
+
inst._zod.check = (payload) => {
|
|
593
|
+
if (isValidIBAN(payload.value))
|
|
594
|
+
return;
|
|
595
|
+
payload.issues.push({
|
|
596
|
+
code: "invalid_format",
|
|
597
|
+
format: "iban",
|
|
598
|
+
input: payload.value,
|
|
599
|
+
inst,
|
|
600
|
+
continue: !def.abort,
|
|
601
|
+
});
|
|
602
|
+
};
|
|
603
|
+
});
|
|
566
604
|
////////////////////////////// ZodJWT //////////////////////////////
|
|
567
605
|
function isValidJWT(token, algorithm = null) {
|
|
568
606
|
try {
|
|
@@ -616,7 +654,7 @@ exports.$ZodCustomStringFormat = core.$constructor("$ZodCustomStringFormat", (in
|
|
|
616
654
|
});
|
|
617
655
|
exports.$ZodNumber = core.$constructor("$ZodNumber", (inst, def) => {
|
|
618
656
|
exports.$ZodType.init(inst, def);
|
|
619
|
-
inst._zod.pattern =
|
|
657
|
+
inst._zod.pattern = regexes.number;
|
|
620
658
|
inst._zod.parse = (payload, _ctx) => {
|
|
621
659
|
if (def.coerce)
|
|
622
660
|
try {
|
|
@@ -824,6 +862,7 @@ exports.$ZodArray = core.$constructor("$ZodArray", (inst, def) => {
|
|
|
824
862
|
}
|
|
825
863
|
payload.value = memo ? memo.alloc(inst, payload, Array(input.length), ctx) : Array(input.length);
|
|
826
864
|
const proms = [];
|
|
865
|
+
const abortEarly = ctx?.abortEarly;
|
|
827
866
|
for (let i = 0; i < input.length; i++) {
|
|
828
867
|
const item = input[i];
|
|
829
868
|
const result = def.element._zod.run({
|
|
@@ -835,6 +874,9 @@ exports.$ZodArray = core.$constructor("$ZodArray", (inst, def) => {
|
|
|
835
874
|
}
|
|
836
875
|
else {
|
|
837
876
|
handleArrayResult(result, payload, i);
|
|
877
|
+
// the element's payload is authoritative here, since handleArrayResult forwards every issue; an object's is not, because it drops a failed absent optional
|
|
878
|
+
if (abortEarly && result.issues.length !== 0 && util.aborted(result))
|
|
879
|
+
break;
|
|
838
880
|
}
|
|
839
881
|
}
|
|
840
882
|
if (proms.length) {
|
|
@@ -901,14 +943,21 @@ function normalizeDef(def) {
|
|
|
901
943
|
optionalKeys: new Set(okeys),
|
|
902
944
|
};
|
|
903
945
|
}
|
|
904
|
-
function handleCatchall(proms, input, payload, ctx, def, inst) {
|
|
946
|
+
function handleCatchall(proms, input, payload, ctx, def, inst, abortEarly) {
|
|
905
947
|
const unrecognized = [];
|
|
906
948
|
const keySet = def.keySet;
|
|
907
949
|
const _catchall = def.catchall._zod;
|
|
908
950
|
const t = _catchall.def.type;
|
|
909
951
|
const optin = _catchall.optin;
|
|
910
952
|
const optout = _catchall.optout;
|
|
953
|
+
// starts at 0, not the current length: the shape phase already ran and may have aborted
|
|
954
|
+
let seen = 0;
|
|
911
955
|
for (const key in input) {
|
|
956
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
957
|
+
if (util.aborted(payload, seen))
|
|
958
|
+
break;
|
|
959
|
+
seen = payload.issues.length;
|
|
960
|
+
}
|
|
912
961
|
// Must precede the __proto__ branch: a declared key is not unrecognized, even though the shape loop deliberately strips __proto__ from the parsed output.
|
|
913
962
|
if (keySet.has(key))
|
|
914
963
|
continue;
|
|
@@ -946,26 +995,22 @@ function handleCatchall(proms, input, payload, ctx, def, inst) {
|
|
|
946
995
|
return payload;
|
|
947
996
|
});
|
|
948
997
|
}
|
|
949
|
-
// Whichever object a def's `shape` currently answers from: the one the caller passed until the first read, the frozen copy after it. Keyed by def, so a def rebuilt by a builder is simply absent rather than inheriting the source's. Read its keys with `Object.keys`, which does not invoke them — that is what lets a discriminated union check its discriminator without resolving an option whose getters reference the union being constructed.
|
|
950
|
-
const propShapes = new WeakMap();
|
|
951
998
|
exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
952
999
|
// requires cast because technically $ZodObject doesn't extend
|
|
953
1000
|
exports.$ZodType.init(inst, def);
|
|
954
|
-
// const sh = def.shape;
|
|
955
1001
|
const desc = Object.getOwnPropertyDescriptor(def, "shape");
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
});
|
|
1002
|
+
// a cloned def carries its source's accessor, which knows the shape it answers from; adopting that keeps the clone's keys readable without running it
|
|
1003
|
+
const sh = desc?.get ? desc.get.raw : (def.shape ?? {});
|
|
1004
|
+
if (sh) {
|
|
1005
|
+
// Freezes the shape on first read, so its getters resolve once and every later read sees the same schemas.
|
|
1006
|
+
const get = () => {
|
|
1007
|
+
const newSh = { ...sh };
|
|
1008
|
+
Object.defineProperty(def, "shape", { value: newSh });
|
|
1009
|
+
get.raw = newSh;
|
|
1010
|
+
return newSh;
|
|
1011
|
+
};
|
|
1012
|
+
get.raw = sh;
|
|
1013
|
+
Object.defineProperty(def, "shape", { get });
|
|
969
1014
|
}
|
|
970
1015
|
const _normalized = util.cached(() => normalizeDef(def));
|
|
971
1016
|
util.defineLazyInternal(inst, "propValues", (zod) => {
|
|
@@ -1006,7 +1051,14 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
|
1006
1051
|
payload.value = memo ? memo.alloc(inst, payload, {}, ctx) : {};
|
|
1007
1052
|
const proms = [];
|
|
1008
1053
|
const shape = value.shape;
|
|
1054
|
+
const abortEarly = ctx?.abortEarly;
|
|
1055
|
+
let seen = payload.issues.length;
|
|
1009
1056
|
for (const key of value.allKeys) {
|
|
1057
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1058
|
+
if (util.aborted(payload, seen))
|
|
1059
|
+
break;
|
|
1060
|
+
seen = payload.issues.length;
|
|
1061
|
+
}
|
|
1010
1062
|
if (key === "__proto__")
|
|
1011
1063
|
continue;
|
|
1012
1064
|
const el = shape[key];
|
|
@@ -1023,7 +1075,7 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
|
1023
1075
|
if (!catchall) {
|
|
1024
1076
|
return proms.length ? Promise.all(proms).then(() => payload) : payload;
|
|
1025
1077
|
}
|
|
1026
|
-
return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
|
|
1078
|
+
return handleCatchall(proms, input, payload, ctx, _normalized.value, inst, abortEarly === true);
|
|
1027
1079
|
};
|
|
1028
1080
|
});
|
|
1029
1081
|
exports.$ZodObjectJIT = core.$constructor("$ZodObjectJIT", (inst, def) => {
|
|
@@ -1038,12 +1090,18 @@ exports.$ZodObjectJIT = core.$constructor("$ZodObjectJIT", (inst, def) => {
|
|
|
1038
1090
|
// a symbol has no source literal, so it is read as `syms[i]` off the closed-over scope
|
|
1039
1091
|
const doc = new doc_js_1.Doc(["payload", "ctx"], { shape, inst, memo, syms });
|
|
1040
1092
|
const parseStr = (k) => `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
|
|
1041
|
-
//
|
|
1093
|
+
// prefixes in place, like util.prefixIssues. newResult must land before the early return: a catchall runs after this and would otherwise write onto the caller's input
|
|
1042
1094
|
const prefixStr = (id, k) => `
|
|
1095
|
+
let ${id}_ab = false;
|
|
1043
1096
|
for (let i = 0; i < ${id}.issues.length; i++) {
|
|
1044
1097
|
const iss = ${id}.issues[i];
|
|
1045
1098
|
iss.path = iss.path ? [${k}, ...iss.path] : [${k}];
|
|
1046
1099
|
payload.issues.push(iss);
|
|
1100
|
+
if (iss.continue !== true) ${id}_ab = true;
|
|
1101
|
+
}
|
|
1102
|
+
if (${id}_ab && ctx && ctx.abortEarly) {
|
|
1103
|
+
payload.value = newResult;
|
|
1104
|
+
return payload;
|
|
1047
1105
|
}`;
|
|
1048
1106
|
doc.write(`const input = payload.value;`);
|
|
1049
1107
|
const ids = Object.create(null);
|
|
@@ -1092,6 +1150,10 @@ exports.$ZodObjectJIT = core.$constructor("$ZodObjectJIT", (inst, def) => {
|
|
|
1092
1150
|
input: undefined,
|
|
1093
1151
|
path: [${k}]
|
|
1094
1152
|
});
|
|
1153
|
+
if (ctx && ctx.abortEarly) {
|
|
1154
|
+
payload.value = newResult;
|
|
1155
|
+
return payload;
|
|
1156
|
+
}
|
|
1095
1157
|
}
|
|
1096
1158
|
|
|
1097
1159
|
if (${id}_present) {
|
|
@@ -1147,7 +1209,7 @@ exports.$ZodObjectJIT = core.$constructor("$ZodObjectJIT", (inst, def) => {
|
|
|
1147
1209
|
payload = fastpass(payload, ctx);
|
|
1148
1210
|
if (!catchall)
|
|
1149
1211
|
return payload;
|
|
1150
|
-
return handleCatchall([], input, payload, ctx, value, inst);
|
|
1212
|
+
return handleCatchall([], input, payload, ctx, value, inst, ctx?.abortEarly === true);
|
|
1151
1213
|
}
|
|
1152
1214
|
return superParse(payload, ctx);
|
|
1153
1215
|
};
|
|
@@ -1284,22 +1346,38 @@ exports.$ZodXor = core.$constructor("$ZodXor", (inst, def) => {
|
|
|
1284
1346
|
});
|
|
1285
1347
|
};
|
|
1286
1348
|
});
|
|
1287
|
-
/** Returns the option
|
|
1349
|
+
/** Returns the option whose discriminator claims `value`, or throws if ambiguous. */
|
|
1288
1350
|
function getDiscriminatedOption(union, value) {
|
|
1289
1351
|
const internals = union._zod;
|
|
1290
1352
|
let map = internals.bag.optionsMap;
|
|
1291
1353
|
if (!map) {
|
|
1292
|
-
map =
|
|
1293
|
-
const { options, discriminator } = internals.def;
|
|
1294
|
-
for (const option of options) {
|
|
1295
|
-
// First declaration wins, matching the order the parse path resolves a duplicate in.
|
|
1296
|
-
for (const v of option._zod.propValues?.[discriminator] ?? [])
|
|
1297
|
-
if (!map.has(v))
|
|
1298
|
-
map.set(v, option);
|
|
1299
|
-
}
|
|
1354
|
+
map = discriminatorMap(internals.def);
|
|
1300
1355
|
internals.bag.optionsMap = map;
|
|
1301
1356
|
}
|
|
1302
|
-
|
|
1357
|
+
const option = map.get(value);
|
|
1358
|
+
if (option === null)
|
|
1359
|
+
throw new Error(`Ambiguous discriminator value "${String(value)}"`);
|
|
1360
|
+
return option;
|
|
1361
|
+
}
|
|
1362
|
+
function discriminatorMap(def) {
|
|
1363
|
+
const map = new Map();
|
|
1364
|
+
for (const option of def.options) {
|
|
1365
|
+
const values = option._zod.propValues?.[def.discriminator];
|
|
1366
|
+
if (!values || values.size === 0)
|
|
1367
|
+
throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(option)}"`);
|
|
1368
|
+
for (const value of values) {
|
|
1369
|
+
if (map.has(value)) {
|
|
1370
|
+
if (value !== undefined)
|
|
1371
|
+
throw new Error(`Duplicate discriminator value "${String(value)}"`);
|
|
1372
|
+
// keep the collision marked so a later member cannot reclaim it
|
|
1373
|
+
map.set(value, null);
|
|
1374
|
+
}
|
|
1375
|
+
else {
|
|
1376
|
+
map.set(value, option);
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
return map;
|
|
1303
1381
|
}
|
|
1304
1382
|
exports.$ZodDiscriminatedUnion =
|
|
1305
1383
|
/*@__PURE__*/
|
|
@@ -1309,10 +1387,13 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
|
1309
1387
|
const _super = inst._zod.parse;
|
|
1310
1388
|
util.defineLazyInternal(inst, "propValues", (zod) => {
|
|
1311
1389
|
const propValues = {};
|
|
1390
|
+
let undefinedCount = 0;
|
|
1312
1391
|
for (const option of zod.def.options) {
|
|
1313
1392
|
const pv = option._zod.propValues;
|
|
1314
1393
|
if (!pv || Object.keys(pv).length === 0)
|
|
1315
1394
|
throw new Error(`Invalid discriminated union option at index "${zod.def.options.indexOf(option)}"`);
|
|
1395
|
+
if (pv[zod.def.discriminator]?.has(undefined))
|
|
1396
|
+
undefinedCount++;
|
|
1316
1397
|
for (const [k, v] of Object.entries(pv)) {
|
|
1317
1398
|
if (!Object.prototype.hasOwnProperty.call(propValues, k)) {
|
|
1318
1399
|
util.assignProp(propValues, k, new Set());
|
|
@@ -1322,31 +1403,18 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
|
1322
1403
|
}
|
|
1323
1404
|
}
|
|
1324
1405
|
}
|
|
1406
|
+
if (!zod.def.unionFallback && undefinedCount > 1)
|
|
1407
|
+
propValues[zod.def.discriminator]?.delete(undefined);
|
|
1325
1408
|
return propValues;
|
|
1326
1409
|
});
|
|
1327
|
-
// Checked now rather than in the lookup map below, so an option that lacks the discriminator fails at the `discriminatedUnion` call instead of on the first object parsed. Options whose shape cannot be enumerated without resolving it — pipes
|
|
1410
|
+
// Checked now rather than in the lookup map below, so an option that lacks the discriminator fails at the `discriminatedUnion` call instead of on the first object parsed. Options whose shape cannot be enumerated without resolving it — pipes and lazies — are left to the map.
|
|
1328
1411
|
def.options.forEach((option, i) => {
|
|
1329
|
-
const propShape =
|
|
1412
|
+
const propShape = util.rawShape(option._zod.def);
|
|
1330
1413
|
if (propShape && !Object.prototype.hasOwnProperty.call(propShape, def.discriminator)) {
|
|
1331
1414
|
throw new Error(`Invalid discriminated union option at index "${i}"`);
|
|
1332
1415
|
}
|
|
1333
1416
|
});
|
|
1334
|
-
const disc = util.cached(() =>
|
|
1335
|
-
const opts = def.options;
|
|
1336
|
-
const map = new Map();
|
|
1337
|
-
for (const o of opts) {
|
|
1338
|
-
const values = o._zod.propValues?.[def.discriminator];
|
|
1339
|
-
if (!values || values.size === 0)
|
|
1340
|
-
throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(o)}"`);
|
|
1341
|
-
for (const v of values) {
|
|
1342
|
-
if (map.has(v)) {
|
|
1343
|
-
throw new Error(`Duplicate discriminator value "${String(v)}"`);
|
|
1344
|
-
}
|
|
1345
|
-
map.set(v, o);
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
return map;
|
|
1349
|
-
});
|
|
1417
|
+
const disc = util.cached(() => discriminatorMap(def));
|
|
1350
1418
|
inst._zod.parse = (payload, ctx) => {
|
|
1351
1419
|
const input = payload.value;
|
|
1352
1420
|
if (!util.isObject(input)) {
|
|
@@ -1358,8 +1426,10 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
|
1358
1426
|
});
|
|
1359
1427
|
return payload;
|
|
1360
1428
|
}
|
|
1361
|
-
const
|
|
1362
|
-
|
|
1429
|
+
const value = input?.[def.discriminator];
|
|
1430
|
+
const opt = disc.value.get(value);
|
|
1431
|
+
// forward metadata cannot choose an encoder for an absent tag
|
|
1432
|
+
if (opt && (value !== undefined || ctx.direction !== "backward")) {
|
|
1363
1433
|
return opt._zod.run(payload, ctx);
|
|
1364
1434
|
}
|
|
1365
1435
|
// Fall back to union matching when the fast discriminator path fails:
|
|
@@ -1374,7 +1444,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
|
1374
1444
|
errors: [],
|
|
1375
1445
|
note: "No matching discriminator",
|
|
1376
1446
|
discriminator: def.discriminator,
|
|
1377
|
-
options: Array.from(disc.value.keys()),
|
|
1447
|
+
options: Array.from(disc.value.keys()).filter((value) => disc.value.get(value) !== null),
|
|
1378
1448
|
input,
|
|
1379
1449
|
path: [def.discriminator],
|
|
1380
1450
|
inst,
|
|
@@ -1547,6 +1617,9 @@ exports.$ZodTuple = core.$constructor("$ZodTuple", (inst, def) => {
|
|
|
1547
1617
|
}
|
|
1548
1618
|
// Run every item in parallel, collecting results into an indexed array. The post-processing in `handleTupleResults` walks them in order so it can decide whether an absent optional-output error can truncate the tail or must be reported to preserve required output.
|
|
1549
1619
|
const itemResults = new Array(items.length);
|
|
1620
|
+
// only tracked when there is a rest loop to skip
|
|
1621
|
+
const abortEarly = def.rest ? ctx?.abortEarly : undefined;
|
|
1622
|
+
let itemAborted = false;
|
|
1550
1623
|
for (let i = 0; i < items.length; i++) {
|
|
1551
1624
|
const r = items[i]._zod.run({ value: input[i], issues: [] }, ctx);
|
|
1552
1625
|
if (r instanceof Promise) {
|
|
@@ -1556,12 +1629,21 @@ exports.$ZodTuple = core.$constructor("$ZodTuple", (inst, def) => {
|
|
|
1556
1629
|
}
|
|
1557
1630
|
else {
|
|
1558
1631
|
itemResults[i] = r;
|
|
1632
|
+
if (abortEarly && !itemAborted && r.issues.length)
|
|
1633
|
+
itemAborted = util.aborted(r);
|
|
1559
1634
|
}
|
|
1560
1635
|
}
|
|
1561
|
-
|
|
1636
|
+
// sound because rest is non-empty exactly when every fixed index is present, the one case handleTupleResults cannot discard an item's issues
|
|
1637
|
+
if (def.rest && !itemAborted) {
|
|
1562
1638
|
let i = items.length - 1;
|
|
1563
1639
|
const rest = input.slice(items.length);
|
|
1640
|
+
let seen = payload.issues.length;
|
|
1564
1641
|
for (const el of rest) {
|
|
1642
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1643
|
+
if (util.aborted(payload, seen))
|
|
1644
|
+
break;
|
|
1645
|
+
seen = payload.issues.length;
|
|
1646
|
+
}
|
|
1565
1647
|
i++;
|
|
1566
1648
|
const result = def.rest._zod.run({ value: el, issues: [] }, ctx);
|
|
1567
1649
|
if (result instanceof Promise) {
|
|
@@ -1643,6 +1725,7 @@ exports.$ZodRecord = core.$constructor("$ZodRecord", (inst, def) => {
|
|
|
1643
1725
|
});
|
|
1644
1726
|
return payload;
|
|
1645
1727
|
}
|
|
1728
|
+
// no guard in either loop below: a record's invalid_key aborts but an enclosing intersection can reconcile it, so a stopped loop hides keys the sibling does not own and the intersection then rejects nothing
|
|
1646
1729
|
const proms = [];
|
|
1647
1730
|
const values = def.keyType._zod.values;
|
|
1648
1731
|
if (values && !def.partial) {
|
|
@@ -1814,7 +1897,14 @@ exports.$ZodMap = core.$constructor("$ZodMap", (inst, def) => {
|
|
|
1814
1897
|
}
|
|
1815
1898
|
const proms = [];
|
|
1816
1899
|
payload.value = memo ? memo.alloc(inst, payload, new Map(), ctx) : new Map();
|
|
1900
|
+
const abortEarly = ctx?.abortEarly;
|
|
1901
|
+
let seen = payload.issues.length;
|
|
1817
1902
|
for (const [key, value] of input) {
|
|
1903
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1904
|
+
if (util.aborted(payload, seen))
|
|
1905
|
+
break;
|
|
1906
|
+
seen = payload.issues.length;
|
|
1907
|
+
}
|
|
1818
1908
|
const keyResult = def.keyType._zod.run({ value: key, issues: [] }, ctx);
|
|
1819
1909
|
const valueResult = def.valueType._zod.run({ value: value, issues: [] }, ctx);
|
|
1820
1910
|
if (keyResult instanceof Promise || valueResult instanceof Promise) {
|
|
@@ -1880,7 +1970,14 @@ exports.$ZodSet = core.$constructor("$ZodSet", (inst, def) => {
|
|
|
1880
1970
|
}
|
|
1881
1971
|
const proms = [];
|
|
1882
1972
|
payload.value = memo ? memo.alloc(inst, payload, new Set(), ctx) : new Set();
|
|
1973
|
+
const abortEarly = ctx?.abortEarly;
|
|
1974
|
+
let seen = payload.issues.length;
|
|
1883
1975
|
for (const item of input) {
|
|
1976
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1977
|
+
if (util.aborted(payload, seen))
|
|
1978
|
+
break;
|
|
1979
|
+
seen = payload.issues.length;
|
|
1980
|
+
}
|
|
1884
1981
|
const result = def.valueType._zod.run({ value: item, issues: [] }, ctx);
|
|
1885
1982
|
if (result instanceof Promise) {
|
|
1886
1983
|
proms.push(result.then((result) => handleSetResult(result, payload)));
|
|
@@ -1904,9 +2001,11 @@ exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
|
|
|
1904
2001
|
const values = util.getEnumValues(def.entries);
|
|
1905
2002
|
const valuesSet = new Set(values);
|
|
1906
2003
|
inst._zod.values = valuesSet;
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
2004
|
+
util.defineLazyInternal(inst, "pattern", (zod) => {
|
|
2005
|
+
const patternValues = util.getEnumValues(zod.def.entries).filter((k) => util.propertyKeyTypes.has(typeof k));
|
|
2006
|
+
// unmatchable fallback, RE2-safe: an empty alternation would compile to /^()$/, which matches ""
|
|
2007
|
+
return new RegExp(patternValues.length ? `^(${patternValues.map((o) => util.escapeRegex(o.toString())).join("|")})$` : "^[^\\s\\S]$");
|
|
2008
|
+
});
|
|
1910
2009
|
inst._zod.parse = (payload, _ctx) => {
|
|
1911
2010
|
const input = payload.value;
|
|
1912
2011
|
if (valuesSet.has(input)) {
|
|
@@ -1925,12 +2024,15 @@ exports.$ZodLiteral = core.$constructor("$ZodLiteral", (inst, def) => {
|
|
|
1925
2024
|
exports.$ZodType.init(inst, def);
|
|
1926
2025
|
const values = new Set(def.values);
|
|
1927
2026
|
inst._zod.values = values;
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
2027
|
+
util.defineLazyInternal(inst, "pattern", (zod) => {
|
|
2028
|
+
const vals = zod.def.values;
|
|
2029
|
+
// unmatchable fallback, RE2-safe: an empty alternation would compile to /^()$/, which matches ""
|
|
2030
|
+
return new RegExp(vals.length
|
|
2031
|
+
? `^(${vals
|
|
2032
|
+
.map((o) => typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o))
|
|
2033
|
+
.join("|")})$`
|
|
2034
|
+
: "^[^\\s\\S]$");
|
|
2035
|
+
});
|
|
1934
2036
|
inst._zod.parse = (payload, _ctx) => {
|
|
1935
2037
|
const input = payload.value;
|
|
1936
2038
|
if (values.has(input)) {
|
|
@@ -2294,22 +2396,67 @@ function handleReadonlyResult(payload) {
|
|
|
2294
2396
|
payload.value = Object.freeze(payload.value);
|
|
2295
2397
|
return payload;
|
|
2296
2398
|
}
|
|
2399
|
+
// a leaf's pattern source with its own checks folded in: the last pattern-carrying check wins, else length bounds narrow the catch-all, else an integer format narrows the number form. the fold lives here instead of on `_zod.pattern` so a bundle without template literals never pays for it
|
|
2400
|
+
function leafPattern(schema) {
|
|
2401
|
+
const def = schema._zod.def;
|
|
2402
|
+
let pattern = def.pattern;
|
|
2403
|
+
let isInt = !!def.format?.includes("int");
|
|
2404
|
+
let minimum;
|
|
2405
|
+
let maximum;
|
|
2406
|
+
for (const ch of def.checks ?? []) {
|
|
2407
|
+
const d = ch._zod.def;
|
|
2408
|
+
if (d.pattern)
|
|
2409
|
+
pattern = d.pattern;
|
|
2410
|
+
isInt || (isInt = !!d.format?.includes("int"));
|
|
2411
|
+
const lo = d.minimum ?? d.length;
|
|
2412
|
+
const hi = d.maximum ?? d.length;
|
|
2413
|
+
if (lo !== undefined && (minimum === undefined || lo > minimum))
|
|
2414
|
+
minimum = lo;
|
|
2415
|
+
if (hi !== undefined && (maximum === undefined || hi < maximum))
|
|
2416
|
+
maximum = hi;
|
|
2417
|
+
}
|
|
2418
|
+
if (pattern)
|
|
2419
|
+
return pattern.source;
|
|
2420
|
+
// an empty range matches nothing at runtime, and `{8,5}` is not a legal quantifier
|
|
2421
|
+
if (minimum !== undefined && maximum !== undefined && minimum > maximum)
|
|
2422
|
+
return "(?!)";
|
|
2423
|
+
if (minimum !== undefined || maximum !== undefined)
|
|
2424
|
+
return regexes.string({ minimum, maximum }).source;
|
|
2425
|
+
const own = schema._zod.pattern;
|
|
2426
|
+
return (isInt && own === regexes.number ? regexes.integer : own)?.source;
|
|
2427
|
+
}
|
|
2428
|
+
// a part's pattern source. a wrapper's pattern embeds its inner pattern's source verbatim, so the folded form is substituted in place without knowing the wrapper's own composition; a union's options are joined the way the union builds its own pattern
|
|
2429
|
+
function partPattern(schema) {
|
|
2430
|
+
const def = schema._zod.def;
|
|
2431
|
+
const own = schema._zod.pattern?.source;
|
|
2432
|
+
// lazy resolves its inner on the internals, not the def
|
|
2433
|
+
const inner = def.innerType ?? schema._zod.innerType;
|
|
2434
|
+
if (inner) {
|
|
2435
|
+
const before = inner._zod.pattern?.source;
|
|
2436
|
+
const after = partPattern(inner);
|
|
2437
|
+
if (own && before && after && after !== before) {
|
|
2438
|
+
return own.replace(util.cleanRegex(before), () => util.cleanRegex(after));
|
|
2439
|
+
}
|
|
2440
|
+
return own;
|
|
2441
|
+
}
|
|
2442
|
+
if (def.options) {
|
|
2443
|
+
const sources = def.options.map(partPattern);
|
|
2444
|
+
if (sources.every(Boolean))
|
|
2445
|
+
return `^(${sources.map((s) => util.cleanRegex(s)).join("|")})$`;
|
|
2446
|
+
}
|
|
2447
|
+
return leafPattern(schema);
|
|
2448
|
+
}
|
|
2297
2449
|
exports.$ZodTemplateLiteral = core.$constructor("$ZodTemplateLiteral", (inst, def) => {
|
|
2298
2450
|
exports.$ZodType.init(inst, def);
|
|
2299
2451
|
const regexParts = [];
|
|
2300
2452
|
for (const part of def.parts) {
|
|
2301
2453
|
if (typeof part === "object" && part !== null) {
|
|
2302
2454
|
// is Zod schema
|
|
2303
|
-
|
|
2304
|
-
|
|
2455
|
+
const source = partPattern(part);
|
|
2456
|
+
if (!source) {
|
|
2305
2457
|
throw new Error(`Invalid template literal part, no pattern found: ${[...part._zod.traits].shift()}`);
|
|
2306
2458
|
}
|
|
2307
|
-
|
|
2308
|
-
if (!source)
|
|
2309
|
-
throw new Error(`Invalid template literal part: ${part._zod.traits}`);
|
|
2310
|
-
const start = source.startsWith("^") ? 1 : 0;
|
|
2311
|
-
const end = source.endsWith("$") ? source.length - 1 : source.length;
|
|
2312
|
-
regexParts.push(source.slice(start, end));
|
|
2459
|
+
regexParts.push(util.cleanRegex(source));
|
|
2313
2460
|
}
|
|
2314
2461
|
else if (part === null || util.primitiveTypes.has(typeof part)) {
|
|
2315
2462
|
regexParts.push(util.escapeRegex(`${part}`));
|
|
@@ -2479,3 +2626,83 @@ function handleRefineResult(result, payload, input, inst) {
|
|
|
2479
2626
|
payload.issues.push(util.issue(_iss));
|
|
2480
2627
|
}
|
|
2481
2628
|
}
|
|
2629
|
+
// asserts in place: the child result's value is discarded, matching z.property(), because a nested object or array schema rebuilds its output even when nothing transformed
|
|
2630
|
+
function handlePropertiesResult(result, payload, key) {
|
|
2631
|
+
if (result.issues.length) {
|
|
2632
|
+
payload.issues.push(...util.prefixIssues(key, result.issues));
|
|
2633
|
+
}
|
|
2634
|
+
}
|
|
2635
|
+
exports.$ZodProperties = core.$constructor("$ZodProperties", (inst, def) => {
|
|
2636
|
+
// $ZodType.init prepends an instance that already carries the $ZodCheck trait to its own `checks`, which would run the shape a second time and cost the parse context. Initializing the check trait after it keeps the schema role in `parse`, where the context arrives.
|
|
2637
|
+
exports.$ZodType.init(inst, def);
|
|
2638
|
+
checks.$ZodCheck.init(inst, def);
|
|
2639
|
+
const memo = core.globalConfig.memoizer;
|
|
2640
|
+
memo?.attach(inst);
|
|
2641
|
+
// key and schema snapshotted together: reading one live and the other cached lets a later mutation of the caller's shape object pair a stale key with a missing schema
|
|
2642
|
+
let entries;
|
|
2643
|
+
const runShape = (payload, ctx) => {
|
|
2644
|
+
entries ?? (entries = Reflect.ownKeys(def.shape).map((key) => [key, def.shape[key]]));
|
|
2645
|
+
const input = payload.value;
|
|
2646
|
+
let proms;
|
|
2647
|
+
for (const [key, schema] of entries) {
|
|
2648
|
+
const result = schema._zod.run({ value: input[key], issues: [] }, ctx);
|
|
2649
|
+
if (result instanceof Promise) {
|
|
2650
|
+
proms ?? (proms = []);
|
|
2651
|
+
proms.push(result.then((result) => handlePropertiesResult(result, payload, key)));
|
|
2652
|
+
}
|
|
2653
|
+
else {
|
|
2654
|
+
handlePropertiesResult(result, payload, key);
|
|
2655
|
+
}
|
|
2656
|
+
}
|
|
2657
|
+
if (proms)
|
|
2658
|
+
return Promise.all(proms).then(() => undefined);
|
|
2659
|
+
return undefined;
|
|
2660
|
+
};
|
|
2661
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2662
|
+
const input = payload.value;
|
|
2663
|
+
// as a schema this is the type gate, and it infers an object shape, so a primitive is a type error. A function passes: its properties read like any other object's, and z.instanceof allows one.
|
|
2664
|
+
if (input === null || (typeof input !== "object" && typeof input !== "function")) {
|
|
2665
|
+
payload.issues.push({ expected: "object", code: "invalid_type", input, inst });
|
|
2666
|
+
return payload;
|
|
2667
|
+
}
|
|
2668
|
+
// both sides declare the shape's input type, so the assertion runs forward in either direction; encoding the children backward would reject the very type this schema claims to take
|
|
2669
|
+
if (ctx.direction === "backward")
|
|
2670
|
+
ctx = { ...ctx, direction: "forward" };
|
|
2671
|
+
// the input is its own output here, so it registers as its own memo entry: a cycle re-entering this node hits the bucket instead of recursing forever
|
|
2672
|
+
if (memo)
|
|
2673
|
+
memo.alloc(inst, payload, input, ctx);
|
|
2674
|
+
const result = runShape(payload, ctx);
|
|
2675
|
+
return result instanceof Promise ? result.then(() => payload) : payload;
|
|
2676
|
+
};
|
|
2677
|
+
// as a check the base schema already typed the value, so this only asserts the properties — which read on a primitive too, matching z.property() on a string's length. It gets no context of its own, so a cycle through a spread schema is not tracked.
|
|
2678
|
+
inst._zod.check = (payload) => {
|
|
2679
|
+
if (payload.value == null) {
|
|
2680
|
+
payload.issues.push({ expected: "object", code: "invalid_type", input: payload.value, inst });
|
|
2681
|
+
return undefined;
|
|
2682
|
+
}
|
|
2683
|
+
return runShape(payload, {});
|
|
2684
|
+
};
|
|
2685
|
+
}, {
|
|
2686
|
+
*[Symbol.iterator]() {
|
|
2687
|
+
yield this;
|
|
2688
|
+
},
|
|
2689
|
+
});
|
|
2690
|
+
|
|
2691
|
+
// seal-cjs-exports
|
|
2692
|
+
(function () {
|
|
2693
|
+
var keys = Object.getOwnPropertyNames(exports);
|
|
2694
|
+
for (var i = 0; i < keys.length; i++) {
|
|
2695
|
+
var desc = Object.getOwnPropertyDescriptor(exports, keys[i]);
|
|
2696
|
+
if (!desc || !desc.get || !desc.configurable) continue;
|
|
2697
|
+
var value;
|
|
2698
|
+
try {
|
|
2699
|
+
value = desc.get();
|
|
2700
|
+
} catch (e) {
|
|
2701
|
+
continue;
|
|
2702
|
+
}
|
|
2703
|
+
// a circular require may not have settled this one yet, so leave it live
|
|
2704
|
+
if (value === undefined) continue;
|
|
2705
|
+
Object.defineProperty(exports, keys[i], { value: value, writable: false, enumerable: desc.enumerable, configurable: false });
|
|
2706
|
+
}
|
|
2707
|
+
Object.freeze(exports);
|
|
2708
|
+
})();
|