| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- // import { $ZodType } from "./schemas.js";
- import * as core from "./core.js";
- import * as regexes from "./regexes.js";
- import * as util from "./util.js";
- export const $ZodCheck = /*@__PURE__*/ core.$constructor("$ZodCheck", (inst, def) => {
- var _a;
- inst._zod ?? (inst._zod = {});
- inst._zod.def = def;
- (_a = inst._zod).onattach ?? (_a.onattach = []);
- });
- const numericOriginMap = {
- number: "number",
- bigint: "bigint",
- object: "date",
- };
- export const $ZodCheckLessThan = /*@__PURE__*/ core.$constructor("$ZodCheckLessThan", (inst, def) => {
- $ZodCheck.init(inst, def);
- const origin = numericOriginMap[typeof def.value];
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- const curr = (def.inclusive ? bag.maximum : bag.exclusiveMaximum) ?? Number.POSITIVE_INFINITY;
- if (def.value < curr) {
- if (def.inclusive)
- bag.maximum = def.value;
- else
- bag.exclusiveMaximum = def.value;
- }
- });
- inst._zod.check = (payload) => {
- if (def.inclusive ? payload.value <= def.value : payload.value < def.value) {
- return;
- }
- payload.issues.push({
- origin,
- code: "too_big",
- maximum: typeof def.value === "object" ? def.value.getTime() : def.value,
- input: payload.value,
- inclusive: def.inclusive,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckGreaterThan = /*@__PURE__*/ core.$constructor("$ZodCheckGreaterThan", (inst, def) => {
- $ZodCheck.init(inst, def);
- const origin = numericOriginMap[typeof def.value];
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- const curr = (def.inclusive ? bag.minimum : bag.exclusiveMinimum) ?? Number.NEGATIVE_INFINITY;
- if (def.value > curr) {
- if (def.inclusive)
- bag.minimum = def.value;
- else
- bag.exclusiveMinimum = def.value;
- }
- });
- inst._zod.check = (payload) => {
- if (def.inclusive ? payload.value >= def.value : payload.value > def.value) {
- return;
- }
- payload.issues.push({
- origin,
- code: "too_small",
- minimum: typeof def.value === "object" ? def.value.getTime() : def.value,
- input: payload.value,
- inclusive: def.inclusive,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckMultipleOf =
- /*@__PURE__*/ core.$constructor("$ZodCheckMultipleOf", (inst, def) => {
- $ZodCheck.init(inst, def);
- inst._zod.onattach.push((inst) => {
- var _a;
- (_a = inst._zod.bag).multipleOf ?? (_a.multipleOf = def.value);
- });
- inst._zod.check = (payload) => {
- if (typeof payload.value !== typeof def.value)
- throw new Error("Cannot mix number and bigint in multiple_of check.");
- const isMultiple = typeof payload.value === "bigint"
- ? payload.value % def.value === BigInt(0)
- : util.floatSafeRemainder(payload.value, def.value) === 0;
- if (isMultiple)
- return;
- payload.issues.push({
- origin: typeof payload.value,
- code: "not_multiple_of",
- divisor: def.value,
- input: payload.value,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckNumberFormat = /*@__PURE__*/ core.$constructor("$ZodCheckNumberFormat", (inst, def) => {
- $ZodCheck.init(inst, def); // no format checks
- def.format = def.format || "float64";
- const isInt = def.format?.includes("int");
- const origin = isInt ? "int" : "number";
- const [minimum, maximum] = util.NUMBER_FORMAT_RANGES[def.format];
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.format = def.format;
- bag.minimum = minimum;
- bag.maximum = maximum;
- if (isInt)
- bag.pattern = regexes.integer;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- if (isInt) {
- if (!Number.isInteger(input)) {
- // invalid_format issue
- // payload.issues.push({
- // expected: def.format,
- // format: def.format,
- // code: "invalid_format",
- // input,
- // inst,
- // });
- // invalid_type issue
- payload.issues.push({
- expected: origin,
- format: def.format,
- code: "invalid_type",
- continue: false,
- input,
- inst,
- });
- return;
- // not_multiple_of issue
- // payload.issues.push({
- // code: "not_multiple_of",
- // origin: "number",
- // input,
- // inst,
- // divisor: 1,
- // });
- }
- if (!Number.isSafeInteger(input)) {
- if (input > 0) {
- // too_big
- payload.issues.push({
- input,
- code: "too_big",
- maximum: Number.MAX_SAFE_INTEGER,
- note: "Integers must be within the safe integer range.",
- inst,
- origin,
- inclusive: true,
- continue: !def.abort,
- });
- }
- else {
- // too_small
- payload.issues.push({
- input,
- code: "too_small",
- minimum: Number.MIN_SAFE_INTEGER,
- note: "Integers must be within the safe integer range.",
- inst,
- origin,
- inclusive: true,
- continue: !def.abort,
- });
- }
- return;
- }
- }
- if (input < minimum) {
- payload.issues.push({
- origin: "number",
- input,
- code: "too_small",
- minimum,
- inclusive: true,
- inst,
- continue: !def.abort,
- });
- }
- if (input > maximum) {
- payload.issues.push({
- origin: "number",
- input,
- code: "too_big",
- maximum,
- inclusive: true,
- inst,
- continue: !def.abort,
- });
- }
- };
- });
- export const $ZodCheckBigIntFormat = /*@__PURE__*/ core.$constructor("$ZodCheckBigIntFormat", (inst, def) => {
- $ZodCheck.init(inst, def); // no format checks
- const [minimum, maximum] = util.BIGINT_FORMAT_RANGES[def.format];
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.format = def.format;
- bag.minimum = minimum;
- bag.maximum = maximum;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- if (input < minimum) {
- payload.issues.push({
- origin: "bigint",
- input,
- code: "too_small",
- minimum: minimum,
- inclusive: true,
- inst,
- continue: !def.abort,
- });
- }
- if (input > maximum) {
- payload.issues.push({
- origin: "bigint",
- input,
- code: "too_big",
- maximum,
- inclusive: true,
- inst,
- continue: !def.abort,
- });
- }
- };
- });
- export const $ZodCheckMaxSize = /*@__PURE__*/ core.$constructor("$ZodCheckMaxSize", (inst, def) => {
- var _a;
- $ZodCheck.init(inst, def);
- (_a = inst._zod.def).when ?? (_a.when = (payload) => {
- const val = payload.value;
- return !util.nullish(val) && val.size !== undefined;
- });
- inst._zod.onattach.push((inst) => {
- const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
- if (def.maximum < curr)
- inst._zod.bag.maximum = def.maximum;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- const size = input.size;
- if (size <= def.maximum)
- return;
- payload.issues.push({
- origin: util.getSizableOrigin(input),
- code: "too_big",
- maximum: def.maximum,
- inclusive: true,
- input,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckMinSize = /*@__PURE__*/ core.$constructor("$ZodCheckMinSize", (inst, def) => {
- var _a;
- $ZodCheck.init(inst, def);
- (_a = inst._zod.def).when ?? (_a.when = (payload) => {
- const val = payload.value;
- return !util.nullish(val) && val.size !== undefined;
- });
- inst._zod.onattach.push((inst) => {
- const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
- if (def.minimum > curr)
- inst._zod.bag.minimum = def.minimum;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- const size = input.size;
- if (size >= def.minimum)
- return;
- payload.issues.push({
- origin: util.getSizableOrigin(input),
- code: "too_small",
- minimum: def.minimum,
- inclusive: true,
- input,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckSizeEquals = /*@__PURE__*/ core.$constructor("$ZodCheckSizeEquals", (inst, def) => {
- var _a;
- $ZodCheck.init(inst, def);
- (_a = inst._zod.def).when ?? (_a.when = (payload) => {
- const val = payload.value;
- return !util.nullish(val) && val.size !== undefined;
- });
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.minimum = def.size;
- bag.maximum = def.size;
- bag.size = def.size;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- const size = input.size;
- if (size === def.size)
- return;
- const tooBig = size > def.size;
- payload.issues.push({
- origin: util.getSizableOrigin(input),
- ...(tooBig ? { code: "too_big", maximum: def.size } : { code: "too_small", minimum: def.size }),
- inclusive: true,
- exact: true,
- input: payload.value,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckMaxLength = /*@__PURE__*/ core.$constructor("$ZodCheckMaxLength", (inst, def) => {
- var _a;
- $ZodCheck.init(inst, def);
- (_a = inst._zod.def).when ?? (_a.when = (payload) => {
- const val = payload.value;
- return !util.nullish(val) && val.length !== undefined;
- });
- inst._zod.onattach.push((inst) => {
- const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
- if (def.maximum < curr)
- inst._zod.bag.maximum = def.maximum;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- const length = input.length;
- if (length <= def.maximum)
- return;
- const origin = util.getLengthableOrigin(input);
- payload.issues.push({
- origin,
- code: "too_big",
- maximum: def.maximum,
- inclusive: true,
- input,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckMinLength = /*@__PURE__*/ core.$constructor("$ZodCheckMinLength", (inst, def) => {
- var _a;
- $ZodCheck.init(inst, def);
- (_a = inst._zod.def).when ?? (_a.when = (payload) => {
- const val = payload.value;
- return !util.nullish(val) && val.length !== undefined;
- });
- inst._zod.onattach.push((inst) => {
- const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
- if (def.minimum > curr)
- inst._zod.bag.minimum = def.minimum;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- const length = input.length;
- if (length >= def.minimum)
- return;
- const origin = util.getLengthableOrigin(input);
- payload.issues.push({
- origin,
- code: "too_small",
- minimum: def.minimum,
- inclusive: true,
- input,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckLengthEquals = /*@__PURE__*/ core.$constructor("$ZodCheckLengthEquals", (inst, def) => {
- var _a;
- $ZodCheck.init(inst, def);
- (_a = inst._zod.def).when ?? (_a.when = (payload) => {
- const val = payload.value;
- return !util.nullish(val) && val.length !== undefined;
- });
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.minimum = def.length;
- bag.maximum = def.length;
- bag.length = def.length;
- });
- inst._zod.check = (payload) => {
- const input = payload.value;
- const length = input.length;
- if (length === def.length)
- return;
- const origin = util.getLengthableOrigin(input);
- const tooBig = length > def.length;
- payload.issues.push({
- origin,
- ...(tooBig ? { code: "too_big", maximum: def.length } : { code: "too_small", minimum: def.length }),
- inclusive: true,
- exact: true,
- input: payload.value,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckStringFormat = /*@__PURE__*/ core.$constructor("$ZodCheckStringFormat", (inst, def) => {
- var _a, _b;
- $ZodCheck.init(inst, def);
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.format = def.format;
- if (def.pattern) {
- bag.patterns ?? (bag.patterns = new Set());
- bag.patterns.add(def.pattern);
- }
- });
- if (def.pattern)
- (_a = inst._zod).check ?? (_a.check = (payload) => {
- def.pattern.lastIndex = 0;
- if (def.pattern.test(payload.value))
- return;
- payload.issues.push({
- origin: "string",
- code: "invalid_format",
- format: def.format,
- input: payload.value,
- ...(def.pattern ? { pattern: def.pattern.toString() } : {}),
- inst,
- continue: !def.abort,
- });
- });
- else
- (_b = inst._zod).check ?? (_b.check = () => { });
- });
- export const $ZodCheckRegex = /*@__PURE__*/ core.$constructor("$ZodCheckRegex", (inst, def) => {
- $ZodCheckStringFormat.init(inst, def);
- inst._zod.check = (payload) => {
- def.pattern.lastIndex = 0;
- if (def.pattern.test(payload.value))
- return;
- payload.issues.push({
- origin: "string",
- code: "invalid_format",
- format: "regex",
- input: payload.value,
- pattern: def.pattern.toString(),
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckLowerCase = /*@__PURE__*/ core.$constructor("$ZodCheckLowerCase", (inst, def) => {
- def.pattern ?? (def.pattern = regexes.lowercase);
- $ZodCheckStringFormat.init(inst, def);
- });
- export const $ZodCheckUpperCase = /*@__PURE__*/ core.$constructor("$ZodCheckUpperCase", (inst, def) => {
- def.pattern ?? (def.pattern = regexes.uppercase);
- $ZodCheckStringFormat.init(inst, def);
- });
- export const $ZodCheckIncludes = /*@__PURE__*/ core.$constructor("$ZodCheckIncludes", (inst, def) => {
- $ZodCheck.init(inst, def);
- const escapedRegex = util.escapeRegex(def.includes);
- const pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position}}${escapedRegex}` : escapedRegex);
- def.pattern = pattern;
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.patterns ?? (bag.patterns = new Set());
- bag.patterns.add(pattern);
- });
- inst._zod.check = (payload) => {
- if (payload.value.includes(def.includes, def.position))
- return;
- payload.issues.push({
- origin: "string",
- code: "invalid_format",
- format: "includes",
- includes: def.includes,
- input: payload.value,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckStartsWith = /*@__PURE__*/ core.$constructor("$ZodCheckStartsWith", (inst, def) => {
- $ZodCheck.init(inst, def);
- const pattern = new RegExp(`^${util.escapeRegex(def.prefix)}.*`);
- def.pattern ?? (def.pattern = pattern);
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.patterns ?? (bag.patterns = new Set());
- bag.patterns.add(pattern);
- });
- inst._zod.check = (payload) => {
- if (payload.value.startsWith(def.prefix))
- return;
- payload.issues.push({
- origin: "string",
- code: "invalid_format",
- format: "starts_with",
- prefix: def.prefix,
- input: payload.value,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckEndsWith = /*@__PURE__*/ core.$constructor("$ZodCheckEndsWith", (inst, def) => {
- $ZodCheck.init(inst, def);
- const pattern = new RegExp(`.*${util.escapeRegex(def.suffix)}$`);
- def.pattern ?? (def.pattern = pattern);
- inst._zod.onattach.push((inst) => {
- const bag = inst._zod.bag;
- bag.patterns ?? (bag.patterns = new Set());
- bag.patterns.add(pattern);
- });
- inst._zod.check = (payload) => {
- if (payload.value.endsWith(def.suffix))
- return;
- payload.issues.push({
- origin: "string",
- code: "invalid_format",
- format: "ends_with",
- suffix: def.suffix,
- input: payload.value,
- inst,
- continue: !def.abort,
- });
- };
- });
- ///////////////////////////////////
- ///// $ZodCheckProperty /////
- ///////////////////////////////////
- function handleCheckPropertyResult(result, payload, property) {
- if (result.issues.length) {
- payload.issues.push(...util.prefixIssues(property, result.issues));
- }
- }
- export const $ZodCheckProperty = /*@__PURE__*/ core.$constructor("$ZodCheckProperty", (inst, def) => {
- $ZodCheck.init(inst, def);
- inst._zod.check = (payload) => {
- const result = def.schema._zod.run({
- value: payload.value[def.property],
- issues: [],
- }, {});
- if (result instanceof Promise) {
- return result.then((result) => handleCheckPropertyResult(result, payload, def.property));
- }
- handleCheckPropertyResult(result, payload, def.property);
- return;
- };
- });
- export const $ZodCheckMimeType = /*@__PURE__*/ core.$constructor("$ZodCheckMimeType", (inst, def) => {
- $ZodCheck.init(inst, def);
- const mimeSet = new Set(def.mime);
- inst._zod.onattach.push((inst) => {
- inst._zod.bag.mime = def.mime;
- });
- inst._zod.check = (payload) => {
- if (mimeSet.has(payload.value.type))
- return;
- payload.issues.push({
- code: "invalid_value",
- values: def.mime,
- input: payload.value.type,
- inst,
- continue: !def.abort,
- });
- };
- });
- export const $ZodCheckOverwrite = /*@__PURE__*/ core.$constructor("$ZodCheckOverwrite", (inst, def) => {
- $ZodCheck.init(inst, def);
- inst._zod.check = (payload) => {
- payload.value = def.tx(payload.value);
- };
- });
|