zod 4.5.4 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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/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 +1 -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 +193 -81
- 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 +314 -83
- 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/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/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/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/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 +164 -65
- package/v4/core/compile.d.cts +18 -4
- package/v4/core/compile.d.ts +18 -4
- package/v4/core/compile.js +145 -66
- 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 +285 -66
- package/v4/core/schemas.d.cts +34 -3
- package/v4/core/schemas.d.ts +34 -3
- package/v4/core/schemas.js +264 -65
- 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 +19 -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/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.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as checks from "./checks.js";
|
|
2
2
|
import * as core from "./core.js";
|
|
3
3
|
import { Doc } from "./doc.js";
|
|
4
|
-
import { parse, parseAsync
|
|
4
|
+
import { parse, parseAsync } from "./parse.js";
|
|
5
5
|
import * as regexes from "./regexes.js";
|
|
6
6
|
import * as util from "./util.js";
|
|
7
7
|
import { version } from "./versions.js";
|
|
@@ -128,16 +128,24 @@ export const $ZodType = /*@__PURE__*/ core.$constructor("$ZodType", (inst, def)
|
|
|
128
128
|
},
|
|
129
129
|
});
|
|
130
130
|
/** The Standard Schema surface for `inst`. Shared so wrappers can extend it without forcing it. */
|
|
131
|
-
|
|
131
|
+
// 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
|
|
132
|
+
const toStandardResult = (r, ctx) => r.issues.length ? { issues: r.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())) } : { value: r.value };
|
|
133
|
+
async function validateAsync(inst, value) {
|
|
134
|
+
const ctx = { async: true };
|
|
135
|
+
return toStandardResult((await inst._zod.run({ value, issues: [] }, ctx)), ctx);
|
|
136
|
+
}
|
|
132
137
|
export function standardProps(inst) {
|
|
133
138
|
return {
|
|
134
139
|
validate: (value) => {
|
|
140
|
+
const ctx = { async: false };
|
|
135
141
|
try {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return safeParseAsync(inst, value).then(toStandardResult);
|
|
142
|
+
const r = inst._zod.run({ value, issues: [] }, ctx);
|
|
143
|
+
if (!(r instanceof Promise))
|
|
144
|
+
return toStandardResult(r, ctx);
|
|
140
145
|
}
|
|
146
|
+
catch (_) { }
|
|
147
|
+
// async function so a synchronously throwing check rejects instead of escaping validate
|
|
148
|
+
return validateAsync(inst, value);
|
|
141
149
|
},
|
|
142
150
|
vendor: "zod",
|
|
143
151
|
version: 1,
|
|
@@ -146,7 +154,8 @@ export function standardProps(inst) {
|
|
|
146
154
|
export { clone } from "./util.js";
|
|
147
155
|
export const $ZodString = /*@__PURE__*/ core.$constructor("$ZodString", (inst, def) => {
|
|
148
156
|
$ZodType.init(inst, def);
|
|
149
|
-
|
|
157
|
+
// a format's own pattern, else unbounded; a template literal derives the check-aware form itself
|
|
158
|
+
inst._zod.pattern = def.pattern ?? regexes.anyString;
|
|
150
159
|
inst._zod.parse = (payload, _) => {
|
|
151
160
|
if (def.coerce)
|
|
152
161
|
try {
|
|
@@ -332,13 +341,6 @@ export const $ZodKSUID = /*@__PURE__*/ core.$constructor("$ZodKSUID", (inst, def
|
|
|
332
341
|
export const $ZodISODateTime = /*@__PURE__*/ core.$constructor("$ZodISODateTime", (inst, def) => {
|
|
333
342
|
def.pattern ?? (def.pattern = regexes.datetime(def));
|
|
334
343
|
$ZodStringFormat.init(inst, def);
|
|
335
|
-
// 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
|
|
336
|
-
if (def.local || def.precision === -1) {
|
|
337
|
-
inst._zod.bag.laxFormat = true;
|
|
338
|
-
inst._zod.onattach.push((s) => {
|
|
339
|
-
s._zod.bag.laxFormat = true;
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
344
|
});
|
|
343
345
|
export const $ZodISODate = /*@__PURE__*/ core.$constructor("$ZodISODate", (inst, def) => {
|
|
344
346
|
def.pattern ?? (def.pattern = regexes.date);
|
|
@@ -355,7 +357,6 @@ export const $ZodISODuration = /*@__PURE__*/ core.$constructor("$ZodISODuration"
|
|
|
355
357
|
export const $ZodIPv4 = /*@__PURE__*/ core.$constructor("$ZodIPv4", (inst, def) => {
|
|
356
358
|
def.pattern ?? (def.pattern = regexes.ipv4);
|
|
357
359
|
$ZodStringFormat.init(inst, def);
|
|
358
|
-
inst._zod.bag.format = `ipv4`;
|
|
359
360
|
});
|
|
360
361
|
/** 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`. */
|
|
361
362
|
const ipv6Alphabet = /^[0-9a-fA-F:.]+$/;
|
|
@@ -374,7 +375,6 @@ export function isValidIPv6(value) {
|
|
|
374
375
|
export const $ZodIPv6 = /*@__PURE__*/ core.$constructor("$ZodIPv6", (inst, def) => {
|
|
375
376
|
def.pattern ?? (def.pattern = regexes.ipv6);
|
|
376
377
|
$ZodStringFormat.init(inst, def);
|
|
377
|
-
inst._zod.bag.format = `ipv6`;
|
|
378
378
|
inst._zod.check = (payload) => {
|
|
379
379
|
if (!isValidIPv6(payload.value)) {
|
|
380
380
|
payload.issues.push({
|
|
@@ -390,7 +390,6 @@ export const $ZodIPv6 = /*@__PURE__*/ core.$constructor("$ZodIPv6", (inst, def)
|
|
|
390
390
|
export const $ZodMAC = /*@__PURE__*/ core.$constructor("$ZodMAC", (inst, def) => {
|
|
391
391
|
def.pattern ?? (def.pattern = regexes.mac(def.delimiter));
|
|
392
392
|
$ZodStringFormat.init(inst, def);
|
|
393
|
-
inst._zod.bag.format = `mac`;
|
|
394
393
|
});
|
|
395
394
|
export const $ZodCIDRv4 = /*@__PURE__*/ core.$constructor("$ZodCIDRv4", (inst, def) => {
|
|
396
395
|
def.pattern ?? (def.pattern = regexes.cidrv4);
|
|
@@ -443,10 +442,11 @@ export function isValidBase64(data) {
|
|
|
443
442
|
return false;
|
|
444
443
|
}
|
|
445
444
|
}
|
|
445
|
+
// 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
|
|
446
|
+
export const base64Charset = /^[0-9a-zA-Z+/]*={0,2}$/;
|
|
446
447
|
export const $ZodBase64 = /*@__PURE__*/ core.$constructor("$ZodBase64", (inst, def) => {
|
|
447
|
-
def.pattern ?? (def.pattern =
|
|
448
|
+
def.pattern ?? (def.pattern = base64Charset);
|
|
448
449
|
$ZodStringFormat.init(inst, def);
|
|
449
|
-
inst._zod.bag.contentEncoding = "base64";
|
|
450
450
|
inst._zod.check = (payload) => {
|
|
451
451
|
if (isValidBase64(payload.value))
|
|
452
452
|
return;
|
|
@@ -459,18 +459,19 @@ export const $ZodBase64 = /*@__PURE__*/ core.$constructor("$ZodBase64", (inst, d
|
|
|
459
459
|
});
|
|
460
460
|
};
|
|
461
461
|
});
|
|
462
|
-
//////////////////////////////
|
|
462
|
+
////////////////////////////// ZodBase64URL //////////////////////////////
|
|
463
|
+
// lax on purpose: the quantified regexes.base64url overflows the regex stack on multi-MB input; isValidBase64 enforces length on the padded string
|
|
464
|
+
export const base64urlCharset = /^[A-Za-z0-9_-]*$/;
|
|
463
465
|
export function isValidBase64URL(data) {
|
|
464
|
-
if (!
|
|
466
|
+
if (!base64urlCharset.test(data))
|
|
465
467
|
return false;
|
|
466
468
|
const base64 = data.replace(/[-_]/g, (c) => (c === "-" ? "+" : "/"));
|
|
467
469
|
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
468
470
|
return isValidBase64(padded);
|
|
469
471
|
}
|
|
470
472
|
export const $ZodBase64URL = /*@__PURE__*/ core.$constructor("$ZodBase64URL", (inst, def) => {
|
|
471
|
-
def.pattern ?? (def.pattern =
|
|
473
|
+
def.pattern ?? (def.pattern = base64urlCharset);
|
|
472
474
|
$ZodStringFormat.init(inst, def);
|
|
473
|
-
inst._zod.bag.contentEncoding = "base64url";
|
|
474
475
|
inst._zod.check = (payload) => {
|
|
475
476
|
if (isValidBase64URL(payload.value))
|
|
476
477
|
return;
|
|
@@ -495,7 +496,7 @@ function isLuhnAlgo(digits) {
|
|
|
495
496
|
let bit = 1;
|
|
496
497
|
let sum = 0;
|
|
497
498
|
while (length) {
|
|
498
|
-
const value =
|
|
499
|
+
const value = digits.charCodeAt(--length) - 48;
|
|
499
500
|
bit ^= 1;
|
|
500
501
|
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][value] : value;
|
|
501
502
|
}
|
|
@@ -522,6 +523,42 @@ export const $ZodCreditCard = /*@__PURE__*/ core.$constructor("$ZodCreditCard",
|
|
|
522
523
|
});
|
|
523
524
|
};
|
|
524
525
|
});
|
|
526
|
+
////////////////////////////// ZodIBAN //////////////////////////////
|
|
527
|
+
// iso 7064 mod 97-10 checksum without BigInt
|
|
528
|
+
function isIso7064Mod97(iban) {
|
|
529
|
+
let remainder = 0;
|
|
530
|
+
const len = iban.length;
|
|
531
|
+
for (let i = 4; i < len; i++) {
|
|
532
|
+
const code = iban.charCodeAt(i);
|
|
533
|
+
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
534
|
+
}
|
|
535
|
+
for (let i = 0; i < 4; i++) {
|
|
536
|
+
const code = iban.charCodeAt(i);
|
|
537
|
+
remainder = (code >= 65 ? remainder * 100 + (code - 55) : remainder * 10 + (code - 48)) % 97;
|
|
538
|
+
}
|
|
539
|
+
return remainder === 1;
|
|
540
|
+
}
|
|
541
|
+
export function isValidIBAN(input) {
|
|
542
|
+
if (!regexes.iban.test(input))
|
|
543
|
+
return false;
|
|
544
|
+
return isIso7064Mod97(input);
|
|
545
|
+
}
|
|
546
|
+
export const $ZodIBAN = /*@__PURE__*/ core.$constructor("$ZodIBAN", (inst, def) => {
|
|
547
|
+
// shape only — checksum is not expressible as a pattern
|
|
548
|
+
def.pattern ?? (def.pattern = regexes.iban);
|
|
549
|
+
$ZodStringFormat.init(inst, def);
|
|
550
|
+
inst._zod.check = (payload) => {
|
|
551
|
+
if (isValidIBAN(payload.value))
|
|
552
|
+
return;
|
|
553
|
+
payload.issues.push({
|
|
554
|
+
code: "invalid_format",
|
|
555
|
+
format: "iban",
|
|
556
|
+
input: payload.value,
|
|
557
|
+
inst,
|
|
558
|
+
continue: !def.abort,
|
|
559
|
+
});
|
|
560
|
+
};
|
|
561
|
+
});
|
|
525
562
|
////////////////////////////// ZodJWT //////////////////////////////
|
|
526
563
|
export function isValidJWT(token, algorithm = null) {
|
|
527
564
|
try {
|
|
@@ -575,7 +612,7 @@ export const $ZodCustomStringFormat = /*@__PURE__*/ core.$constructor("$ZodCusto
|
|
|
575
612
|
});
|
|
576
613
|
export const $ZodNumber = /*@__PURE__*/ core.$constructor("$ZodNumber", (inst, def) => {
|
|
577
614
|
$ZodType.init(inst, def);
|
|
578
|
-
inst._zod.pattern =
|
|
615
|
+
inst._zod.pattern = regexes.number;
|
|
579
616
|
inst._zod.parse = (payload, _ctx) => {
|
|
580
617
|
if (def.coerce)
|
|
581
618
|
try {
|
|
@@ -783,6 +820,7 @@ export const $ZodArray = /*@__PURE__*/ core.$constructor("$ZodArray", (inst, def
|
|
|
783
820
|
}
|
|
784
821
|
payload.value = memo ? memo.alloc(inst, payload, Array(input.length), ctx) : Array(input.length);
|
|
785
822
|
const proms = [];
|
|
823
|
+
const abortEarly = ctx?.abortEarly;
|
|
786
824
|
for (let i = 0; i < input.length; i++) {
|
|
787
825
|
const item = input[i];
|
|
788
826
|
const result = def.element._zod.run({
|
|
@@ -794,6 +832,9 @@ export const $ZodArray = /*@__PURE__*/ core.$constructor("$ZodArray", (inst, def
|
|
|
794
832
|
}
|
|
795
833
|
else {
|
|
796
834
|
handleArrayResult(result, payload, i);
|
|
835
|
+
// the element's payload is authoritative here, since handleArrayResult forwards every issue; an object's is not, because it drops a failed absent optional
|
|
836
|
+
if (abortEarly && result.issues.length !== 0 && util.aborted(result))
|
|
837
|
+
break;
|
|
797
838
|
}
|
|
798
839
|
}
|
|
799
840
|
if (proms.length) {
|
|
@@ -860,14 +901,21 @@ function normalizeDef(def) {
|
|
|
860
901
|
optionalKeys: new Set(okeys),
|
|
861
902
|
};
|
|
862
903
|
}
|
|
863
|
-
function handleCatchall(proms, input, payload, ctx, def, inst) {
|
|
904
|
+
function handleCatchall(proms, input, payload, ctx, def, inst, abortEarly) {
|
|
864
905
|
const unrecognized = [];
|
|
865
906
|
const keySet = def.keySet;
|
|
866
907
|
const _catchall = def.catchall._zod;
|
|
867
908
|
const t = _catchall.def.type;
|
|
868
909
|
const optin = _catchall.optin;
|
|
869
910
|
const optout = _catchall.optout;
|
|
911
|
+
// starts at 0, not the current length: the shape phase already ran and may have aborted
|
|
912
|
+
let seen = 0;
|
|
870
913
|
for (const key in input) {
|
|
914
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
915
|
+
if (util.aborted(payload, seen))
|
|
916
|
+
break;
|
|
917
|
+
seen = payload.issues.length;
|
|
918
|
+
}
|
|
871
919
|
// Must precede the __proto__ branch: a declared key is not unrecognized, even though the shape loop deliberately strips __proto__ from the parsed output.
|
|
872
920
|
if (keySet.has(key))
|
|
873
921
|
continue;
|
|
@@ -905,26 +953,22 @@ function handleCatchall(proms, input, payload, ctx, def, inst) {
|
|
|
905
953
|
return payload;
|
|
906
954
|
});
|
|
907
955
|
}
|
|
908
|
-
// 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.
|
|
909
|
-
const propShapes = new WeakMap();
|
|
910
956
|
export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, def) => {
|
|
911
957
|
// requires cast because technically $ZodObject doesn't extend
|
|
912
958
|
$ZodType.init(inst, def);
|
|
913
|
-
// const sh = def.shape;
|
|
914
959
|
const desc = Object.getOwnPropertyDescriptor(def, "shape");
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
});
|
|
960
|
+
// 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
|
|
961
|
+
const sh = desc?.get ? desc.get.raw : (def.shape ?? {});
|
|
962
|
+
if (sh) {
|
|
963
|
+
// Freezes the shape on first read, so its getters resolve once and every later read sees the same schemas.
|
|
964
|
+
const get = () => {
|
|
965
|
+
const newSh = { ...sh };
|
|
966
|
+
Object.defineProperty(def, "shape", { value: newSh });
|
|
967
|
+
get.raw = newSh;
|
|
968
|
+
return newSh;
|
|
969
|
+
};
|
|
970
|
+
get.raw = sh;
|
|
971
|
+
Object.defineProperty(def, "shape", { get });
|
|
928
972
|
}
|
|
929
973
|
const _normalized = util.cached(() => normalizeDef(def));
|
|
930
974
|
util.defineLazyInternal(inst, "propValues", (zod) => {
|
|
@@ -965,7 +1009,14 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
|
|
|
965
1009
|
payload.value = memo ? memo.alloc(inst, payload, {}, ctx) : {};
|
|
966
1010
|
const proms = [];
|
|
967
1011
|
const shape = value.shape;
|
|
1012
|
+
const abortEarly = ctx?.abortEarly;
|
|
1013
|
+
let seen = payload.issues.length;
|
|
968
1014
|
for (const key of value.allKeys) {
|
|
1015
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1016
|
+
if (util.aborted(payload, seen))
|
|
1017
|
+
break;
|
|
1018
|
+
seen = payload.issues.length;
|
|
1019
|
+
}
|
|
969
1020
|
if (key === "__proto__")
|
|
970
1021
|
continue;
|
|
971
1022
|
const el = shape[key];
|
|
@@ -982,7 +1033,7 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
|
|
|
982
1033
|
if (!catchall) {
|
|
983
1034
|
return proms.length ? Promise.all(proms).then(() => payload) : payload;
|
|
984
1035
|
}
|
|
985
|
-
return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
|
|
1036
|
+
return handleCatchall(proms, input, payload, ctx, _normalized.value, inst, abortEarly === true);
|
|
986
1037
|
};
|
|
987
1038
|
});
|
|
988
1039
|
export const $ZodObjectJIT = /*@__PURE__*/ core.$constructor("$ZodObjectJIT", (inst, def) => {
|
|
@@ -997,12 +1048,18 @@ export const $ZodObjectJIT = /*@__PURE__*/ core.$constructor("$ZodObjectJIT", (i
|
|
|
997
1048
|
// a symbol has no source literal, so it is read as `syms[i]` off the closed-over scope
|
|
998
1049
|
const doc = new Doc(["payload", "ctx"], { shape, inst, memo, syms });
|
|
999
1050
|
const parseStr = (k) => `shape[${k}]._zod.run({ value: input[${k}], issues: [] }, ctx)`;
|
|
1000
|
-
//
|
|
1051
|
+
// 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
|
|
1001
1052
|
const prefixStr = (id, k) => `
|
|
1053
|
+
let ${id}_ab = false;
|
|
1002
1054
|
for (let i = 0; i < ${id}.issues.length; i++) {
|
|
1003
1055
|
const iss = ${id}.issues[i];
|
|
1004
1056
|
iss.path = iss.path ? [${k}, ...iss.path] : [${k}];
|
|
1005
1057
|
payload.issues.push(iss);
|
|
1058
|
+
if (iss.continue !== true) ${id}_ab = true;
|
|
1059
|
+
}
|
|
1060
|
+
if (${id}_ab && ctx && ctx.abortEarly) {
|
|
1061
|
+
payload.value = newResult;
|
|
1062
|
+
return payload;
|
|
1006
1063
|
}`;
|
|
1007
1064
|
doc.write(`const input = payload.value;`);
|
|
1008
1065
|
const ids = Object.create(null);
|
|
@@ -1051,6 +1108,10 @@ export const $ZodObjectJIT = /*@__PURE__*/ core.$constructor("$ZodObjectJIT", (i
|
|
|
1051
1108
|
input: undefined,
|
|
1052
1109
|
path: [${k}]
|
|
1053
1110
|
});
|
|
1111
|
+
if (ctx && ctx.abortEarly) {
|
|
1112
|
+
payload.value = newResult;
|
|
1113
|
+
return payload;
|
|
1114
|
+
}
|
|
1054
1115
|
}
|
|
1055
1116
|
|
|
1056
1117
|
if (${id}_present) {
|
|
@@ -1106,7 +1167,7 @@ export const $ZodObjectJIT = /*@__PURE__*/ core.$constructor("$ZodObjectJIT", (i
|
|
|
1106
1167
|
payload = fastpass(payload, ctx);
|
|
1107
1168
|
if (!catchall)
|
|
1108
1169
|
return payload;
|
|
1109
|
-
return handleCatchall([], input, payload, ctx, value, inst);
|
|
1170
|
+
return handleCatchall([], input, payload, ctx, value, inst, ctx?.abortEarly === true);
|
|
1110
1171
|
}
|
|
1111
1172
|
return superParse(payload, ctx);
|
|
1112
1173
|
};
|
|
@@ -1283,9 +1344,9 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
|
1283
1344
|
}
|
|
1284
1345
|
return propValues;
|
|
1285
1346
|
});
|
|
1286
|
-
// 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
|
|
1347
|
+
// 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.
|
|
1287
1348
|
def.options.forEach((option, i) => {
|
|
1288
|
-
const propShape =
|
|
1349
|
+
const propShape = util.rawShape(option._zod.def);
|
|
1289
1350
|
if (propShape && !Object.prototype.hasOwnProperty.call(propShape, def.discriminator)) {
|
|
1290
1351
|
throw new Error(`Invalid discriminated union option at index "${i}"`);
|
|
1291
1352
|
}
|
|
@@ -1506,6 +1567,9 @@ export const $ZodTuple = /*@__PURE__*/ core.$constructor("$ZodTuple", (inst, def
|
|
|
1506
1567
|
}
|
|
1507
1568
|
// 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.
|
|
1508
1569
|
const itemResults = new Array(items.length);
|
|
1570
|
+
// only tracked when there is a rest loop to skip
|
|
1571
|
+
const abortEarly = def.rest ? ctx?.abortEarly : undefined;
|
|
1572
|
+
let itemAborted = false;
|
|
1509
1573
|
for (let i = 0; i < items.length; i++) {
|
|
1510
1574
|
const r = items[i]._zod.run({ value: input[i], issues: [] }, ctx);
|
|
1511
1575
|
if (r instanceof Promise) {
|
|
@@ -1515,12 +1579,21 @@ export const $ZodTuple = /*@__PURE__*/ core.$constructor("$ZodTuple", (inst, def
|
|
|
1515
1579
|
}
|
|
1516
1580
|
else {
|
|
1517
1581
|
itemResults[i] = r;
|
|
1582
|
+
if (abortEarly && !itemAborted && r.issues.length)
|
|
1583
|
+
itemAborted = util.aborted(r);
|
|
1518
1584
|
}
|
|
1519
1585
|
}
|
|
1520
|
-
|
|
1586
|
+
// sound because rest is non-empty exactly when every fixed index is present, the one case handleTupleResults cannot discard an item's issues
|
|
1587
|
+
if (def.rest && !itemAborted) {
|
|
1521
1588
|
let i = items.length - 1;
|
|
1522
1589
|
const rest = input.slice(items.length);
|
|
1590
|
+
let seen = payload.issues.length;
|
|
1523
1591
|
for (const el of rest) {
|
|
1592
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1593
|
+
if (util.aborted(payload, seen))
|
|
1594
|
+
break;
|
|
1595
|
+
seen = payload.issues.length;
|
|
1596
|
+
}
|
|
1524
1597
|
i++;
|
|
1525
1598
|
const result = def.rest._zod.run({ value: el, issues: [] }, ctx);
|
|
1526
1599
|
if (result instanceof Promise) {
|
|
@@ -1602,6 +1675,7 @@ export const $ZodRecord = /*@__PURE__*/ core.$constructor("$ZodRecord", (inst, d
|
|
|
1602
1675
|
});
|
|
1603
1676
|
return payload;
|
|
1604
1677
|
}
|
|
1678
|
+
// 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
|
|
1605
1679
|
const proms = [];
|
|
1606
1680
|
const values = def.keyType._zod.values;
|
|
1607
1681
|
if (values && !def.partial) {
|
|
@@ -1773,7 +1847,14 @@ export const $ZodMap = /*@__PURE__*/ core.$constructor("$ZodMap", (inst, def) =>
|
|
|
1773
1847
|
}
|
|
1774
1848
|
const proms = [];
|
|
1775
1849
|
payload.value = memo ? memo.alloc(inst, payload, new Map(), ctx) : new Map();
|
|
1850
|
+
const abortEarly = ctx?.abortEarly;
|
|
1851
|
+
let seen = payload.issues.length;
|
|
1776
1852
|
for (const [key, value] of input) {
|
|
1853
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1854
|
+
if (util.aborted(payload, seen))
|
|
1855
|
+
break;
|
|
1856
|
+
seen = payload.issues.length;
|
|
1857
|
+
}
|
|
1777
1858
|
const keyResult = def.keyType._zod.run({ value: key, issues: [] }, ctx);
|
|
1778
1859
|
const valueResult = def.valueType._zod.run({ value: value, issues: [] }, ctx);
|
|
1779
1860
|
if (keyResult instanceof Promise || valueResult instanceof Promise) {
|
|
@@ -1839,7 +1920,14 @@ export const $ZodSet = /*@__PURE__*/ core.$constructor("$ZodSet", (inst, def) =>
|
|
|
1839
1920
|
}
|
|
1840
1921
|
const proms = [];
|
|
1841
1922
|
payload.value = memo ? memo.alloc(inst, payload, new Set(), ctx) : new Set();
|
|
1923
|
+
const abortEarly = ctx?.abortEarly;
|
|
1924
|
+
let seen = payload.issues.length;
|
|
1842
1925
|
for (const item of input) {
|
|
1926
|
+
if (abortEarly && payload.issues.length !== seen) {
|
|
1927
|
+
if (util.aborted(payload, seen))
|
|
1928
|
+
break;
|
|
1929
|
+
seen = payload.issues.length;
|
|
1930
|
+
}
|
|
1843
1931
|
const result = def.valueType._zod.run({ value: item, issues: [] }, ctx);
|
|
1844
1932
|
if (result instanceof Promise) {
|
|
1845
1933
|
proms.push(result.then((result) => handleSetResult(result, payload)));
|
|
@@ -1863,9 +1951,11 @@ export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def)
|
|
|
1863
1951
|
const values = util.getEnumValues(def.entries);
|
|
1864
1952
|
const valuesSet = new Set(values);
|
|
1865
1953
|
inst._zod.values = valuesSet;
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1954
|
+
util.defineLazyInternal(inst, "pattern", (zod) => {
|
|
1955
|
+
const patternValues = util.getEnumValues(zod.def.entries).filter((k) => util.propertyKeyTypes.has(typeof k));
|
|
1956
|
+
// unmatchable fallback, RE2-safe: an empty alternation would compile to /^()$/, which matches ""
|
|
1957
|
+
return new RegExp(patternValues.length ? `^(${patternValues.map((o) => util.escapeRegex(o.toString())).join("|")})$` : "^[^\\s\\S]$");
|
|
1958
|
+
});
|
|
1869
1959
|
inst._zod.parse = (payload, _ctx) => {
|
|
1870
1960
|
const input = payload.value;
|
|
1871
1961
|
if (valuesSet.has(input)) {
|
|
@@ -1884,12 +1974,15 @@ export const $ZodLiteral = /*@__PURE__*/ core.$constructor("$ZodLiteral", (inst,
|
|
|
1884
1974
|
$ZodType.init(inst, def);
|
|
1885
1975
|
const values = new Set(def.values);
|
|
1886
1976
|
inst._zod.values = values;
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1977
|
+
util.defineLazyInternal(inst, "pattern", (zod) => {
|
|
1978
|
+
const vals = zod.def.values;
|
|
1979
|
+
// unmatchable fallback, RE2-safe: an empty alternation would compile to /^()$/, which matches ""
|
|
1980
|
+
return new RegExp(vals.length
|
|
1981
|
+
? `^(${vals
|
|
1982
|
+
.map((o) => typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o))
|
|
1983
|
+
.join("|")})$`
|
|
1984
|
+
: "^[^\\s\\S]$");
|
|
1985
|
+
});
|
|
1893
1986
|
inst._zod.parse = (payload, _ctx) => {
|
|
1894
1987
|
const input = payload.value;
|
|
1895
1988
|
if (values.has(input)) {
|
|
@@ -2253,22 +2346,67 @@ function handleReadonlyResult(payload) {
|
|
|
2253
2346
|
payload.value = Object.freeze(payload.value);
|
|
2254
2347
|
return payload;
|
|
2255
2348
|
}
|
|
2349
|
+
// 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
|
|
2350
|
+
function leafPattern(schema) {
|
|
2351
|
+
const def = schema._zod.def;
|
|
2352
|
+
let pattern = def.pattern;
|
|
2353
|
+
let isInt = !!def.format?.includes("int");
|
|
2354
|
+
let minimum;
|
|
2355
|
+
let maximum;
|
|
2356
|
+
for (const ch of def.checks ?? []) {
|
|
2357
|
+
const d = ch._zod.def;
|
|
2358
|
+
if (d.pattern)
|
|
2359
|
+
pattern = d.pattern;
|
|
2360
|
+
isInt || (isInt = !!d.format?.includes("int"));
|
|
2361
|
+
const lo = d.minimum ?? d.length;
|
|
2362
|
+
const hi = d.maximum ?? d.length;
|
|
2363
|
+
if (lo !== undefined && (minimum === undefined || lo > minimum))
|
|
2364
|
+
minimum = lo;
|
|
2365
|
+
if (hi !== undefined && (maximum === undefined || hi < maximum))
|
|
2366
|
+
maximum = hi;
|
|
2367
|
+
}
|
|
2368
|
+
if (pattern)
|
|
2369
|
+
return pattern.source;
|
|
2370
|
+
// an empty range matches nothing at runtime, and `{8,5}` is not a legal quantifier
|
|
2371
|
+
if (minimum !== undefined && maximum !== undefined && minimum > maximum)
|
|
2372
|
+
return "(?!)";
|
|
2373
|
+
if (minimum !== undefined || maximum !== undefined)
|
|
2374
|
+
return regexes.string({ minimum, maximum }).source;
|
|
2375
|
+
const own = schema._zod.pattern;
|
|
2376
|
+
return (isInt && own === regexes.number ? regexes.integer : own)?.source;
|
|
2377
|
+
}
|
|
2378
|
+
// 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
|
|
2379
|
+
function partPattern(schema) {
|
|
2380
|
+
const def = schema._zod.def;
|
|
2381
|
+
const own = schema._zod.pattern?.source;
|
|
2382
|
+
// lazy resolves its inner on the internals, not the def
|
|
2383
|
+
const inner = def.innerType ?? schema._zod.innerType;
|
|
2384
|
+
if (inner) {
|
|
2385
|
+
const before = inner._zod.pattern?.source;
|
|
2386
|
+
const after = partPattern(inner);
|
|
2387
|
+
if (own && before && after && after !== before) {
|
|
2388
|
+
return own.replace(util.cleanRegex(before), () => util.cleanRegex(after));
|
|
2389
|
+
}
|
|
2390
|
+
return own;
|
|
2391
|
+
}
|
|
2392
|
+
if (def.options) {
|
|
2393
|
+
const sources = def.options.map(partPattern);
|
|
2394
|
+
if (sources.every(Boolean))
|
|
2395
|
+
return `^(${sources.map((s) => util.cleanRegex(s)).join("|")})$`;
|
|
2396
|
+
}
|
|
2397
|
+
return leafPattern(schema);
|
|
2398
|
+
}
|
|
2256
2399
|
export const $ZodTemplateLiteral = /*@__PURE__*/ core.$constructor("$ZodTemplateLiteral", (inst, def) => {
|
|
2257
2400
|
$ZodType.init(inst, def);
|
|
2258
2401
|
const regexParts = [];
|
|
2259
2402
|
for (const part of def.parts) {
|
|
2260
2403
|
if (typeof part === "object" && part !== null) {
|
|
2261
2404
|
// is Zod schema
|
|
2262
|
-
|
|
2263
|
-
|
|
2405
|
+
const source = partPattern(part);
|
|
2406
|
+
if (!source) {
|
|
2264
2407
|
throw new Error(`Invalid template literal part, no pattern found: ${[...part._zod.traits].shift()}`);
|
|
2265
2408
|
}
|
|
2266
|
-
|
|
2267
|
-
if (!source)
|
|
2268
|
-
throw new Error(`Invalid template literal part: ${part._zod.traits}`);
|
|
2269
|
-
const start = source.startsWith("^") ? 1 : 0;
|
|
2270
|
-
const end = source.endsWith("$") ? source.length - 1 : source.length;
|
|
2271
|
-
regexParts.push(source.slice(start, end));
|
|
2409
|
+
regexParts.push(util.cleanRegex(source));
|
|
2272
2410
|
}
|
|
2273
2411
|
else if (part === null || util.primitiveTypes.has(typeof part)) {
|
|
2274
2412
|
regexParts.push(util.escapeRegex(`${part}`));
|
|
@@ -2438,3 +2576,64 @@ function handleRefineResult(result, payload, input, inst) {
|
|
|
2438
2576
|
payload.issues.push(util.issue(_iss));
|
|
2439
2577
|
}
|
|
2440
2578
|
}
|
|
2579
|
+
// 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
|
|
2580
|
+
function handlePropertiesResult(result, payload, key) {
|
|
2581
|
+
if (result.issues.length) {
|
|
2582
|
+
payload.issues.push(...util.prefixIssues(key, result.issues));
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
export const $ZodProperties = /*@__PURE__*/ core.$constructor("$ZodProperties", (inst, def) => {
|
|
2586
|
+
// $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.
|
|
2587
|
+
$ZodType.init(inst, def);
|
|
2588
|
+
checks.$ZodCheck.init(inst, def);
|
|
2589
|
+
const memo = core.globalConfig.memoizer;
|
|
2590
|
+
memo?.attach(inst);
|
|
2591
|
+
// 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
|
|
2592
|
+
let entries;
|
|
2593
|
+
const runShape = (payload, ctx) => {
|
|
2594
|
+
entries ?? (entries = Reflect.ownKeys(def.shape).map((key) => [key, def.shape[key]]));
|
|
2595
|
+
const input = payload.value;
|
|
2596
|
+
let proms;
|
|
2597
|
+
for (const [key, schema] of entries) {
|
|
2598
|
+
const result = schema._zod.run({ value: input[key], issues: [] }, ctx);
|
|
2599
|
+
if (result instanceof Promise) {
|
|
2600
|
+
proms ?? (proms = []);
|
|
2601
|
+
proms.push(result.then((result) => handlePropertiesResult(result, payload, key)));
|
|
2602
|
+
}
|
|
2603
|
+
else {
|
|
2604
|
+
handlePropertiesResult(result, payload, key);
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
if (proms)
|
|
2608
|
+
return Promise.all(proms).then(() => undefined);
|
|
2609
|
+
return undefined;
|
|
2610
|
+
};
|
|
2611
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2612
|
+
const input = payload.value;
|
|
2613
|
+
// 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.
|
|
2614
|
+
if (input === null || (typeof input !== "object" && typeof input !== "function")) {
|
|
2615
|
+
payload.issues.push({ expected: "object", code: "invalid_type", input, inst });
|
|
2616
|
+
return payload;
|
|
2617
|
+
}
|
|
2618
|
+
// 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
|
|
2619
|
+
if (ctx.direction === "backward")
|
|
2620
|
+
ctx = { ...ctx, direction: "forward" };
|
|
2621
|
+
// 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
|
|
2622
|
+
if (memo)
|
|
2623
|
+
memo.alloc(inst, payload, input, ctx);
|
|
2624
|
+
const result = runShape(payload, ctx);
|
|
2625
|
+
return result instanceof Promise ? result.then(() => payload) : payload;
|
|
2626
|
+
};
|
|
2627
|
+
// 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.
|
|
2628
|
+
inst._zod.check = (payload) => {
|
|
2629
|
+
if (payload.value == null) {
|
|
2630
|
+
payload.issues.push({ expected: "object", code: "invalid_type", input: payload.value, inst });
|
|
2631
|
+
return undefined;
|
|
2632
|
+
}
|
|
2633
|
+
return runShape(payload, {});
|
|
2634
|
+
};
|
|
2635
|
+
}, {
|
|
2636
|
+
*[Symbol.iterator]() {
|
|
2637
|
+
yield this;
|
|
2638
|
+
},
|
|
2639
|
+
});
|
|
@@ -1,2 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
|
|
4
|
+
// seal-cjs-exports
|
|
5
|
+
(function () {
|
|
6
|
+
var keys = Object.getOwnPropertyNames(exports);
|
|
7
|
+
for (var i = 0; i < keys.length; i++) {
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(exports, keys[i]);
|
|
9
|
+
if (!desc || !desc.get || !desc.configurable) continue;
|
|
10
|
+
var value;
|
|
11
|
+
try {
|
|
12
|
+
value = desc.get();
|
|
13
|
+
} catch (e) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
// a circular require may not have settled this one yet, so leave it live
|
|
17
|
+
if (value === undefined) continue;
|
|
18
|
+
Object.defineProperty(exports, keys[i], { value: value, writable: false, enumerable: desc.enumerable, configurable: false });
|
|
19
|
+
}
|
|
20
|
+
Object.freeze(exports);
|
|
21
|
+
})();
|