UNPKG

@charliethomson/better-session

Version:

### Adds specific and useful typing, and security through obscurity to the browser storage

1,673 lines 276 kB
var Ta = Object.defineProperty; var Ba = (n, e, t) => e in n ? Ta(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; var ar = (n, e, t) => (Ba(n, typeof e != "symbol" ? e + "" : e, t), t); var fe; (function(n) { n.assertEqual = (s) => s; function e(s) { } n.assertIs = e; function t(s) { throw new Error(); } n.assertNever = t, n.arrayToEnum = (s) => { const i = {}; for (const l of s) i[l] = l; return i; }, n.getValidEnumValues = (s) => { const i = n.objectKeys(s).filter((c) => typeof s[s[c]] != "number"), l = {}; for (const c of i) l[c] = s[c]; return n.objectValues(l); }, n.objectValues = (s) => n.objectKeys(s).map(function(i) { return s[i]; }), n.objectKeys = typeof Object.keys == "function" ? (s) => Object.keys(s) : (s) => { const i = []; for (const l in s) Object.prototype.hasOwnProperty.call(s, l) && i.push(l); return i; }, n.find = (s, i) => { for (const l of s) if (i(l)) return l; }, n.isInteger = typeof Number.isInteger == "function" ? (s) => Number.isInteger(s) : (s) => typeof s == "number" && isFinite(s) && Math.floor(s) === s; function r(s, i = " | ") { return s.map((l) => typeof l == "string" ? `'${l}'` : l).join(i); } n.joinValues = r, n.jsonStringifyReplacer = (s, i) => typeof i == "bigint" ? i.toString() : i; })(fe || (fe = {})); var h0; (function(n) { n.mergeShapes = (e, t) => ({ ...e, ...t // second overwrites first }); })(h0 || (h0 = {})); const M = fe.arrayToEnum([ "string", "nan", "number", "integer", "float", "boolean", "date", "bigint", "symbol", "function", "undefined", "null", "array", "object", "unknown", "promise", "void", "never", "map", "set" ]), Ge = (n) => { switch (typeof n) { case "undefined": return M.undefined; case "string": return M.string; case "number": return isNaN(n) ? M.nan : M.number; case "boolean": return M.boolean; case "function": return M.function; case "bigint": return M.bigint; case "symbol": return M.symbol; case "object": return Array.isArray(n) ? M.array : n === null ? M.null : n.then && typeof n.then == "function" && n.catch && typeof n.catch == "function" ? M.promise : typeof Map < "u" && n instanceof Map ? M.map : typeof Set < "u" && n instanceof Set ? M.set : typeof Date < "u" && n instanceof Date ? M.date : M.object; default: return M.unknown; } }, Z = fe.arrayToEnum([ "invalid_type", "invalid_literal", "custom", "invalid_union", "invalid_union_discriminator", "invalid_enum_value", "unrecognized_keys", "invalid_arguments", "invalid_return_type", "invalid_date", "invalid_string", "too_small", "too_big", "invalid_intersection_types", "not_multiple_of", "not_finite" ]), Aa = (n) => JSON.stringify(n, null, 2).replace(/"([^"]+)":/g, "$1:"); class Pe extends Error { constructor(e) { super(), this.issues = [], this.addIssue = (r) => { this.issues = [...this.issues, r]; }, this.addIssues = (r = []) => { this.issues = [...this.issues, ...r]; }; const t = new.target.prototype; Object.setPrototypeOf ? Object.setPrototypeOf(this, t) : this.__proto__ = t, this.name = "ZodError", this.issues = e; } get errors() { return this.issues; } format(e) { const t = e || function(i) { return i.message; }, r = { _errors: [] }, s = (i) => { for (const l of i.issues) if (l.code === "invalid_union") l.unionErrors.map(s); else if (l.code === "invalid_return_type") s(l.returnTypeError); else if (l.code === "invalid_arguments") s(l.argumentsError); else if (l.path.length === 0) r._errors.push(t(l)); else { let c = r, x = 0; for (; x < l.path.length; ) { const o = l.path[x]; x === l.path.length - 1 ? (c[o] = c[o] || { _errors: [] }, c[o]._errors.push(t(l))) : c[o] = c[o] || { _errors: [] }, c = c[o], x++; } } }; return s(this), r; } toString() { return this.message; } get message() { return JSON.stringify(this.issues, fe.jsonStringifyReplacer, 2); } get isEmpty() { return this.issues.length === 0; } flatten(e = (t) => t.message) { const t = {}, r = []; for (const s of this.issues) s.path.length > 0 ? (t[s.path[0]] = t[s.path[0]] || [], t[s.path[0]].push(e(s))) : r.push(e(s)); return { formErrors: r, fieldErrors: t }; } get formErrors() { return this.flatten(); } } Pe.create = (n) => new Pe(n); const Tt = (n, e) => { let t; switch (n.code) { case Z.invalid_type: n.received === M.undefined ? t = "Required" : t = `Expected ${n.expected}, received ${n.received}`; break; case Z.invalid_literal: t = `Invalid literal value, expected ${JSON.stringify(n.expected, fe.jsonStringifyReplacer)}`; break; case Z.unrecognized_keys: t = `Unrecognized key(s) in object: ${fe.joinValues(n.keys, ", ")}`; break; case Z.invalid_union: t = "Invalid input"; break; case Z.invalid_union_discriminator: t = `Invalid discriminator value. Expected ${fe.joinValues(n.options)}`; break; case Z.invalid_enum_value: t = `Invalid enum value. Expected ${fe.joinValues(n.options)}, received '${n.received}'`; break; case Z.invalid_arguments: t = "Invalid function arguments"; break; case Z.invalid_return_type: t = "Invalid function return type"; break; case Z.invalid_date: t = "Invalid date"; break; case Z.invalid_string: typeof n.validation == "object" ? "includes" in n.validation ? (t = `Invalid input: must include "${n.validation.includes}"`, typeof n.validation.position == "number" && (t = `${t} at one or more positions greater than or equal to ${n.validation.position}`)) : "startsWith" in n.validation ? t = `Invalid input: must start with "${n.validation.startsWith}"` : "endsWith" in n.validation ? t = `Invalid input: must end with "${n.validation.endsWith}"` : fe.assertNever(n.validation) : n.validation !== "regex" ? t = `Invalid ${n.validation}` : t = "Invalid"; break; case Z.too_small: n.type === "array" ? t = `Array must contain ${n.exact ? "exactly" : n.inclusive ? "at least" : "more than"} ${n.minimum} element(s)` : n.type === "string" ? t = `String must contain ${n.exact ? "exactly" : n.inclusive ? "at least" : "over"} ${n.minimum} character(s)` : n.type === "number" ? t = `Number must be ${n.exact ? "exactly equal to " : n.inclusive ? "greater than or equal to " : "greater than "}${n.minimum}` : n.type === "date" ? t = `Date must be ${n.exact ? "exactly equal to " : n.inclusive ? "greater than or equal to " : "greater than "}${new Date(Number(n.minimum))}` : t = "Invalid input"; break; case Z.too_big: n.type === "array" ? t = `Array must contain ${n.exact ? "exactly" : n.inclusive ? "at most" : "less than"} ${n.maximum} element(s)` : n.type === "string" ? t = `String must contain ${n.exact ? "exactly" : n.inclusive ? "at most" : "under"} ${n.maximum} character(s)` : n.type === "number" ? t = `Number must be ${n.exact ? "exactly" : n.inclusive ? "less than or equal to" : "less than"} ${n.maximum}` : n.type === "bigint" ? t = `BigInt must be ${n.exact ? "exactly" : n.inclusive ? "less than or equal to" : "less than"} ${n.maximum}` : n.type === "date" ? t = `Date must be ${n.exact ? "exactly" : n.inclusive ? "smaller than or equal to" : "smaller than"} ${new Date(Number(n.maximum))}` : t = "Invalid input"; break; case Z.custom: t = "Invalid input"; break; case Z.invalid_intersection_types: t = "Intersection results could not be merged"; break; case Z.not_multiple_of: t = `Number must be a multiple of ${n.multipleOf}`; break; case Z.not_finite: t = "Number must be finite"; break; default: t = e.defaultError, fe.assertNever(n); } return { message: t }; }; let Pn = Tt; function Oa(n) { Pn = n; } function ir() { return Pn; } const or = (n) => { const { data: e, path: t, errorMaps: r, issueData: s } = n, i = [...t, ...s.path || []], l = { ...s, path: i }; let c = ""; const x = r.filter((o) => !!o).slice().reverse(); for (const o of x) c = o(l, { data: e, defaultError: c }).message; return { ...s, path: i, message: s.message || c }; }, Pa = []; function q(n, e) { const t = or({ issueData: e, data: n.data, path: n.path, errorMaps: [ n.common.contextualErrorMap, n.schemaErrorMap, ir(), Tt // then global default map ].filter((r) => !!r) }); n.common.issues.push(t); } class Ce { constructor() { this.value = "valid"; } dirty() { this.value === "valid" && (this.value = "dirty"); } abort() { this.value !== "aborted" && (this.value = "aborted"); } static mergeArray(e, t) { const r = []; for (const s of t) { if (s.status === "aborted") return te; s.status === "dirty" && e.dirty(), r.push(s.value); } return { status: e.value, value: r }; } static async mergeObjectAsync(e, t) { const r = []; for (const s of t) r.push({ key: await s.key, value: await s.value }); return Ce.mergeObjectSync(e, r); } static mergeObjectSync(e, t) { const r = {}; for (const s of t) { const { key: i, value: l } = s; if (i.status === "aborted" || l.status === "aborted") return te; i.status === "dirty" && e.dirty(), l.status === "dirty" && e.dirty(), (typeof l.value < "u" || s.alwaysSet) && (r[i.value] = l.value); } return { status: e.value, value: r }; } } const te = Object.freeze({ status: "aborted" }), In = (n) => ({ status: "dirty", value: n }), Te = (n) => ({ status: "valid", value: n }), v0 = (n) => n.status === "aborted", p0 = (n) => n.status === "dirty", cr = (n) => n.status === "valid", ur = (n) => typeof Promise < "u" && n instanceof Promise; var Y; (function(n) { n.errToObj = (e) => typeof e == "string" ? { message: e } : e || {}, n.toString = (e) => typeof e == "string" ? e : e == null ? void 0 : e.message; })(Y || (Y = {})); class He { constructor(e, t, r, s) { this._cachedPath = [], this.parent = e, this.data = t, this._path = r, this._key = s; } get path() { return this._cachedPath.length || (this._key instanceof Array ? this._cachedPath.push(...this._path, ...this._key) : this._cachedPath.push(...this._path, this._key)), this._cachedPath; } } const V0 = (n, e) => { if (cr(e)) return { success: !0, data: e.value }; if (!n.common.issues.length) throw new Error("Validation failed but no issues detected."); return { success: !1, get error() { if (this._error) return this._error; const t = new Pe(n.common.issues); return this._error = t, this._error; } }; }; function re(n) { if (!n) return {}; const { errorMap: e, invalid_type_error: t, required_error: r, description: s } = n; if (e && (t || r)) throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`); return e ? { errorMap: e, description: s } : { errorMap: (l, c) => l.code !== "invalid_type" ? { message: c.defaultError } : typeof c.data > "u" ? { message: r ?? c.defaultError } : { message: t ?? c.defaultError }, description: s }; } class ae { constructor(e) { this.spa = this.safeParseAsync, this._def = e, this.parse = this.parse.bind(this), this.safeParse = this.safeParse.bind(this), this.parseAsync = this.parseAsync.bind(this), this.safeParseAsync = this.safeParseAsync.bind(this), this.spa = this.spa.bind(this), this.refine = this.refine.bind(this), this.refinement = this.refinement.bind(this), this.superRefine = this.superRefine.bind(this), this.optional = this.optional.bind(this), this.nullable = this.nullable.bind(this), this.nullish = this.nullish.bind(this), this.array = this.array.bind(this), this.promise = this.promise.bind(this), this.or = this.or.bind(this), this.and = this.and.bind(this), this.transform = this.transform.bind(this), this.brand = this.brand.bind(this), this.default = this.default.bind(this), this.catch = this.catch.bind(this), this.describe = this.describe.bind(this), this.pipe = this.pipe.bind(this), this.isNullable = this.isNullable.bind(this), this.isOptional = this.isOptional.bind(this); } get description() { return this._def.description; } _getType(e) { return Ge(e.data); } _getOrReturnCtx(e, t) { return t || { common: e.parent.common, data: e.data, parsedType: Ge(e.data), schemaErrorMap: this._def.errorMap, path: e.path, parent: e.parent }; } _processInputParams(e) { return { status: new Ce(), ctx: { common: e.parent.common, data: e.data, parsedType: Ge(e.data), schemaErrorMap: this._def.errorMap, path: e.path, parent: e.parent } }; } _parseSync(e) { const t = this._parse(e); if (ur(t)) throw new Error("Synchronous parse encountered promise."); return t; } _parseAsync(e) { const t = this._parse(e); return Promise.resolve(t); } parse(e, t) { const r = this.safeParse(e, t); if (r.success) return r.data; throw r.error; } safeParse(e, t) { var r; const s = { common: { issues: [], async: (r = t == null ? void 0 : t.async) !== null && r !== void 0 ? r : !1, contextualErrorMap: t == null ? void 0 : t.errorMap }, path: (t == null ? void 0 : t.path) || [], schemaErrorMap: this._def.errorMap, parent: null, data: e, parsedType: Ge(e) }, i = this._parseSync({ data: e, path: s.path, parent: s }); return V0(s, i); } async parseAsync(e, t) { const r = await this.safeParseAsync(e, t); if (r.success) return r.data; throw r.error; } async safeParseAsync(e, t) { const r = { common: { issues: [], contextualErrorMap: t == null ? void 0 : t.errorMap, async: !0 }, path: (t == null ? void 0 : t.path) || [], schemaErrorMap: this._def.errorMap, parent: null, data: e, parsedType: Ge(e) }, s = this._parse({ data: e, path: r.path, parent: r }), i = await (ur(s) ? s : Promise.resolve(s)); return V0(r, i); } refine(e, t) { const r = (s) => typeof t == "string" || typeof t > "u" ? { message: t } : typeof t == "function" ? t(s) : t; return this._refinement((s, i) => { const l = e(s), c = () => i.addIssue({ code: Z.custom, ...r(s) }); return typeof Promise < "u" && l instanceof Promise ? l.then((x) => x ? !0 : (c(), !1)) : l ? !0 : (c(), !1); }); } refinement(e, t) { return this._refinement((r, s) => e(r) ? !0 : (s.addIssue(typeof t == "function" ? t(r, s) : t), !1)); } _refinement(e) { return new Ne({ schema: this, typeName: Q.ZodEffects, effect: { type: "refinement", refinement: e } }); } superRefine(e) { return this._refinement(e); } optional() { return We.create(this, this._def); } nullable() { return ct.create(this, this._def); } nullish() { return this.nullable().optional(); } array() { return Ie.create(this, this._def); } promise() { return kt.create(this, this._def); } or(e) { return Pt.create([this, e], this._def); } and(e) { return It.create(this, e, this._def); } transform(e) { return new Ne({ ...re(this._def), schema: this, typeName: Q.ZodEffects, effect: { type: "transform", transform: e } }); } default(e) { const t = typeof e == "function" ? e : () => e; return new Ht({ ...re(this._def), innerType: this, defaultValue: t, typeName: Q.ZodDefault }); } brand() { return new Dn({ typeName: Q.ZodBranded, type: this, ...re(this._def) }); } catch(e) { const t = typeof e == "function" ? e : () => e; return new xr({ ...re(this._def), innerType: this, catchValue: t, typeName: Q.ZodCatch }); } describe(e) { const t = this.constructor; return new t({ ...this._def, description: e }); } pipe(e) { return Zt.create(this, e); } isOptional() { return this.safeParse(void 0).success; } isNullable() { return this.safeParse(null).success; } } const Ia = /^c[^\s-]{8,}$/i, Na = /^[a-z][a-z0-9]*$/, Da = /[0-9A-HJKMNP-TV-Z]{26}/, ja = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i, za = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/, Ha = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u, Za = /^(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))$/, La = /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/, $a = (n) => n.precision ? n.offset ? new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${n.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`) : new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${n.precision}}Z$`) : n.precision === 0 ? n.offset ? new RegExp("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$") : new RegExp("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$") : n.offset ? new RegExp("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$") : new RegExp("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$"); function Ma(n, e) { return !!((e === "v4" || !e) && Za.test(n) || (e === "v6" || !e) && La.test(n)); } class Oe extends ae { constructor() { super(...arguments), this._regex = (e, t, r) => this.refinement((s) => e.test(s), { validation: t, code: Z.invalid_string, ...Y.errToObj(r) }), this.nonempty = (e) => this.min(1, Y.errToObj(e)), this.trim = () => new Oe({ ...this._def, checks: [...this._def.checks, { kind: "trim" }] }), this.toLowerCase = () => new Oe({ ...this._def, checks: [...this._def.checks, { kind: "toLowerCase" }] }), this.toUpperCase = () => new Oe({ ...this._def, checks: [...this._def.checks, { kind: "toUpperCase" }] }); } _parse(e) { if (this._def.coerce && (e.data = String(e.data)), this._getType(e) !== M.string) { const i = this._getOrReturnCtx(e); return q( i, { code: Z.invalid_type, expected: M.string, received: i.parsedType } // ), te; } const r = new Ce(); let s; for (const i of this._def.checks) if (i.kind === "min") e.data.length < i.value && (s = this._getOrReturnCtx(e, s), q(s, { code: Z.too_small, minimum: i.value, type: "string", inclusive: !0, exact: !1, message: i.message }), r.dirty()); else if (i.kind === "max") e.data.length > i.value && (s = this._getOrReturnCtx(e, s), q(s, { code: Z.too_big, maximum: i.value, type: "string", inclusive: !0, exact: !1, message: i.message }), r.dirty()); else if (i.kind === "length") { const l = e.data.length > i.value, c = e.data.length < i.value; (l || c) && (s = this._getOrReturnCtx(e, s), l ? q(s, { code: Z.too_big, maximum: i.value, type: "string", inclusive: !0, exact: !0, message: i.message }) : c && q(s, { code: Z.too_small, minimum: i.value, type: "string", inclusive: !0, exact: !0, message: i.message }), r.dirty()); } else if (i.kind === "email") za.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "email", code: Z.invalid_string, message: i.message }), r.dirty()); else if (i.kind === "emoji") Ha.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "emoji", code: Z.invalid_string, message: i.message }), r.dirty()); else if (i.kind === "uuid") ja.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "uuid", code: Z.invalid_string, message: i.message }), r.dirty()); else if (i.kind === "cuid") Ia.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "cuid", code: Z.invalid_string, message: i.message }), r.dirty()); else if (i.kind === "cuid2") Na.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "cuid2", code: Z.invalid_string, message: i.message }), r.dirty()); else if (i.kind === "ulid") Da.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "ulid", code: Z.invalid_string, message: i.message }), r.dirty()); else if (i.kind === "url") try { new URL(e.data); } catch { s = this._getOrReturnCtx(e, s), q(s, { validation: "url", code: Z.invalid_string, message: i.message }), r.dirty(); } else i.kind === "regex" ? (i.regex.lastIndex = 0, i.regex.test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "regex", code: Z.invalid_string, message: i.message }), r.dirty())) : i.kind === "trim" ? e.data = e.data.trim() : i.kind === "includes" ? e.data.includes(i.value, i.position) || (s = this._getOrReturnCtx(e, s), q(s, { code: Z.invalid_string, validation: { includes: i.value, position: i.position }, message: i.message }), r.dirty()) : i.kind === "toLowerCase" ? e.data = e.data.toLowerCase() : i.kind === "toUpperCase" ? e.data = e.data.toUpperCase() : i.kind === "startsWith" ? e.data.startsWith(i.value) || (s = this._getOrReturnCtx(e, s), q(s, { code: Z.invalid_string, validation: { startsWith: i.value }, message: i.message }), r.dirty()) : i.kind === "endsWith" ? e.data.endsWith(i.value) || (s = this._getOrReturnCtx(e, s), q(s, { code: Z.invalid_string, validation: { endsWith: i.value }, message: i.message }), r.dirty()) : i.kind === "datetime" ? $a(i).test(e.data) || (s = this._getOrReturnCtx(e, s), q(s, { code: Z.invalid_string, validation: "datetime", message: i.message }), r.dirty()) : i.kind === "ip" ? Ma(e.data, i.version) || (s = this._getOrReturnCtx(e, s), q(s, { validation: "ip", code: Z.invalid_string, message: i.message }), r.dirty()) : fe.assertNever(i); return { status: r.value, value: e.data }; } _addCheck(e) { return new Oe({ ...this._def, checks: [...this._def.checks, e] }); } email(e) { return this._addCheck({ kind: "email", ...Y.errToObj(e) }); } url(e) { return this._addCheck({ kind: "url", ...Y.errToObj(e) }); } emoji(e) { return this._addCheck({ kind: "emoji", ...Y.errToObj(e) }); } uuid(e) { return this._addCheck({ kind: "uuid", ...Y.errToObj(e) }); } cuid(e) { return this._addCheck({ kind: "cuid", ...Y.errToObj(e) }); } cuid2(e) { return this._addCheck({ kind: "cuid2", ...Y.errToObj(e) }); } ulid(e) { return this._addCheck({ kind: "ulid", ...Y.errToObj(e) }); } ip(e) { return this._addCheck({ kind: "ip", ...Y.errToObj(e) }); } datetime(e) { var t; return typeof e == "string" ? this._addCheck({ kind: "datetime", precision: null, offset: !1, message: e }) : this._addCheck({ kind: "datetime", precision: typeof (e == null ? void 0 : e.precision) > "u" ? null : e == null ? void 0 : e.precision, offset: (t = e == null ? void 0 : e.offset) !== null && t !== void 0 ? t : !1, ...Y.errToObj(e == null ? void 0 : e.message) }); } regex(e, t) { return this._addCheck({ kind: "regex", regex: e, ...Y.errToObj(t) }); } includes(e, t) { return this._addCheck({ kind: "includes", value: e, position: t == null ? void 0 : t.position, ...Y.errToObj(t == null ? void 0 : t.message) }); } startsWith(e, t) { return this._addCheck({ kind: "startsWith", value: e, ...Y.errToObj(t) }); } endsWith(e, t) { return this._addCheck({ kind: "endsWith", value: e, ...Y.errToObj(t) }); } min(e, t) { return this._addCheck({ kind: "min", value: e, ...Y.errToObj(t) }); } max(e, t) { return this._addCheck({ kind: "max", value: e, ...Y.errToObj(t) }); } length(e, t) { return this._addCheck({ kind: "length", value: e, ...Y.errToObj(t) }); } get isDatetime() { return !!this._def.checks.find((e) => e.kind === "datetime"); } get isEmail() { return !!this._def.checks.find((e) => e.kind === "email"); } get isURL() { return !!this._def.checks.find((e) => e.kind === "url"); } get isEmoji() { return !!this._def.checks.find((e) => e.kind === "emoji"); } get isUUID() { return !!this._def.checks.find((e) => e.kind === "uuid"); } get isCUID() { return !!this._def.checks.find((e) => e.kind === "cuid"); } get isCUID2() { return !!this._def.checks.find((e) => e.kind === "cuid2"); } get isULID() { return !!this._def.checks.find((e) => e.kind === "ulid"); } get isIP() { return !!this._def.checks.find((e) => e.kind === "ip"); } get minLength() { let e = null; for (const t of this._def.checks) t.kind === "min" && (e === null || t.value > e) && (e = t.value); return e; } get maxLength() { let e = null; for (const t of this._def.checks) t.kind === "max" && (e === null || t.value < e) && (e = t.value); return e; } } Oe.create = (n) => { var e; return new Oe({ checks: [], typeName: Q.ZodString, coerce: (e = n == null ? void 0 : n.coerce) !== null && e !== void 0 ? e : !1, ...re(n) }); }; function qa(n, e) { const t = (n.toString().split(".")[1] || "").length, r = (e.toString().split(".")[1] || "").length, s = t > r ? t : r, i = parseInt(n.toFixed(s).replace(".", "")), l = parseInt(e.toFixed(s).replace(".", "")); return i % l / Math.pow(10, s); } class Xe extends ae { constructor() { super(...arguments), this.min = this.gte, this.max = this.lte, this.step = this.multipleOf; } _parse(e) { if (this._def.coerce && (e.data = Number(e.data)), this._getType(e) !== M.number) { const i = this._getOrReturnCtx(e); return q(i, { code: Z.invalid_type, expected: M.number, received: i.parsedType }), te; } let r; const s = new Ce(); for (const i of this._def.checks) i.kind === "int" ? fe.isInteger(e.data) || (r = this._getOrReturnCtx(e, r), q(r, { code: Z.invalid_type, expected: "integer", received: "float", message: i.message }), s.dirty()) : i.kind === "min" ? (i.inclusive ? e.data < i.value : e.data <= i.value) && (r = this._getOrReturnCtx(e, r), q(r, { code: Z.too_small, minimum: i.value, type: "number", inclusive: i.inclusive, exact: !1, message: i.message }), s.dirty()) : i.kind === "max" ? (i.inclusive ? e.data > i.value : e.data >= i.value) && (r = this._getOrReturnCtx(e, r), q(r, { code: Z.too_big, maximum: i.value, type: "number", inclusive: i.inclusive, exact: !1, message: i.message }), s.dirty()) : i.kind === "multipleOf" ? qa(e.data, i.value) !== 0 && (r = this._getOrReturnCtx(e, r), q(r, { code: Z.not_multiple_of, multipleOf: i.value, message: i.message }), s.dirty()) : i.kind === "finite" ? Number.isFinite(e.data) || (r = this._getOrReturnCtx(e, r), q(r, { code: Z.not_finite, message: i.message }), s.dirty()) : fe.assertNever(i); return { status: s.value, value: e.data }; } gte(e, t) { return this.setLimit("min", e, !0, Y.toString(t)); } gt(e, t) { return this.setLimit("min", e, !1, Y.toString(t)); } lte(e, t) { return this.setLimit("max", e, !0, Y.toString(t)); } lt(e, t) { return this.setLimit("max", e, !1, Y.toString(t)); } setLimit(e, t, r, s) { return new Xe({ ...this._def, checks: [ ...this._def.checks, { kind: e, value: t, inclusive: r, message: Y.toString(s) } ] }); } _addCheck(e) { return new Xe({ ...this._def, checks: [...this._def.checks, e] }); } int(e) { return this._addCheck({ kind: "int", message: Y.toString(e) }); } positive(e) { return this._addCheck({ kind: "min", value: 0, inclusive: !1, message: Y.toString(e) }); } negative(e) { return this._addCheck({ kind: "max", value: 0, inclusive: !1, message: Y.toString(e) }); } nonpositive(e) { return this._addCheck({ kind: "max", value: 0, inclusive: !0, message: Y.toString(e) }); } nonnegative(e) { return this._addCheck({ kind: "min", value: 0, inclusive: !0, message: Y.toString(e) }); } multipleOf(e, t) { return this._addCheck({ kind: "multipleOf", value: e, message: Y.toString(t) }); } finite(e) { return this._addCheck({ kind: "finite", message: Y.toString(e) }); } safe(e) { return this._addCheck({ kind: "min", inclusive: !0, value: Number.MIN_SAFE_INTEGER, message: Y.toString(e) })._addCheck({ kind: "max", inclusive: !0, value: Number.MAX_SAFE_INTEGER, message: Y.toString(e) }); } get minValue() { let e = null; for (const t of this._def.checks) t.kind === "min" && (e === null || t.value > e) && (e = t.value); return e; } get maxValue() { let e = null; for (const t of this._def.checks) t.kind === "max" && (e === null || t.value < e) && (e = t.value); return e; } get isInt() { return !!this._def.checks.find((e) => e.kind === "int" || e.kind === "multipleOf" && fe.isInteger(e.value)); } get isFinite() { let e = null, t = null; for (const r of this._def.checks) { if (r.kind === "finite" || r.kind === "int" || r.kind === "multipleOf") return !0; r.kind === "min" ? (t === null || r.value > t) && (t = r.value) : r.kind === "max" && (e === null || r.value < e) && (e = r.value); } return Number.isFinite(t) && Number.isFinite(e); } } Xe.create = (n) => new Xe({ checks: [], typeName: Q.ZodNumber, coerce: (n == null ? void 0 : n.coerce) || !1, ...re(n) }); class Qe extends ae { constructor() { super(...arguments), this.min = this.gte, this.max = this.lte; } _parse(e) { if (this._def.coerce && (e.data = BigInt(e.data)), this._getType(e) !== M.bigint) { const i = this._getOrReturnCtx(e); return q(i, { code: Z.invalid_type, expected: M.bigint, received: i.parsedType }), te; } let r; const s = new Ce(); for (const i of this._def.checks) i.kind === "min" ? (i.inclusive ? e.data < i.value : e.data <= i.value) && (r = this._getOrReturnCtx(e, r), q(r, { code: Z.too_small, type: "bigint", minimum: i.value, inclusive: i.inclusive, message: i.message }), s.dirty()) : i.kind === "max" ? (i.inclusive ? e.data > i.value : e.data >= i.value) && (r = this._getOrReturnCtx(e, r), q(r, { code: Z.too_big, type: "bigint", maximum: i.value, inclusive: i.inclusive, message: i.message }), s.dirty()) : i.kind === "multipleOf" ? e.data % i.value !== BigInt(0) && (r = this._getOrReturnCtx(e, r), q(r, { code: Z.not_multiple_of, multipleOf: i.value, message: i.message }), s.dirty()) : fe.assertNever(i); return { status: s.value, value: e.data }; } gte(e, t) { return this.setLimit("min", e, !0, Y.toString(t)); } gt(e, t) { return this.setLimit("min", e, !1, Y.toString(t)); } lte(e, t) { return this.setLimit("max", e, !0, Y.toString(t)); } lt(e, t) { return this.setLimit("max", e, !1, Y.toString(t)); } setLimit(e, t, r, s) { return new Qe({ ...this._def, checks: [ ...this._def.checks, { kind: e, value: t, inclusive: r, message: Y.toString(s) } ] }); } _addCheck(e) { return new Qe({ ...this._def, checks: [...this._def.checks, e] }); } positive(e) { return this._addCheck({ kind: "min", value: BigInt(0), inclusive: !1, message: Y.toString(e) }); } negative(e) { return this._addCheck({ kind: "max", value: BigInt(0), inclusive: !1, message: Y.toString(e) }); } nonpositive(e) { return this._addCheck({ kind: "max", value: BigInt(0), inclusive: !0, message: Y.toString(e) }); } nonnegative(e) { return this._addCheck({ kind: "min", value: BigInt(0), inclusive: !0, message: Y.toString(e) }); } multipleOf(e, t) { return this._addCheck({ kind: "multipleOf", value: e, message: Y.toString(t) }); } get minValue() { let e = null; for (const t of this._def.checks) t.kind === "min" && (e === null || t.value > e) && (e = t.value); return e; } get maxValue() { let e = null; for (const t of this._def.checks) t.kind === "max" && (e === null || t.value < e) && (e = t.value); return e; } } Qe.create = (n) => { var e; return new Qe({ checks: [], typeName: Q.ZodBigInt, coerce: (e = n == null ? void 0 : n.coerce) !== null && e !== void 0 ? e : !1, ...re(n) }); }; class Bt extends ae { _parse(e) { if (this._def.coerce && (e.data = !!e.data), this._getType(e) !== M.boolean) { const r = this._getOrReturnCtx(e); return q(r, { code: Z.invalid_type, expected: M.boolean, received: r.parsedType }), te; } return Te(e.data); } } Bt.create = (n) => new Bt({ typeName: Q.ZodBoolean, coerce: (n == null ? void 0 : n.coerce) || !1, ...re(n) }); class it extends ae { _parse(e) { if (this._def.coerce && (e.data = new Date(e.data)), this._getType(e) !== M.date) { const i = this._getOrReturnCtx(e); return q(i, { code: Z.invalid_type, expected: M.date, received: i.parsedType }), te; } if (isNaN(e.data.getTime())) { const i = this._getOrReturnCtx(e); return q(i, { code: Z.invalid_date }), te; } const r = new Ce(); let s; for (const i of this._def.checks) i.kind === "min" ? e.data.getTime() < i.value && (s = this._getOrReturnCtx(e, s), q(s, { code: Z.too_small, message: i.message, inclusive: !0, exact: !1, minimum: i.value, type: "date" }), r.dirty()) : i.kind === "max" ? e.data.getTime() > i.value && (s = this._getOrReturnCtx(e, s), q(s, { code: Z.too_big, message: i.message, inclusive: !0, exact: !1, maximum: i.value, type: "date" }), r.dirty()) : fe.assertNever(i); return { status: r.value, value: new Date(e.data.getTime()) }; } _addCheck(e) { return new it({ ...this._def, checks: [...this._def.checks, e] }); } min(e, t) { return this._addCheck({ kind: "min", value: e.getTime(), message: Y.toString(t) }); } max(e, t) { return this._addCheck({ kind: "max", value: e.getTime(), message: Y.toString(t) }); } get minDate() { let e = null; for (const t of this._def.checks) t.kind === "min" && (e === null || t.value > e) && (e = t.value); return e != null ? new Date(e) : null; } get maxDate() { let e = null; for (const t of this._def.checks) t.kind === "max" && (e === null || t.value < e) && (e = t.value); return e != null ? new Date(e) : null; } } it.create = (n) => new it({ checks: [], coerce: (n == null ? void 0 : n.coerce) || !1, typeName: Q.ZodDate, ...re(n) }); class fr extends ae { _parse(e) { if (this._getType(e) !== M.symbol) { const r = this._getOrReturnCtx(e); return q(r, { code: Z.invalid_type, expected: M.symbol, received: r.parsedType }), te; } return Te(e.data); } } fr.create = (n) => new fr({ typeName: Q.ZodSymbol, ...re(n) }); class At extends ae { _parse(e) { if (this._getType(e) !== M.undefined) { const r = this._getOrReturnCtx(e); return q(r, { code: Z.invalid_type, expected: M.undefined, received: r.parsedType }), te; } return Te(e.data); } } At.create = (n) => new At({ typeName: Q.ZodUndefined, ...re(n) }); class Ot extends ae { _parse(e) { if (this._getType(e) !== M.null) { const r = this._getOrReturnCtx(e); return q(r, { code: Z.invalid_type, expected: M.null, received: r.parsedType }), te; } return Te(e.data); } } Ot.create = (n) => new Ot({ typeName: Q.ZodNull, ...re(n) }); class bt extends ae { constructor() { super(...arguments), this._any = !0; } _parse(e) { return Te(e.data); } } bt.create = (n) => new bt({ typeName: Q.ZodAny, ...re(n) }); class st extends ae { constructor() { super(...arguments), this._unknown = !0; } _parse(e) { return Te(e.data); } } st.create = (n) => new st({ typeName: Q.ZodUnknown, ...re(n) }); class Fe extends ae { _parse(e) { const t = this._getOrReturnCtx(e); return q(t, { code: Z.invalid_type, expected: M.never, received: t.parsedType }), te; } } Fe.create = (n) => new Fe({ typeName: Q.ZodNever, ...re(n) }); class lr extends ae { _parse(e) { if (this._getType(e) !== M.undefined) { const r = this._getOrReturnCtx(e); return q(r, { code: Z.invalid_type, expected: M.void, received: r.parsedType }), te; } return Te(e.data); } } lr.create = (n) => new lr({ typeName: Q.ZodVoid, ...re(n) }); class Ie extends ae { _parse(e) { const { ctx: t, status: r } = this._processInputParams(e), s = this._def; if (t.parsedType !== M.array) return q(t, { code: Z.invalid_type, expected: M.array, received: t.parsedType }), te; if (s.exactLength !== null) { const l = t.data.length > s.exactLength.value, c = t.data.length < s.exactLength.value; (l || c) && (q(t, { code: l ? Z.too_big : Z.too_small, minimum: c ? s.exactLength.value : void 0, maximum: l ? s.exactLength.value : void 0, type: "array", inclusive: !0, exact: !0, message: s.exactLength.message }), r.dirty()); } if (s.minLength !== null && t.data.length < s.minLength.value && (q(t, { code: Z.too_small, minimum: s.minLength.value, type: "array", inclusive: !0, exact: !1, message: s.minLength.message }), r.dirty()), s.maxLength !== null && t.data.length > s.maxLength.value && (q(t, { code: Z.too_big, maximum: s.maxLength.value, type: "array", inclusive: !0, exact: !1, message: s.maxLength.message }), r.dirty()), t.common.async) return Promise.all([...t.data].map((l, c) => s.type._parseAsync(new He(t, l, t.path, c)))).then((l) => Ce.mergeArray(r, l)); const i = [...t.data].map((l, c) => s.type._parseSync(new He(t, l, t.path, c))); return Ce.mergeArray(r, i); } get element() { return this._def.type; } min(e, t) { return new Ie({ ...this._def, minLength: { value: e, message: Y.toString(t) } }); } max(e, t) { return new Ie({ ...this._def, maxLength: { value: e, message: Y.toString(t) } }); } length(e, t) { return new Ie({ ...this._def, exactLength: { value: e, message: Y.toString(t) } }); } nonempty(e) { return this.min(1, e); } } Ie.create = (n, e) => new Ie({ type: n, minLength: null, maxLength: null, exactLength: null, typeName: Q.ZodArray, ...re(e) }); function _t(n) { if (n instanceof pe) { const e = {}; for (const t in n.shape) { const r = n.shape[t]; e[t] = We.create(_t(r)); } return new pe({ ...n._def, shape: () => e }); } else return n instanceof Ie ? new Ie({ ...n._def, type: _t(n.element) }) : n instanceof We ? We.create(_t(n.unwrap())) : n instanceof ct ? ct.create(_t(n.unwrap())) : n instanceof Ze ? Ze.create(n.items.map((e) => _t(e))) : n; } class pe extends ae { constructor() { super(...arguments), this._cached = null, this.nonstrict = this.passthrough, this.augment = this.extend; } _getCached() { if (this._cached !== null) return this._cached; const e = this._def.shape(), t = fe.objectKeys(e); return this._cached = { shape: e, keys: t }; } _parse(e) { if (this._getType(e) !== M.object) { const o = this._getOrReturnCtx(e); return q(o, { code: Z.invalid_type, expected: M.object, received: o.parsedType }), te; } const { status: r, ctx: s } = this._processInputParams(e), { shape: i, keys: l } = this._getCached(), c = []; if (!(this._def.catchall instanceof Fe && this._def.unknownKeys === "strip")) for (const o in s.data) l.includes(o) || c.push(o); const x = []; for (const o of l) { const u = i[o], w = s.data[o]; x.push({ key: { status: "valid", value: o }, value: u._parse(new He(s, w, s.path, o)), alwaysSet: o in s.data }); } if (this._def.catchall instanceof Fe) { const o = this._def.unknownKeys; if (o === "passthrough") for (const u of c) x.push({ key: { status: "valid", value: u }, value: { status: "valid", value: s.data[u] } }); else if (o === "strict") c.length > 0 && (q(s, { code: Z.unrecognized_keys, keys: c }), r.dirty()); else if (o !== "strip") throw new Error("Internal ZodObject error: invalid unknownKeys value."); } else { const o = this._def.catchall; for (const u of c) { const w = s.data[u]; x.push({ key: { status: "valid", value: u }, value: o._parse( new He(s, w, s.path, u) //, ctx.child(key), value, getParsedType(value) ), alwaysSet: u in s.data }); } } return s.common.async ? Promise.resolve().then(async () => { const o = []; for (const u of x) { const w = await u.key; o.push({ key: w, value: await u.value, alwaysSet: u.alwaysSet }); } return o; }).then((o) => Ce.mergeObjectSync(r, o)) : Ce.mergeObjectSync(r, x); } get shape() { return this._def.shape(); } strict(e) { return Y.errToObj, new pe({ ...this._def, unknownKeys: "strict", ...e !== void 0 ? { errorMap: (t, r) => { var s, i, l, c; const x = (l = (i = (s = this._def).errorMap) === null || i === void 0 ? void 0 : i.call(s, t, r).message) !== null && l !== void 0 ? l : r.defaultError; return t.code === "unrecognized_keys" ? { message: (c = Y.errToObj(e).message) !== null && c !== void 0 ? c : x } : { message: x }; } } : {} }); } strip() { return new pe({ ...this._def, unknownKeys: "strip" }); } passthrough() { return new pe({ ...this._def, unknownKeys: "passthrough" }); } // const AugmentFactory = // <Def extends ZodObjectDef>(def: Def) => // <Augmentation extends ZodRawShape>( // augmentation: Augmentation // ): ZodObject< // extendShape<ReturnType<Def["shape"]>, Augmentation>, // Def["unknownKeys"], // Def["catchall"] // > => { // return new ZodObject({ // ...def, // shape: () => ({ // ...def.shape(), // ...augmentation, // }), // }) as any; // }; extend(e) { return new pe({ ...this._def, shape: () => ({ ...this._def.shape(), ...e }) }); } /** * Prior to zod@1.0.12 there was a bug in the * inferred type of merged objects. Please * upgrade if you are experiencing issues. */ merge(e) { return new pe({ unknownKeys: e._def.unknownKeys, catchall: e._def.catchall, shape: () => ({ ...this._def.shape(), ...e._def.shape() }), typeName: Q.ZodObject }); } // merge< // Incoming extends AnyZodObject, // Augmentation extends Incoming["shape"], // NewOutput extends { // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation // ? Augmentation[k]["_output"] // : k extends keyof Output // ? Output[k] // : never; // }, // NewInput extends { // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation // ? Augmentation[k]["_input"] // : k extends keyof Input // ? Input[k] // : never; // } // >( // merging: Incoming // ): ZodObject< // extendShape<T, ReturnType<Incoming["_def"]["shape"]>>, // Incoming["_def"]["unknownKeys"], // Incoming["_def"]["catchall"], // NewOutput, // NewInput // > { // const merged: any = new ZodObject({ // unknownKeys: merging._def.unknownKeys, // catchall: merging._def.catchall, // shape: () => // objectUtil.mergeShapes(this._def.shape(), merging._def.shape()), // typeName: ZodFirstPartyTypeKind.ZodObject, // }) as any; // return merged; // } setKey(e, t) { return this.augment({ [e]: t }); } // merge<Incoming extends AnyZodObject>( // merging: Incoming // ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => { // ZodObject< // extendShape<T, ReturnType<Incoming["_def"]["shape"]>>, // Incoming["_def"]["unknownKeys"], // Incoming["_def"]["catchall"] // > { // // const mergedShape = objectUtil.mergeShapes( // // this._def.shape(), // // merging._def.shape() // // ); // const merged: any = new ZodObject({ // unknownKeys: merging._def.unknownKeys, // catchall: merging._def.catchall, // shape: () => // objectUtil.mergeShapes(this._def.shape(), merging._def.shape()), // typeName: ZodFirstPartyTypeKind.ZodObject, // }) as any; // return merged; // } catchall(e) { return new pe({ ...this._def, catchall: e }); } pick(e) { const t = {}; return fe.objectKeys(e).forEach((r) => { e[r] && this.shape[r] && (t[r] = this.shape[r]); }), new pe({ ...this._def, shape: () => t }); } omit(e) { const t = {}; return fe.objectKeys(this.shape).forEach((r) => { e[r] || (t[r] = this.shape[r]); }), new pe({ ...this._def, shape: () => t }); } /** * @deprecated */ deepPartial() { return _t(this); } partial(e) { const t = {}; return fe.objectKeys(this.shape).forEach((r) => { const s = this.shape[r]; e && !e[r] ? t[r] = s : t[r] = s.optional(); }), new pe({ ...this._def, shape: () => t }); } required(e) { const t = {}; return fe.objectKeys(this.shape).forEach((r) => { if (e && !e[r]) t[r] = this.shape[r]; else { let i = this.shape[r]; for (; i instanceof We; ) i = i._def.innerType; t[r] = i; } }), new pe({ ...this._def, shape: () => t }); } keyof() { return Nn(fe.objectKeys(this.shape)); } } pe.create = (n, e) => new pe({ shape: () => n, unknownKeys: "strip", catchall: Fe.create(), typeName: Q.ZodObject, ...re(e) }); pe.