zod 4.5.3 → 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 +316 -0
- package/src/v4/classic/tests/default.test.ts +46 -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 +162 -36
- 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 +204 -36
- package/v4/core/memoizer.js +162 -36
- 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
|
@@ -3,9 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createStandardJSONSchemaMethod = exports.createToJSONSchemaMethod = void 0;
|
|
4
4
|
exports.initializeContext = initializeContext;
|
|
5
5
|
exports.handleUnrepresentable = handleUnrepresentable;
|
|
6
|
-
exports.
|
|
6
|
+
exports.processSchema = processSchema;
|
|
7
|
+
exports.process = processSchema;
|
|
8
|
+
exports.processSchema = processSchema;
|
|
9
|
+
exports.process = processSchema;
|
|
7
10
|
exports.extractDefs = extractDefs;
|
|
8
11
|
exports.finalize = finalize;
|
|
12
|
+
exports.isTransforming = isTransforming;
|
|
9
13
|
const registries_js_1 = require("./registries.cjs");
|
|
10
14
|
const util_js_1 = require("./util.cjs");
|
|
11
15
|
function assignProps(target, ...sources) {
|
|
@@ -67,7 +71,8 @@ function handleUnrepresentable(schema, ctx, json, params, message) {
|
|
|
67
71
|
Object.assign(json, result);
|
|
68
72
|
return true;
|
|
69
73
|
}
|
|
70
|
-
|
|
74
|
+
// never rename this back to `process`: bundler polyfills inject a top-level `const process` that a lexical declaration of the same name collides with (#6397)
|
|
75
|
+
function processSchema(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
71
76
|
var _a;
|
|
72
77
|
const def = schema._zod.def;
|
|
73
78
|
// check for schema in seens
|
|
@@ -113,7 +118,7 @@ function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
|
113
118
|
// Also set ref if processor didn't (for inheritance)
|
|
114
119
|
if (!result.ref)
|
|
115
120
|
result.ref = parent;
|
|
116
|
-
|
|
121
|
+
processSchema(parent, ctx, params);
|
|
117
122
|
ctx.seen.get(parent).isParent = true;
|
|
118
123
|
}
|
|
119
124
|
}
|
|
@@ -250,8 +255,6 @@ function extractDefs(ctx, schema
|
|
|
250
255
|
if (seen.count > 1) {
|
|
251
256
|
if (ctx.reused === "ref") {
|
|
252
257
|
extractToDef(entry);
|
|
253
|
-
// biome-ignore lint:
|
|
254
|
-
continue;
|
|
255
258
|
}
|
|
256
259
|
}
|
|
257
260
|
}
|
|
@@ -645,7 +648,7 @@ function isTransforming(_schema, _ctx) {
|
|
|
645
648
|
*/
|
|
646
649
|
const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
|
|
647
650
|
const ctx = initializeContext({ ...params, processors });
|
|
648
|
-
|
|
651
|
+
processSchema(schema, ctx);
|
|
649
652
|
extractDefs(ctx, schema);
|
|
650
653
|
return finalize(ctx, schema);
|
|
651
654
|
};
|
|
@@ -653,8 +656,27 @@ exports.createToJSONSchemaMethod = createToJSONSchemaMethod;
|
|
|
653
656
|
const createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
|
|
654
657
|
const { libraryOptions, target } = params ?? {};
|
|
655
658
|
const ctx = initializeContext({ ...(libraryOptions ?? {}), target, io, processors });
|
|
656
|
-
|
|
659
|
+
processSchema(schema, ctx);
|
|
657
660
|
extractDefs(ctx, schema);
|
|
658
661
|
return finalize(ctx, schema);
|
|
659
662
|
};
|
|
660
663
|
exports.createStandardJSONSchemaMethod = createStandardJSONSchemaMethod;
|
|
664
|
+
|
|
665
|
+
// seal-cjs-exports
|
|
666
|
+
(function () {
|
|
667
|
+
var keys = Object.getOwnPropertyNames(exports);
|
|
668
|
+
for (var i = 0; i < keys.length; i++) {
|
|
669
|
+
var desc = Object.getOwnPropertyDescriptor(exports, keys[i]);
|
|
670
|
+
if (!desc || !desc.get || !desc.configurable) continue;
|
|
671
|
+
var value;
|
|
672
|
+
try {
|
|
673
|
+
value = desc.get();
|
|
674
|
+
} catch (e) {
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
// a circular require may not have settled this one yet, so leave it live
|
|
678
|
+
if (value === undefined) continue;
|
|
679
|
+
Object.defineProperty(exports, keys[i], { value: value, writable: false, enumerable: desc.enumerable, configurable: false });
|
|
680
|
+
}
|
|
681
|
+
Object.freeze(exports);
|
|
682
|
+
})();
|
|
@@ -101,7 +101,7 @@ export interface ToJSONSchemaContext {
|
|
|
101
101
|
* covers both passes in `finalize`: the ref flattening and the `$defs` build.
|
|
102
102
|
*
|
|
103
103
|
* The passes are valid only while nothing they read has changed, so both are cleared in
|
|
104
|
-
* `
|
|
104
|
+
* `processSchema()` when the map grows, and in `JSONSchemaGenerator.emit()`, which can also change
|
|
105
105
|
* the `cycles` and `reused` they branch on.
|
|
106
106
|
*
|
|
107
107
|
* One case is deliberately not covered: an `override` callback that writes to
|
|
@@ -132,9 +132,14 @@ export declare function initializeContext(params: JSONSchemaGeneratorParams): To
|
|
|
132
132
|
* custom JSON Schema was written into `json`, in which case the caller must not write its own.
|
|
133
133
|
*/
|
|
134
134
|
export declare function handleUnrepresentable(schema: schemas.$ZodType, ctx: ToJSONSchemaContext, json: JSONSchema.BaseSchema, params: ProcessParams, message: string): boolean;
|
|
135
|
-
export declare function
|
|
135
|
+
export declare function processSchema<T extends schemas.$ZodType>(schema: T, ctx: ToJSONSchemaContext, _params?: ProcessParams): JSONSchema.BaseSchema;
|
|
136
|
+
/** @deprecated Renamed to `processSchema`. An export alias declares no binding, so it is safe to keep. */
|
|
137
|
+
export { processSchema as process };
|
|
136
138
|
export declare function extractDefs<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): void;
|
|
137
139
|
export declare function finalize<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): ZodStandardJSONSchemaPayload<T>;
|
|
140
|
+
export declare function isTransforming(_schema: schemas.$ZodType, _ctx?: {
|
|
141
|
+
seen: Set<schemas.$ZodType>;
|
|
142
|
+
}): boolean;
|
|
138
143
|
export type ZodStandardSchemaWithJSON<T> = StandardSchemaWithJSONProps<core.input<T>, core.output<T>>;
|
|
139
144
|
export interface ZodStandardJSONSchemaPayload<T> extends JSONSchema.BaseSchema {
|
|
140
145
|
"~standard": ZodStandardSchemaWithJSON<T>;
|
|
@@ -150,4 +155,3 @@ export declare const createToJSONSchemaMethod: <T extends schemas.$ZodType>(sche
|
|
|
150
155
|
*/
|
|
151
156
|
type StandardJSONSchemaMethodParams = Parameters<StandardJSONSchemaV1["~standard"]["jsonSchema"]["input"]>[0];
|
|
152
157
|
export declare const createStandardJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, io: "input" | "output", processors?: Record<string, Processor>) => (params?: StandardJSONSchemaMethodParams) => JSONSchema.BaseSchema;
|
|
153
|
-
export {};
|
|
@@ -101,7 +101,7 @@ export interface ToJSONSchemaContext {
|
|
|
101
101
|
* covers both passes in `finalize`: the ref flattening and the `$defs` build.
|
|
102
102
|
*
|
|
103
103
|
* The passes are valid only while nothing they read has changed, so both are cleared in
|
|
104
|
-
* `
|
|
104
|
+
* `processSchema()` when the map grows, and in `JSONSchemaGenerator.emit()`, which can also change
|
|
105
105
|
* the `cycles` and `reused` they branch on.
|
|
106
106
|
*
|
|
107
107
|
* One case is deliberately not covered: an `override` callback that writes to
|
|
@@ -132,9 +132,14 @@ export declare function initializeContext(params: JSONSchemaGeneratorParams): To
|
|
|
132
132
|
* custom JSON Schema was written into `json`, in which case the caller must not write its own.
|
|
133
133
|
*/
|
|
134
134
|
export declare function handleUnrepresentable(schema: schemas.$ZodType, ctx: ToJSONSchemaContext, json: JSONSchema.BaseSchema, params: ProcessParams, message: string): boolean;
|
|
135
|
-
export declare function
|
|
135
|
+
export declare function processSchema<T extends schemas.$ZodType>(schema: T, ctx: ToJSONSchemaContext, _params?: ProcessParams): JSONSchema.BaseSchema;
|
|
136
|
+
/** @deprecated Renamed to `processSchema`. An export alias declares no binding, so it is safe to keep. */
|
|
137
|
+
export { processSchema as process };
|
|
136
138
|
export declare function extractDefs<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): void;
|
|
137
139
|
export declare function finalize<T extends schemas.$ZodType>(ctx: ToJSONSchemaContext, schema: T): ZodStandardJSONSchemaPayload<T>;
|
|
140
|
+
export declare function isTransforming(_schema: schemas.$ZodType, _ctx?: {
|
|
141
|
+
seen: Set<schemas.$ZodType>;
|
|
142
|
+
}): boolean;
|
|
138
143
|
export type ZodStandardSchemaWithJSON<T> = StandardSchemaWithJSONProps<core.input<T>, core.output<T>>;
|
|
139
144
|
export interface ZodStandardJSONSchemaPayload<T> extends JSONSchema.BaseSchema {
|
|
140
145
|
"~standard": ZodStandardSchemaWithJSON<T>;
|
|
@@ -150,4 +155,3 @@ export declare const createToJSONSchemaMethod: <T extends schemas.$ZodType>(sche
|
|
|
150
155
|
*/
|
|
151
156
|
type StandardJSONSchemaMethodParams = Parameters<StandardJSONSchemaV1["~standard"]["jsonSchema"]["input"]>[0];
|
|
152
157
|
export declare const createStandardJSONSchemaMethod: <T extends schemas.$ZodType>(schema: T, io: "input" | "output", processors?: Record<string, Processor>) => (params?: StandardJSONSchemaMethodParams) => JSONSchema.BaseSchema;
|
|
153
|
-
export {};
|
|
@@ -59,7 +59,8 @@ export function handleUnrepresentable(schema, ctx, json, params, message) {
|
|
|
59
59
|
Object.assign(json, result);
|
|
60
60
|
return true;
|
|
61
61
|
}
|
|
62
|
-
|
|
62
|
+
// never rename this back to `process`: bundler polyfills inject a top-level `const process` that a lexical declaration of the same name collides with (#6397)
|
|
63
|
+
export function processSchema(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
63
64
|
var _a;
|
|
64
65
|
const def = schema._zod.def;
|
|
65
66
|
// check for schema in seens
|
|
@@ -105,7 +106,7 @@ export function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
|
105
106
|
// Also set ref if processor didn't (for inheritance)
|
|
106
107
|
if (!result.ref)
|
|
107
108
|
result.ref = parent;
|
|
108
|
-
|
|
109
|
+
processSchema(parent, ctx, params);
|
|
109
110
|
ctx.seen.get(parent).isParent = true;
|
|
110
111
|
}
|
|
111
112
|
}
|
|
@@ -126,6 +127,8 @@ export function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
|
126
127
|
const _result = ctx.seen.get(schema);
|
|
127
128
|
return _result.schema;
|
|
128
129
|
}
|
|
130
|
+
/** @deprecated Renamed to `processSchema`. An export alias declares no binding, so it is safe to keep. */
|
|
131
|
+
export { processSchema as process };
|
|
129
132
|
// Escape a reference token for use in a JSON Pointer fragment (RFC 6901): `~` becomes `~0` and `/` becomes `~1`. The `~` replacement must run first.
|
|
130
133
|
function encodeJSONPointerSegment(segment) {
|
|
131
134
|
return segment.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
@@ -242,8 +245,6 @@ export function extractDefs(ctx, schema
|
|
|
242
245
|
if (seen.count > 1) {
|
|
243
246
|
if (ctx.reused === "ref") {
|
|
244
247
|
extractToDef(entry);
|
|
245
|
-
// biome-ignore lint:
|
|
246
|
-
continue;
|
|
247
248
|
}
|
|
248
249
|
}
|
|
249
250
|
}
|
|
@@ -571,7 +572,7 @@ export function finalize(ctx, schema) {
|
|
|
571
572
|
throw new Error("Error converting schema to JSON.");
|
|
572
573
|
}
|
|
573
574
|
}
|
|
574
|
-
function isTransforming(_schema, _ctx) {
|
|
575
|
+
export function isTransforming(_schema, _ctx) {
|
|
575
576
|
const ctx = _ctx ?? { seen: new Set() };
|
|
576
577
|
if (ctx.seen.has(_schema))
|
|
577
578
|
return false;
|
|
@@ -637,14 +638,14 @@ function isTransforming(_schema, _ctx) {
|
|
|
637
638
|
*/
|
|
638
639
|
export const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
|
|
639
640
|
const ctx = initializeContext({ ...params, processors });
|
|
640
|
-
|
|
641
|
+
processSchema(schema, ctx);
|
|
641
642
|
extractDefs(ctx, schema);
|
|
642
643
|
return finalize(ctx, schema);
|
|
643
644
|
};
|
|
644
645
|
export const createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
|
|
645
646
|
const { libraryOptions, target } = params ?? {};
|
|
646
647
|
const ctx = initializeContext({ ...(libraryOptions ?? {}), target, io, processors });
|
|
647
|
-
|
|
648
|
+
processSchema(schema, ctx);
|
|
648
649
|
extractDefs(ctx, schema);
|
|
649
650
|
return finalize(ctx, schema);
|
|
650
651
|
};
|
package/v4/core/util.cjs
CHANGED
|
@@ -17,6 +17,7 @@ exports.floatSafeRemainder = floatSafeRemainder;
|
|
|
17
17
|
exports.defineLazy = defineLazy;
|
|
18
18
|
exports.objectClone = objectClone;
|
|
19
19
|
exports.assignProp = assignProp;
|
|
20
|
+
exports.rawShape = rawShape;
|
|
20
21
|
exports.mergeDefs = mergeDefs;
|
|
21
22
|
exports.cloneDef = cloneDef;
|
|
22
23
|
exports.getElementAtPath = getElementAtPath;
|
|
@@ -62,6 +63,7 @@ exports.uint8ArrayToHex = uint8ArrayToHex;
|
|
|
62
63
|
exports.members = members;
|
|
63
64
|
exports.own = own;
|
|
64
65
|
exports.hide = hide;
|
|
66
|
+
exports.derived = derived;
|
|
65
67
|
exports.defineLazyInternal = defineLazyInternal;
|
|
66
68
|
exports.installLazyProp = installLazyProp;
|
|
67
69
|
exports.constantCatch = constantCatch;
|
|
@@ -96,18 +98,23 @@ function jsonStringifyReplacer(_, value) {
|
|
|
96
98
|
return value.toString();
|
|
97
99
|
return value;
|
|
98
100
|
}
|
|
101
|
+
// the accessor lives on a shared prototype: an own accessor makes every box a dictionary-mode object (~360 B and a slow load per read against ~100 B and an inlined getter here)
|
|
102
|
+
class Cached {
|
|
103
|
+
constructor(getter) {
|
|
104
|
+
this._getter = getter;
|
|
105
|
+
this._value = undefined;
|
|
106
|
+
}
|
|
107
|
+
get value() {
|
|
108
|
+
const getter = this._getter;
|
|
109
|
+
if (getter !== undefined) {
|
|
110
|
+
this._value = getter();
|
|
111
|
+
this._getter = undefined;
|
|
112
|
+
}
|
|
113
|
+
return this._value;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
99
116
|
function cached(getter) {
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
get value() {
|
|
103
|
-
if (!set) {
|
|
104
|
-
const value = getter();
|
|
105
|
-
Object.defineProperty(this, "value", { value });
|
|
106
|
-
return value;
|
|
107
|
-
}
|
|
108
|
-
throw new Error("cached value already set");
|
|
109
|
-
},
|
|
110
|
-
};
|
|
117
|
+
return new Cached(getter);
|
|
111
118
|
}
|
|
112
119
|
function nullish(input) {
|
|
113
120
|
return input === null || input === undefined;
|
|
@@ -162,6 +169,71 @@ function assignProp(target, prop, value) {
|
|
|
162
169
|
configurable: true,
|
|
163
170
|
});
|
|
164
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Whichever object a def's `shape` currently answers from: the one the caller passed until the first read, the frozen copy after it.
|
|
174
|
+
*
|
|
175
|
+
* Its keys and descriptors read without invoking anything, which is what lets a discriminated union check its discriminator, and the cycle walk read a shape, without resolving a getter that references the schema being constructed. A def that answers `shape` from an accessor of its own has none.
|
|
176
|
+
*/
|
|
177
|
+
function rawShape(def) {
|
|
178
|
+
const desc = Object.getOwnPropertyDescriptor(def, "shape");
|
|
179
|
+
return desc?.get ? desc.get.raw : desc?.value;
|
|
180
|
+
}
|
|
181
|
+
// where a builder reads its source's keys and descriptors, resolving only a shape a def answers for itself. A shape resolves by object spread, so only its enumerable keys are ever part of it.
|
|
182
|
+
function sourceShape(schema) {
|
|
183
|
+
return rawShape(schema._zod.def) ?? schema._zod.def.shape;
|
|
184
|
+
}
|
|
185
|
+
// a key whose value is not settled yet, self-caching so every read after the first gets the same one
|
|
186
|
+
function deferProp(target, key, getter) {
|
|
187
|
+
Object.defineProperty(target, key, {
|
|
188
|
+
get() {
|
|
189
|
+
const value = getter();
|
|
190
|
+
assignProp(this, key, value);
|
|
191
|
+
return value;
|
|
192
|
+
},
|
|
193
|
+
enumerable: true,
|
|
194
|
+
configurable: true,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
// Writes a settled key. A plain assignment is much cheaper than `defineProperty` and produces the same descriptor, but it runs whatever setter already answers to the key — an accessor this shape deferred, or an inherited one, which `__proto__` has on every object and prototype pollution can add for any name.
|
|
198
|
+
function putProp(target, key, value) {
|
|
199
|
+
if (key in target)
|
|
200
|
+
assignProp(target, key, value);
|
|
201
|
+
else
|
|
202
|
+
target[key] = value;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Copies `keys` of `source`'s shape onto `target`, each value passed through `wrap`.
|
|
206
|
+
*
|
|
207
|
+
* A key the source has resolved is copied through now, so the derived shape states it outright and nothing has to resolve it to learn what it holds. A key the source still defers stays deferred, and reads back through the source's own `shape`, so it resolves once and both shapes get that one schema.
|
|
208
|
+
*/
|
|
209
|
+
function mirrorShape(target, source, keys, wrap) {
|
|
210
|
+
const raw = sourceShape(source);
|
|
211
|
+
for (const key of keys) {
|
|
212
|
+
const desc = Object.getOwnPropertyDescriptor(raw, key);
|
|
213
|
+
if (!desc.enumerable)
|
|
214
|
+
continue;
|
|
215
|
+
if (desc.get) {
|
|
216
|
+
deferProp(target, key, () => {
|
|
217
|
+
const value = source._zod.def.shape[key];
|
|
218
|
+
return wrap ? wrap(value, key) : value;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
else
|
|
222
|
+
putProp(target, key, wrap ? wrap(desc.value, key) : desc.value);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// same, for a plain shape a caller passed rather than a schema's
|
|
226
|
+
function mirrorProps(target, source) {
|
|
227
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
228
|
+
const desc = Object.getOwnPropertyDescriptor(source, key);
|
|
229
|
+
if (!desc.enumerable)
|
|
230
|
+
continue;
|
|
231
|
+
if (desc.get)
|
|
232
|
+
deferProp(target, key, () => source[key]);
|
|
233
|
+
else
|
|
234
|
+
putProp(target, key, desc.value);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
165
237
|
function mergeDefs(...defs) {
|
|
166
238
|
const mergedDescriptors = {};
|
|
167
239
|
for (const def of defs) {
|
|
@@ -414,24 +486,23 @@ function pick(schema, mask) {
|
|
|
414
486
|
if (hasChecks) {
|
|
415
487
|
throw new Error(".pick() cannot be used on object schemas containing refinements");
|
|
416
488
|
}
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
return clone(schema, def);
|
|
489
|
+
const newShape = {};
|
|
490
|
+
mirrorShape(newShape, schema, maskedKeys(schema, mask));
|
|
491
|
+
return clone(schema, mergeDefs(currDef, { shape: newShape, checks: [] }));
|
|
492
|
+
}
|
|
493
|
+
// the mask keys that select something, checked against the source's shape without resolving it
|
|
494
|
+
function maskedKeys(schema, mask) {
|
|
495
|
+
const raw = sourceShape(schema);
|
|
496
|
+
const keys = [];
|
|
497
|
+
// `for...in` skips symbols, so a symbol in the mask would select nothing
|
|
498
|
+
for (const key of Reflect.ownKeys(mask)) {
|
|
499
|
+
if (!Object.getOwnPropertyDescriptor(raw, key)?.enumerable) {
|
|
500
|
+
throw new Error(`Unrecognized key: "${String(key)}"`);
|
|
501
|
+
}
|
|
502
|
+
if (mask[key])
|
|
503
|
+
keys.push(key);
|
|
504
|
+
}
|
|
505
|
+
return keys;
|
|
435
506
|
}
|
|
436
507
|
function omit(schema, mask) {
|
|
437
508
|
const currDef = schema._zod.def;
|
|
@@ -440,23 +511,10 @@ function omit(schema, mask) {
|
|
|
440
511
|
if (hasChecks) {
|
|
441
512
|
throw new Error(".omit() cannot be used on object schemas containing refinements");
|
|
442
513
|
}
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
if (!Object.prototype.hasOwnProperty.call(currDef.shape, key)) {
|
|
448
|
-
throw new Error(`Unrecognized key: "${String(key)}"`);
|
|
449
|
-
}
|
|
450
|
-
if (!mask[key])
|
|
451
|
-
continue;
|
|
452
|
-
delete newShape[key];
|
|
453
|
-
}
|
|
454
|
-
assignProp(this, "shape", newShape); // self-caching
|
|
455
|
-
return newShape;
|
|
456
|
-
},
|
|
457
|
-
checks: [],
|
|
458
|
-
});
|
|
459
|
-
return clone(schema, def);
|
|
514
|
+
const omitted = new Set(maskedKeys(schema, mask));
|
|
515
|
+
const newShape = {};
|
|
516
|
+
mirrorShape(newShape, schema, Reflect.ownKeys(sourceShape(schema)).filter((key) => !omitted.has(key)));
|
|
517
|
+
return clone(schema, mergeDefs(currDef, { shape: newShape, checks: [] }));
|
|
460
518
|
}
|
|
461
519
|
function extend(schema, shape) {
|
|
462
520
|
if (!isPlainObject(shape)) {
|
|
@@ -466,34 +524,27 @@ function extend(schema, shape) {
|
|
|
466
524
|
const hasChecks = checks && checks.length > 0;
|
|
467
525
|
if (hasChecks) {
|
|
468
526
|
// Only throw if new shape overlaps with existing shape. Use getOwnPropertyDescriptor to check key existence without accessing values
|
|
469
|
-
const existingShape = schema
|
|
527
|
+
const existingShape = sourceShape(schema);
|
|
470
528
|
for (const key of Reflect.ownKeys(shape)) {
|
|
471
529
|
if (Object.getOwnPropertyDescriptor(existingShape, key) !== undefined) {
|
|
472
530
|
throw new Error("Cannot overwrite keys on object schemas containing refinements. Use `.safeExtend()` instead.");
|
|
473
531
|
}
|
|
474
532
|
}
|
|
475
533
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
return
|
|
534
|
+
return clone(schema, mergeDefs(schema._zod.def, { shape: extended(schema, shape) }));
|
|
535
|
+
}
|
|
536
|
+
// the source's keys, then the caller's overlaid on top
|
|
537
|
+
function extended(schema, shape) {
|
|
538
|
+
const newShape = {};
|
|
539
|
+
mirrorShape(newShape, schema, Reflect.ownKeys(sourceShape(schema)));
|
|
540
|
+
mirrorProps(newShape, shape);
|
|
541
|
+
return newShape;
|
|
484
542
|
}
|
|
485
543
|
function safeExtend(schema, shape) {
|
|
486
544
|
if (!isPlainObject(shape)) {
|
|
487
545
|
throw new Error("Invalid input to safeExtend: expected a plain object");
|
|
488
546
|
}
|
|
489
|
-
|
|
490
|
-
get shape() {
|
|
491
|
-
const _shape = { ...schema._zod.def.shape, ...shape };
|
|
492
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
493
|
-
return _shape;
|
|
494
|
-
},
|
|
495
|
-
});
|
|
496
|
-
return clone(schema, def);
|
|
547
|
+
return clone(schema, mergeDefs(schema._zod.def, { shape: extended(schema, shape) }));
|
|
497
548
|
}
|
|
498
549
|
function merge(a, b) {
|
|
499
550
|
if (!b?._zod?.def) {
|
|
@@ -502,12 +553,11 @@ function merge(a, b) {
|
|
|
502
553
|
if (a._zod.def.checks?.length) {
|
|
503
554
|
throw new Error(".merge() cannot be used on object schemas containing refinements. Use .safeExtend() instead.");
|
|
504
555
|
}
|
|
556
|
+
const newShape = {};
|
|
557
|
+
mirrorShape(newShape, a, Reflect.ownKeys(sourceShape(a)));
|
|
558
|
+
mirrorShape(newShape, b, Reflect.ownKeys(sourceShape(b)));
|
|
505
559
|
const def = mergeDefs(a._zod.def, {
|
|
506
|
-
|
|
507
|
-
const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
|
|
508
|
-
assignProp(this, "shape", _shape); // self-caching
|
|
509
|
-
return _shape;
|
|
510
|
-
},
|
|
560
|
+
shape: newShape,
|
|
511
561
|
get catchall() {
|
|
512
562
|
return b._zod.def.catchall;
|
|
513
563
|
},
|
|
@@ -522,78 +572,19 @@ function partial(Class, schema, mask, name = "partial") {
|
|
|
522
572
|
if (hasChecks) {
|
|
523
573
|
throw new Error(`.${name}() cannot be used on object schemas containing refinements`);
|
|
524
574
|
}
|
|
525
|
-
const
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
for (const key of Reflect.ownKeys(mask)) {
|
|
531
|
-
if (!Object.prototype.hasOwnProperty.call(oldShape, key)) {
|
|
532
|
-
throw new Error(`Unrecognized key: "${String(key)}"`);
|
|
533
|
-
}
|
|
534
|
-
if (!mask[key])
|
|
535
|
-
continue;
|
|
536
|
-
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
537
|
-
shape[key] = Class
|
|
538
|
-
? new Class({
|
|
539
|
-
type: "optional",
|
|
540
|
-
innerType: oldShape[key],
|
|
541
|
-
})
|
|
542
|
-
: oldShape[key];
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
else {
|
|
546
|
-
// the spread copies symbol keys; `for...in` would not reach them
|
|
547
|
-
for (const key of Reflect.ownKeys(oldShape)) {
|
|
548
|
-
// if (oldShape[key]!._zod.optin === "optional") continue;
|
|
549
|
-
shape[key] = Class
|
|
550
|
-
? new Class({
|
|
551
|
-
type: "optional",
|
|
552
|
-
innerType: oldShape[key],
|
|
553
|
-
})
|
|
554
|
-
: oldShape[key];
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
assignProp(this, "shape", shape); // self-caching
|
|
558
|
-
return shape;
|
|
559
|
-
},
|
|
560
|
-
checks: [],
|
|
561
|
-
});
|
|
562
|
-
return clone(schema, def);
|
|
575
|
+
const selected = mask ? new Set(maskedKeys(schema, mask)) : undefined;
|
|
576
|
+
const newShape = {};
|
|
577
|
+
mirrorShape(newShape, schema, Reflect.ownKeys(sourceShape(schema)), Class &&
|
|
578
|
+
((value, key) => (selected && !selected.has(key) ? value : new Class({ type: "optional", innerType: value }))));
|
|
579
|
+
return clone(schema, mergeDefs(schema._zod.def, { shape: newShape, checks: [] }));
|
|
563
580
|
}
|
|
564
581
|
function required(Class, schema, mask) {
|
|
565
|
-
const
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
if (!Object.prototype.hasOwnProperty.call(shape, key)) {
|
|
572
|
-
throw new Error(`Unrecognized key: "${String(key)}"`);
|
|
573
|
-
}
|
|
574
|
-
if (!mask[key])
|
|
575
|
-
continue;
|
|
576
|
-
// overwrite with non-optional
|
|
577
|
-
shape[key] = new Class({
|
|
578
|
-
type: "nonoptional",
|
|
579
|
-
innerType: oldShape[key],
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
else {
|
|
584
|
-
for (const key of Reflect.ownKeys(oldShape)) {
|
|
585
|
-
// overwrite with non-optional
|
|
586
|
-
shape[key] = new Class({
|
|
587
|
-
type: "nonoptional",
|
|
588
|
-
innerType: oldShape[key],
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
assignProp(this, "shape", shape); // self-caching
|
|
593
|
-
return shape;
|
|
594
|
-
},
|
|
595
|
-
});
|
|
596
|
-
return clone(schema, def);
|
|
582
|
+
const selected = mask ? new Set(maskedKeys(schema, mask)) : undefined;
|
|
583
|
+
const newShape = {};
|
|
584
|
+
mirrorShape(newShape, schema, Reflect.ownKeys(sourceShape(schema)), (value, key) =>
|
|
585
|
+
// overwrite with non-optional
|
|
586
|
+
selected && !selected.has(key) ? value : new Class({ type: "nonoptional", innerType: value }));
|
|
587
|
+
return clone(schema, mergeDefs(schema._zod.def, { shape: newShape }));
|
|
597
588
|
}
|
|
598
589
|
// invalid_type | too_big | too_small | invalid_format | not_multiple_of | unrecognized_keys | invalid_union | invalid_key | invalid_element | invalid_value | custom
|
|
599
590
|
function aborted(x, startIndex = 0) {
|
|
@@ -655,13 +646,19 @@ function finalizeIssue(iss, ctx, config) {
|
|
|
655
646
|
unwrapMessage(config.customError?.(iss)) ??
|
|
656
647
|
unwrapMessage(config.localeError?.(iss)) ??
|
|
657
648
|
"Invalid input");
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
649
|
+
// an explicit own-key copy beats object rest with excluded keys, which v8 routes through a generic runtime call; Object.keys rather than for-in so an issue pushed with a prototype does not leak inherited keys, and an own __proto__ key is dropped rather than assigned through the setter
|
|
650
|
+
const full = {};
|
|
651
|
+
for (const k of Object.keys(iss)) {
|
|
652
|
+
if (k === "inst" || k === "schema" || k === "continue" || k === "input" || k === "__proto__")
|
|
653
|
+
continue;
|
|
654
|
+
full[k] = iss[k];
|
|
655
|
+
}
|
|
656
|
+
full.path ?? (full.path = []);
|
|
657
|
+
full.message = message;
|
|
661
658
|
if (ctx?.reportInput) {
|
|
662
|
-
|
|
659
|
+
full.input = iss.input;
|
|
663
660
|
}
|
|
664
|
-
return
|
|
661
|
+
return full;
|
|
665
662
|
}
|
|
666
663
|
function getSizableOrigin(input) {
|
|
667
664
|
if (input instanceof Set)
|
|
@@ -799,6 +796,10 @@ function members(proto, table) {
|
|
|
799
796
|
else
|
|
800
797
|
defineBound(proto, key, desc.value);
|
|
801
798
|
}
|
|
799
|
+
// for..in sees no symbol keys, so well-known members like Symbol.iterator install here
|
|
800
|
+
for (const sym of Object.getOwnPropertySymbols(table)) {
|
|
801
|
+
defineBound(proto, sym, table[sym]);
|
|
802
|
+
}
|
|
802
803
|
}
|
|
803
804
|
/** Shadows a prototype member with an own value, so a getter that builds from the instance runs once. */
|
|
804
805
|
function own(inst, key, value, enumerable = true) {
|
|
@@ -809,6 +810,24 @@ function own(inst, key, value, enumerable = true) {
|
|
|
809
810
|
function hide(inst, key, value) {
|
|
810
811
|
return own(inst, key, value, false);
|
|
811
812
|
}
|
|
813
|
+
/** Adds members a table derives from the instance: each builds on first read and shadows as own data, and assignment shadows the same way, as when these were own properties. */
|
|
814
|
+
function derived(computes, table) {
|
|
815
|
+
for (const key in computes) {
|
|
816
|
+
const compute = computes[key];
|
|
817
|
+
// an object literal's accessor is configurable and enumerable, and `members` copies the descriptor as written
|
|
818
|
+
Object.defineProperty(table, key, {
|
|
819
|
+
configurable: true,
|
|
820
|
+
enumerable: true,
|
|
821
|
+
get() {
|
|
822
|
+
return own(this, key, compute(this));
|
|
823
|
+
},
|
|
824
|
+
set(value) {
|
|
825
|
+
own(this, key, value);
|
|
826
|
+
},
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
return table;
|
|
830
|
+
}
|
|
812
831
|
function defineBound(proto, key, fn) {
|
|
813
832
|
Object.defineProperty(proto, key, {
|
|
814
833
|
configurable: true,
|
|
@@ -913,3 +932,22 @@ function constantCatch(value) {
|
|
|
913
932
|
fn[exports.CONSTANT_CATCH] = true;
|
|
914
933
|
return fn;
|
|
915
934
|
}
|
|
935
|
+
|
|
936
|
+
// seal-cjs-exports
|
|
937
|
+
(function () {
|
|
938
|
+
var keys = Object.getOwnPropertyNames(exports);
|
|
939
|
+
for (var i = 0; i < keys.length; i++) {
|
|
940
|
+
var desc = Object.getOwnPropertyDescriptor(exports, keys[i]);
|
|
941
|
+
if (!desc || !desc.get || !desc.configurable) continue;
|
|
942
|
+
var value;
|
|
943
|
+
try {
|
|
944
|
+
value = desc.get();
|
|
945
|
+
} catch (e) {
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
// a circular require may not have settled this one yet, so leave it live
|
|
949
|
+
if (value === undefined) continue;
|
|
950
|
+
Object.defineProperty(exports, keys[i], { value: value, writable: false, enumerable: desc.enumerable, configurable: false });
|
|
951
|
+
}
|
|
952
|
+
Object.freeze(exports);
|
|
953
|
+
})();
|