UNPKG

@etherna/sdk-js

Version:

Etherna SDKs for operations on the network

1,550 lines 361 kB
var util; (function(Q) { Q.assertEqual = (n) => n; function e(n) { } Q.assertIs = e; function t(n) { throw new Error(); } Q.assertNever = t, Q.arrayToEnum = (n) => { const B = {}; for (const r of n) B[r] = r; return B; }, Q.getValidEnumValues = (n) => { const B = Q.objectKeys(n).filter((o) => typeof n[n[o]] != "number"), r = {}; for (const o of B) r[o] = n[o]; return Q.objectValues(r); }, Q.objectValues = (n) => Q.objectKeys(n).map(function(B) { return n[B]; }), Q.objectKeys = typeof Object.keys == "function" ? (n) => Object.keys(n) : (n) => { const B = []; for (const r in n) Object.prototype.hasOwnProperty.call(n, r) && B.push(r); return B; }, Q.find = (n, B) => { for (const r of n) if (B(r)) return r; }, Q.isInteger = typeof Number.isInteger == "function" ? (n) => Number.isInteger(n) : (n) => typeof n == "number" && isFinite(n) && Math.floor(n) === n; function s(n, B = " | ") { return n.map((r) => typeof r == "string" ? `'${r}'` : r).join(B); } Q.joinValues = s, Q.jsonStringifyReplacer = (n, B) => typeof B == "bigint" ? B.toString() : B; })(util || (util = {})); var objectUtil; (function(Q) { Q.mergeShapes = (e, t) => ({ ...e, ...t // second overwrites first }); })(objectUtil || (objectUtil = {})); const ZodParsedType = util.arrayToEnum([ "string", "nan", "number", "integer", "float", "boolean", "date", "bigint", "symbol", "function", "undefined", "null", "array", "object", "unknown", "promise", "void", "never", "map", "set" ]), getParsedType = (Q) => { switch (typeof Q) { case "undefined": return ZodParsedType.undefined; case "string": return ZodParsedType.string; case "number": return isNaN(Q) ? ZodParsedType.nan : ZodParsedType.number; case "boolean": return ZodParsedType.boolean; case "function": return ZodParsedType.function; case "bigint": return ZodParsedType.bigint; case "symbol": return ZodParsedType.symbol; case "object": return Array.isArray(Q) ? ZodParsedType.array : Q === null ? ZodParsedType.null : Q.then && typeof Q.then == "function" && Q.catch && typeof Q.catch == "function" ? ZodParsedType.promise : typeof Map < "u" && Q instanceof Map ? ZodParsedType.map : typeof Set < "u" && Q instanceof Set ? ZodParsedType.set : typeof Date < "u" && Q instanceof Date ? ZodParsedType.date : ZodParsedType.object; default: return ZodParsedType.unknown; } }, ZodIssueCode = util.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" ]), quotelessJson = (Q) => JSON.stringify(Q, null, 2).replace(/"([^"]+)":/g, "$1:"); class ZodError extends Error { constructor(e) { super(), this.issues = [], this.addIssue = (s) => { this.issues = [...this.issues, s]; }, this.addIssues = (s = []) => { this.issues = [...this.issues, ...s]; }; 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(B) { return B.message; }, s = { _errors: [] }, n = (B) => { for (const r of B.issues) if (r.code === "invalid_union") r.unionErrors.map(n); else if (r.code === "invalid_return_type") n(r.returnTypeError); else if (r.code === "invalid_arguments") n(r.argumentsError); else if (r.path.length === 0) s._errors.push(t(r)); else { let o = s, l = 0; for (; l < r.path.length; ) { const c = r.path[l]; l === r.path.length - 1 ? (o[c] = o[c] || { _errors: [] }, o[c]._errors.push(t(r))) : o[c] = o[c] || { _errors: [] }, o = o[c], l++; } } }; return n(this), s; } toString() { return this.message; } get message() { return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2); } get isEmpty() { return this.issues.length === 0; } flatten(e = (t) => t.message) { const t = {}, s = []; for (const n of this.issues) n.path.length > 0 ? (t[n.path[0]] = t[n.path[0]] || [], t[n.path[0]].push(e(n))) : s.push(e(n)); return { formErrors: s, fieldErrors: t }; } get formErrors() { return this.flatten(); } } ZodError.create = (Q) => new ZodError(Q); const errorMap = (Q, e) => { let t; switch (Q.code) { case ZodIssueCode.invalid_type: Q.received === ZodParsedType.undefined ? t = "Required" : t = `Expected ${Q.expected}, received ${Q.received}`; break; case ZodIssueCode.invalid_literal: t = `Invalid literal value, expected ${JSON.stringify(Q.expected, util.jsonStringifyReplacer)}`; break; case ZodIssueCode.unrecognized_keys: t = `Unrecognized key(s) in object: ${util.joinValues(Q.keys, ", ")}`; break; case ZodIssueCode.invalid_union: t = "Invalid input"; break; case ZodIssueCode.invalid_union_discriminator: t = `Invalid discriminator value. Expected ${util.joinValues(Q.options)}`; break; case ZodIssueCode.invalid_enum_value: t = `Invalid enum value. Expected ${util.joinValues(Q.options)}, received '${Q.received}'`; break; case ZodIssueCode.invalid_arguments: t = "Invalid function arguments"; break; case ZodIssueCode.invalid_return_type: t = "Invalid function return type"; break; case ZodIssueCode.invalid_date: t = "Invalid date"; break; case ZodIssueCode.invalid_string: typeof Q.validation == "object" ? "includes" in Q.validation ? (t = `Invalid input: must include "${Q.validation.includes}"`, typeof Q.validation.position == "number" && (t = `${t} at one or more positions greater than or equal to ${Q.validation.position}`)) : "startsWith" in Q.validation ? t = `Invalid input: must start with "${Q.validation.startsWith}"` : "endsWith" in Q.validation ? t = `Invalid input: must end with "${Q.validation.endsWith}"` : util.assertNever(Q.validation) : Q.validation !== "regex" ? t = `Invalid ${Q.validation}` : t = "Invalid"; break; case ZodIssueCode.too_small: Q.type === "array" ? t = `Array must contain ${Q.exact ? "exactly" : Q.inclusive ? "at least" : "more than"} ${Q.minimum} element(s)` : Q.type === "string" ? t = `String must contain ${Q.exact ? "exactly" : Q.inclusive ? "at least" : "over"} ${Q.minimum} character(s)` : Q.type === "number" ? t = `Number must be ${Q.exact ? "exactly equal to " : Q.inclusive ? "greater than or equal to " : "greater than "}${Q.minimum}` : Q.type === "date" ? t = `Date must be ${Q.exact ? "exactly equal to " : Q.inclusive ? "greater than or equal to " : "greater than "}${new Date(Number(Q.minimum))}` : t = "Invalid input"; break; case ZodIssueCode.too_big: Q.type === "array" ? t = `Array must contain ${Q.exact ? "exactly" : Q.inclusive ? "at most" : "less than"} ${Q.maximum} element(s)` : Q.type === "string" ? t = `String must contain ${Q.exact ? "exactly" : Q.inclusive ? "at most" : "under"} ${Q.maximum} character(s)` : Q.type === "number" ? t = `Number must be ${Q.exact ? "exactly" : Q.inclusive ? "less than or equal to" : "less than"} ${Q.maximum}` : Q.type === "bigint" ? t = `BigInt must be ${Q.exact ? "exactly" : Q.inclusive ? "less than or equal to" : "less than"} ${Q.maximum}` : Q.type === "date" ? t = `Date must be ${Q.exact ? "exactly" : Q.inclusive ? "smaller than or equal to" : "smaller than"} ${new Date(Number(Q.maximum))}` : t = "Invalid input"; break; case ZodIssueCode.custom: t = "Invalid input"; break; case ZodIssueCode.invalid_intersection_types: t = "Intersection results could not be merged"; break; case ZodIssueCode.not_multiple_of: t = `Number must be a multiple of ${Q.multipleOf}`; break; case ZodIssueCode.not_finite: t = "Number must be finite"; break; default: t = e.defaultError, util.assertNever(Q); } return { message: t }; }; let overrideErrorMap = errorMap; function setErrorMap(Q) { overrideErrorMap = Q; } function getErrorMap() { return overrideErrorMap; } const makeIssue = (Q) => { const { data: e, path: t, errorMaps: s, issueData: n } = Q, B = [...t, ...n.path || []], r = { ...n, path: B }; let o = ""; const l = s.filter((c) => !!c).slice().reverse(); for (const c of l) o = c(r, { data: e, defaultError: o }).message; return { ...n, path: B, message: n.message || o }; }, EMPTY_PATH = []; function addIssueToContext(Q, e) { const t = makeIssue({ issueData: e, data: Q.data, path: Q.path, errorMaps: [ Q.common.contextualErrorMap, Q.schemaErrorMap, getErrorMap(), errorMap // then global default map ].filter((s) => !!s) }); Q.common.issues.push(t); } class ParseStatus { constructor() { this.value = "valid"; } dirty() { this.value === "valid" && (this.value = "dirty"); } abort() { this.value !== "aborted" && (this.value = "aborted"); } static mergeArray(e, t) { const s = []; for (const n of t) { if (n.status === "aborted") return INVALID; n.status === "dirty" && e.dirty(), s.push(n.value); } return { status: e.value, value: s }; } static async mergeObjectAsync(e, t) { const s = []; for (const n of t) s.push({ key: await n.key, value: await n.value }); return ParseStatus.mergeObjectSync(e, s); } static mergeObjectSync(e, t) { const s = {}; for (const n of t) { const { key: B, value: r } = n; if (B.status === "aborted" || r.status === "aborted") return INVALID; B.status === "dirty" && e.dirty(), r.status === "dirty" && e.dirty(), B.value !== "__proto__" && (typeof r.value < "u" || n.alwaysSet) && (s[B.value] = r.value); } return { status: e.value, value: s }; } } const INVALID = Object.freeze({ status: "aborted" }), DIRTY = (Q) => ({ status: "dirty", value: Q }), OK = (Q) => ({ status: "valid", value: Q }), isAborted = (Q) => Q.status === "aborted", isDirty = (Q) => Q.status === "dirty", isValid = (Q) => Q.status === "valid", isAsync = (Q) => typeof Promise < "u" && Q instanceof Promise; var errorUtil; (function(Q) { Q.errToObj = (e) => typeof e == "string" ? { message: e } : e || {}, Q.toString = (e) => typeof e == "string" ? e : e?.message; })(errorUtil || (errorUtil = {})); class ParseInputLazyPath { constructor(e, t, s, n) { this._cachedPath = [], this.parent = e, this.data = t, this._path = s, this._key = n; } 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 handleResult = (Q, e) => { if (isValid(e)) return { success: !0, data: e.value }; if (!Q.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 ZodError(Q.common.issues); return this._error = t, this._error; } }; }; function processCreateParams(Q) { if (!Q) return {}; const { errorMap: e, invalid_type_error: t, required_error: s, description: n } = Q; if (e && (t || s)) throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`); return e ? { errorMap: e, description: n } : { errorMap: (r, o) => r.code !== "invalid_type" ? { message: o.defaultError } : typeof o.data > "u" ? { message: s ?? o.defaultError } : { message: t ?? o.defaultError }, description: n }; } class ZodType { 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.readonly = this.readonly.bind(this), this.isNullable = this.isNullable.bind(this), this.isOptional = this.isOptional.bind(this); } get description() { return this._def.description; } _getType(e) { return getParsedType(e.data); } _getOrReturnCtx(e, t) { return t || { common: e.parent.common, data: e.data, parsedType: getParsedType(e.data), schemaErrorMap: this._def.errorMap, path: e.path, parent: e.parent }; } _processInputParams(e) { return { status: new ParseStatus(), ctx: { common: e.parent.common, data: e.data, parsedType: getParsedType(e.data), schemaErrorMap: this._def.errorMap, path: e.path, parent: e.parent } }; } _parseSync(e) { const t = this._parse(e); if (isAsync(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 s = this.safeParse(e, t); if (s.success) return s.data; throw s.error; } safeParse(e, t) { var s; const n = { common: { issues: [], async: (s = t?.async) !== null && s !== void 0 ? s : !1, contextualErrorMap: t?.errorMap }, path: t?.path || [], schemaErrorMap: this._def.errorMap, parent: null, data: e, parsedType: getParsedType(e) }, B = this._parseSync({ data: e, path: n.path, parent: n }); return handleResult(n, B); } async parseAsync(e, t) { const s = await this.safeParseAsync(e, t); if (s.success) return s.data; throw s.error; } async safeParseAsync(e, t) { const s = { common: { issues: [], contextualErrorMap: t?.errorMap, async: !0 }, path: t?.path || [], schemaErrorMap: this._def.errorMap, parent: null, data: e, parsedType: getParsedType(e) }, n = this._parse({ data: e, path: s.path, parent: s }), B = await (isAsync(n) ? n : Promise.resolve(n)); return handleResult(s, B); } refine(e, t) { const s = (n) => typeof t == "string" || typeof t > "u" ? { message: t } : typeof t == "function" ? t(n) : t; return this._refinement((n, B) => { const r = e(n), o = () => B.addIssue({ code: ZodIssueCode.custom, ...s(n) }); return typeof Promise < "u" && r instanceof Promise ? r.then((l) => l ? !0 : (o(), !1)) : r ? !0 : (o(), !1); }); } refinement(e, t) { return this._refinement((s, n) => e(s) ? !0 : (n.addIssue(typeof t == "function" ? t(s, n) : t), !1)); } _refinement(e) { return new ZodEffects({ schema: this, typeName: ZodFirstPartyTypeKind.ZodEffects, effect: { type: "refinement", refinement: e } }); } superRefine(e) { return this._refinement(e); } optional() { return ZodOptional.create(this, this._def); } nullable() { return ZodNullable.create(this, this._def); } nullish() { return this.nullable().optional(); } array() { return ZodArray.create(this, this._def); } promise() { return ZodPromise.create(this, this._def); } or(e) { return ZodUnion.create([this, e], this._def); } and(e) { return ZodIntersection.create(this, e, this._def); } transform(e) { return new ZodEffects({ ...processCreateParams(this._def), schema: this, typeName: ZodFirstPartyTypeKind.ZodEffects, effect: { type: "transform", transform: e } }); } default(e) { const t = typeof e == "function" ? e : () => e; return new ZodDefault({ ...processCreateParams(this._def), innerType: this, defaultValue: t, typeName: ZodFirstPartyTypeKind.ZodDefault }); } brand() { return new ZodBranded({ typeName: ZodFirstPartyTypeKind.ZodBranded, type: this, ...processCreateParams(this._def) }); } catch(e) { const t = typeof e == "function" ? e : () => e; return new ZodCatch({ ...processCreateParams(this._def), innerType: this, catchValue: t, typeName: ZodFirstPartyTypeKind.ZodCatch }); } describe(e) { const t = this.constructor; return new t({ ...this._def, description: e }); } pipe(e) { return ZodPipeline.create(this, e); } readonly() { return ZodReadonly.create(this); } isOptional() { return this.safeParse(void 0).success; } isNullable() { return this.safeParse(null).success; } } const cuidRegex = /^c[^\s-]{8,}$/i, cuid2Regex = /^[a-z][a-z0-9]*$/, ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/, uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i, emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i, _emojiRegex = "^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$"; let emojiRegex; const ipv4Regex = /^(((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}))$/, ipv6Regex = /^(([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})))$/, datetimeRegex = (Q) => Q.precision ? Q.offset ? new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${Q.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`) : new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${Q.precision}}Z$`) : Q.precision === 0 ? Q.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$") : Q.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 isValidIP(Q, e) { return !!((e === "v4" || !e) && ipv4Regex.test(Q) || (e === "v6" || !e) && ipv6Regex.test(Q)); } class ZodString extends ZodType { _parse(e) { if (this._def.coerce && (e.data = String(e.data)), this._getType(e) !== ZodParsedType.string) { const B = this._getOrReturnCtx(e); return addIssueToContext( B, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.string, received: B.parsedType } // ), INVALID; } const s = new ParseStatus(); let n; for (const B of this._def.checks) if (B.kind === "min") e.data.length < B.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_small, minimum: B.value, type: "string", inclusive: !0, exact: !1, message: B.message }), s.dirty()); else if (B.kind === "max") e.data.length > B.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_big, maximum: B.value, type: "string", inclusive: !0, exact: !1, message: B.message }), s.dirty()); else if (B.kind === "length") { const r = e.data.length > B.value, o = e.data.length < B.value; (r || o) && (n = this._getOrReturnCtx(e, n), r ? addIssueToContext(n, { code: ZodIssueCode.too_big, maximum: B.value, type: "string", inclusive: !0, exact: !0, message: B.message }) : o && addIssueToContext(n, { code: ZodIssueCode.too_small, minimum: B.value, type: "string", inclusive: !0, exact: !0, message: B.message }), s.dirty()); } else if (B.kind === "email") emailRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "email", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()); else if (B.kind === "emoji") emojiRegex || (emojiRegex = new RegExp(_emojiRegex, "u")), emojiRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "emoji", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()); else if (B.kind === "uuid") uuidRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "uuid", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()); else if (B.kind === "cuid") cuidRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "cuid", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()); else if (B.kind === "cuid2") cuid2Regex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "cuid2", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()); else if (B.kind === "ulid") ulidRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "ulid", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()); else if (B.kind === "url") try { new URL(e.data); } catch { n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "url", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty(); } else B.kind === "regex" ? (B.regex.lastIndex = 0, B.regex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "regex", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty())) : B.kind === "trim" ? e.data = e.data.trim() : B.kind === "includes" ? e.data.includes(B.value, B.position) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: { includes: B.value, position: B.position }, message: B.message }), s.dirty()) : B.kind === "toLowerCase" ? e.data = e.data.toLowerCase() : B.kind === "toUpperCase" ? e.data = e.data.toUpperCase() : B.kind === "startsWith" ? e.data.startsWith(B.value) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: { startsWith: B.value }, message: B.message }), s.dirty()) : B.kind === "endsWith" ? e.data.endsWith(B.value) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: { endsWith: B.value }, message: B.message }), s.dirty()) : B.kind === "datetime" ? datetimeRegex(B).test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: "datetime", message: B.message }), s.dirty()) : B.kind === "ip" ? isValidIP(e.data, B.version) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "ip", code: ZodIssueCode.invalid_string, message: B.message }), s.dirty()) : util.assertNever(B); return { status: s.value, value: e.data }; } _regex(e, t, s) { return this.refinement((n) => e.test(n), { validation: t, code: ZodIssueCode.invalid_string, ...errorUtil.errToObj(s) }); } _addCheck(e) { return new ZodString({ ...this._def, checks: [...this._def.checks, e] }); } email(e) { return this._addCheck({ kind: "email", ...errorUtil.errToObj(e) }); } url(e) { return this._addCheck({ kind: "url", ...errorUtil.errToObj(e) }); } emoji(e) { return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(e) }); } uuid(e) { return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(e) }); } cuid(e) { return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(e) }); } cuid2(e) { return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(e) }); } ulid(e) { return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(e) }); } ip(e) { return this._addCheck({ kind: "ip", ...errorUtil.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?.precision > "u" ? null : e?.precision, offset: (t = e?.offset) !== null && t !== void 0 ? t : !1, ...errorUtil.errToObj(e?.message) }); } regex(e, t) { return this._addCheck({ kind: "regex", regex: e, ...errorUtil.errToObj(t) }); } includes(e, t) { return this._addCheck({ kind: "includes", value: e, position: t?.position, ...errorUtil.errToObj(t?.message) }); } startsWith(e, t) { return this._addCheck({ kind: "startsWith", value: e, ...errorUtil.errToObj(t) }); } endsWith(e, t) { return this._addCheck({ kind: "endsWith", value: e, ...errorUtil.errToObj(t) }); } min(e, t) { return this._addCheck({ kind: "min", value: e, ...errorUtil.errToObj(t) }); } max(e, t) { return this._addCheck({ kind: "max", value: e, ...errorUtil.errToObj(t) }); } length(e, t) { return this._addCheck({ kind: "length", value: e, ...errorUtil.errToObj(t) }); } /** * @deprecated Use z.string().min(1) instead. * @see {@link ZodString.min} */ nonempty(e) { return this.min(1, errorUtil.errToObj(e)); } trim() { return new ZodString({ ...this._def, checks: [...this._def.checks, { kind: "trim" }] }); } toLowerCase() { return new ZodString({ ...this._def, checks: [...this._def.checks, { kind: "toLowerCase" }] }); } toUpperCase() { return new ZodString({ ...this._def, checks: [...this._def.checks, { kind: "toUpperCase" }] }); } 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; } } ZodString.create = (Q) => { var e; return new ZodString({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString, coerce: (e = Q?.coerce) !== null && e !== void 0 ? e : !1, ...processCreateParams(Q) }); }; function floatSafeRemainder(Q, e) { const t = (Q.toString().split(".")[1] || "").length, s = (e.toString().split(".")[1] || "").length, n = t > s ? t : s, B = parseInt(Q.toFixed(n).replace(".", "")), r = parseInt(e.toFixed(n).replace(".", "")); return B % r / Math.pow(10, n); } class ZodNumber extends ZodType { 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) !== ZodParsedType.number) { const B = this._getOrReturnCtx(e); return addIssueToContext(B, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.number, received: B.parsedType }), INVALID; } let s; const n = new ParseStatus(); for (const B of this._def.checks) B.kind === "int" ? util.isInteger(e.data) || (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: "integer", received: "float", message: B.message }), n.dirty()) : B.kind === "min" ? (B.inclusive ? e.data < B.value : e.data <= B.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_small, minimum: B.value, type: "number", inclusive: B.inclusive, exact: !1, message: B.message }), n.dirty()) : B.kind === "max" ? (B.inclusive ? e.data > B.value : e.data >= B.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_big, maximum: B.value, type: "number", inclusive: B.inclusive, exact: !1, message: B.message }), n.dirty()) : B.kind === "multipleOf" ? floatSafeRemainder(e.data, B.value) !== 0 && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.not_multiple_of, multipleOf: B.value, message: B.message }), n.dirty()) : B.kind === "finite" ? Number.isFinite(e.data) || (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.not_finite, message: B.message }), n.dirty()) : util.assertNever(B); return { status: n.value, value: e.data }; } gte(e, t) { return this.setLimit("min", e, !0, errorUtil.toString(t)); } gt(e, t) { return this.setLimit("min", e, !1, errorUtil.toString(t)); } lte(e, t) { return this.setLimit("max", e, !0, errorUtil.toString(t)); } lt(e, t) { return this.setLimit("max", e, !1, errorUtil.toString(t)); } setLimit(e, t, s, n) { return new ZodNumber({ ...this._def, checks: [ ...this._def.checks, { kind: e, value: t, inclusive: s, message: errorUtil.toString(n) } ] }); } _addCheck(e) { return new ZodNumber({ ...this._def, checks: [...this._def.checks, e] }); } int(e) { return this._addCheck({ kind: "int", message: errorUtil.toString(e) }); } positive(e) { return this._addCheck({ kind: "min", value: 0, inclusive: !1, message: errorUtil.toString(e) }); } negative(e) { return this._addCheck({ kind: "max", value: 0, inclusive: !1, message: errorUtil.toString(e) }); } nonpositive(e) { return this._addCheck({ kind: "max", value: 0, inclusive: !0, message: errorUtil.toString(e) }); } nonnegative(e) { return this._addCheck({ kind: "min", value: 0, inclusive: !0, message: errorUtil.toString(e) }); } multipleOf(e, t) { return this._addCheck({ kind: "multipleOf", value: e, message: errorUtil.toString(t) }); } finite(e) { return this._addCheck({ kind: "finite", message: errorUtil.toString(e) }); } safe(e) { return this._addCheck({ kind: "min", inclusive: !0, value: Number.MIN_SAFE_INTEGER, message: errorUtil.toString(e) })._addCheck({ kind: "max", inclusive: !0, value: Number.MAX_SAFE_INTEGER, message: errorUtil.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" && util.isInteger(e.value)); } get isFinite() { let e = null, t = null; for (const s of this._def.checks) { if (s.kind === "finite" || s.kind === "int" || s.kind === "multipleOf") return !0; s.kind === "min" ? (t === null || s.value > t) && (t = s.value) : s.kind === "max" && (e === null || s.value < e) && (e = s.value); } return Number.isFinite(t) && Number.isFinite(e); } } ZodNumber.create = (Q) => new ZodNumber({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber, coerce: Q?.coerce || !1, ...processCreateParams(Q) }); class ZodBigInt extends ZodType { 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) !== ZodParsedType.bigint) { const B = this._getOrReturnCtx(e); return addIssueToContext(B, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.bigint, received: B.parsedType }), INVALID; } let s; const n = new ParseStatus(); for (const B of this._def.checks) B.kind === "min" ? (B.inclusive ? e.data < B.value : e.data <= B.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_small, type: "bigint", minimum: B.value, inclusive: B.inclusive, message: B.message }), n.dirty()) : B.kind === "max" ? (B.inclusive ? e.data > B.value : e.data >= B.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_big, type: "bigint", maximum: B.value, inclusive: B.inclusive, message: B.message }), n.dirty()) : B.kind === "multipleOf" ? e.data % B.value !== BigInt(0) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.not_multiple_of, multipleOf: B.value, message: B.message }), n.dirty()) : util.assertNever(B); return { status: n.value, value: e.data }; } gte(e, t) { return this.setLimit("min", e, !0, errorUtil.toString(t)); } gt(e, t) { return this.setLimit("min", e, !1, errorUtil.toString(t)); } lte(e, t) { return this.setLimit("max", e, !0, errorUtil.toString(t)); } lt(e, t) { return this.setLimit("max", e, !1, errorUtil.toString(t)); } setLimit(e, t, s, n) { return new ZodBigInt({ ...this._def, checks: [ ...this._def.checks, { kind: e, value: t, inclusive: s, message: errorUtil.toString(n) } ] }); } _addCheck(e) { return new ZodBigInt({ ...this._def, checks: [...this._def.checks, e] }); } positive(e) { return this._addCheck({ kind: "min", value: BigInt(0), inclusive: !1, message: errorUtil.toString(e) }); } negative(e) { return this._addCheck({ kind: "max", value: BigInt(0), inclusive: !1, message: errorUtil.toString(e) }); } nonpositive(e) { return this._addCheck({ kind: "max", value: BigInt(0), inclusive: !0, message: errorUtil.toString(e) }); } nonnegative(e) { return this._addCheck({ kind: "min", value: BigInt(0), inclusive: !0, message: errorUtil.toString(e) }); } multipleOf(e, t) { return this._addCheck({ kind: "multipleOf", value: e, message: errorUtil.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; } } ZodBigInt.create = (Q) => { var e; return new ZodBigInt({ checks: [], typeName: ZodFirstPartyTypeKind.ZodBigInt, coerce: (e = Q?.coerce) !== null && e !== void 0 ? e : !1, ...processCreateParams(Q) }); }; class ZodBoolean extends ZodType { _parse(e) { if (this._def.coerce && (e.data = !!e.data), this._getType(e) !== ZodParsedType.boolean) { const s = this._getOrReturnCtx(e); return addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.boolean, received: s.parsedType }), INVALID; } return OK(e.data); } } ZodBoolean.create = (Q) => new ZodBoolean({ typeName: ZodFirstPartyTypeKind.ZodBoolean, coerce: Q?.coerce || !1, ...processCreateParams(Q) }); class ZodDate extends ZodType { _parse(e) { if (this._def.coerce && (e.data = new Date(e.data)), this._getType(e) !== ZodParsedType.date) { const B = this._getOrReturnCtx(e); return addIssueToContext(B, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.date, received: B.parsedType }), INVALID; } if (isNaN(e.data.getTime())) { const B = this._getOrReturnCtx(e); return addIssueToContext(B, { code: ZodIssueCode.invalid_date }), INVALID; } const s = new ParseStatus(); let n; for (const B of this._def.checks) B.kind === "min" ? e.data.getTime() < B.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_small, message: B.message, inclusive: !0, exact: !1, minimum: B.value, type: "date" }), s.dirty()) : B.kind === "max" ? e.data.getTime() > B.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_big, message: B.message, inclusive: !0, exact: !1, maximum: B.value, type: "date" }), s.dirty()) : util.assertNever(B); return { status: s.value, value: new Date(e.data.getTime()) }; } _addCheck(e) { return new ZodDate({ ...this._def, checks: [...this._def.checks, e] }); } min(e, t) { return this._addCheck({ kind: "min", value: e.getTime(), message: errorUtil.toString(t) }); } max(e, t) { return this._addCheck({ kind: "max", value: e.getTime(), message: errorUtil.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; } } ZodDate.create = (Q) => new ZodDate({ checks: [], coerce: Q?.coerce || !1, typeName: ZodFirstPartyTypeKind.ZodDate, ...processCreateParams(Q) }); class ZodSymbol extends ZodType { _parse(e) { if (this._getType(e) !== ZodParsedType.symbol) { const s = this._getOrReturnCtx(e); return addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.symbol, received: s.parsedType }), INVALID; } return OK(e.data); } } ZodSymbol.create = (Q) => new ZodSymbol({ typeName: ZodFirstPartyTypeKind.ZodSymbol, ...processCreateParams(Q) }); class ZodUndefined extends ZodType { _parse(e) { if (this._getType(e) !== ZodParsedType.undefined) { const s = this._getOrReturnCtx(e); return addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.undefined, received: s.parsedType }), INVALID; } return OK(e.data); } } ZodUndefined.create = (Q) => new ZodUndefined({ typeName: ZodFirstPartyTypeKind.ZodUndefined, ...processCreateParams(Q) }); class ZodNull extends ZodType { _parse(e) { if (this._getType(e) !== ZodParsedType.null) { const s = this._getOrReturnCtx(e); return addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.null, received: s.parsedType }), INVALID; } return OK(e.data); } } ZodNull.create = (Q) => new ZodNull({ typeName: ZodFirstPartyTypeKind.ZodNull, ...processCreateParams(Q) }); class ZodAny extends ZodType { constructor() { super(...arguments), this._any = !0; } _parse(e) { return OK(e.data); } } ZodAny.create = (Q) => new ZodAny({ typeName: ZodFirstPartyTypeKind.ZodAny, ...processCreateParams(Q) }); class ZodUnknown extends ZodType { constructor() { super(...arguments), this._unknown = !0; } _parse(e) { return OK(e.data); } } ZodUnknown.create = (Q) => new ZodUnknown({ typeName: ZodFirstPartyTypeKind.ZodUnknown, ...processCreateParams(Q) }); class ZodNever extends ZodType { _parse(e) { const t = this._getOrReturnCtx(e); return addIssueToContext(t, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.never, received: t.parsedType }), INVALID; } } ZodNever.create = (Q) => new ZodNever({ typeName: ZodFirstPartyTypeKind.ZodNever, ...processCreateParams(Q) }); class ZodVoid extends ZodType { _parse(e) { if (this._getType(e) !== ZodParsedType.undefined) { const s = this._getOrReturnCtx(e); return addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.void, received: s.parsedType }), INVALID; } return OK(e.data); } } ZodVoid.create = (Q) => new ZodVoid({ typeName: ZodFirstPartyTypeKind.ZodVoid, ...processCreateParams(Q) }); class ZodArray extends ZodType { _parse(e) { const { ctx: t, status: s } = this._processInputParams(e), n = this._def; if (t.parsedType !== ZodParsedType.array) return addIssueToContext(t, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.array, received: t.parsedType }), INVALID; if (n.exactLength !== null) { const r = t.data.length > n.exactLength.value, o = t.data.length < n.exactLength.value; (r || o) && (addIssueToContext(t, { code: r ? ZodIssueCode.too_big : ZodIssueCode.too_small, minimum: o ? n.exactLength.value : void 0, maximum: r ? n.exactLength.value : void 0, type: "array", inclusive: !0, exact: !0, message: n.exactLength.message }), s.dirty()); } if (n.minLength !== null && t.data.length < n.minLength.value && (addIssueToContext(t, { code: ZodIssueCode.too_small, minimum: n.minLength.value, type: "array", inclusive: !0, exact: !1, message: n.minLength.message }), s.dirty()), n.maxLength !== null && t.data.length > n.maxLength.value && (addIssueToContext(t, { code: ZodIssueCode.too_big, maximum: n.maxLength.value, type: "array", inclusive: !0, exact: !1, message: n.maxLength.message }), s.dirty()), t.common.async) return Promise.all([...t.data].map((r, o) => n.type._parseAsync(new ParseInputLazyPath(t, r, t.path, o)))).then((r) => ParseStatus.mergeArray(s, r)); const B = [...t.data].map((r, o) => n.type._parseSync(new ParseInputLazyPath(t, r, t.path, o))); return ParseStatus.mergeArray(s, B); } get element() { return this._def.type; } min(e, t) { return new ZodArray({ ...this._def, minLength: { value: e, message: errorUtil.toString(t) } }); } max(e, t) { return new ZodArray({ ...this._def, maxLength: { value: e, message: errorUtil.toString(t) } }); } length(e, t) { return new ZodArray({ ...this._def, exactLength: { value: e, message: errorUtil.toString(t) } }); } nonempty(e) { return this.min(1, e); } } ZodArray.create = (Q, e) => new ZodArray({ type: Q, minLength: null, maxLength: null, exactLength: null, typeName: ZodFirstPartyTypeKind.ZodArray, ...processCreateParams(e) }); function deepPartialify(Q) { if (Q instanceof ZodObject) { const e = {}; for (const t in Q.shape) { const s = Q.shape[t]; e[t] = ZodOptional.create(deepPartialify(s)); } return new ZodObject({ ...Q._def, shape: () => e }); } else return Q instanceof ZodArray ? new ZodArray({ ...Q._def, type: deepPartialify(Q.element) }) : Q instanceof ZodOptional ? ZodOptional.create(deepPartialify(Q.unwrap())) : Q instanceof ZodNullable ? ZodNullable.create(deepPartialify(Q.unwrap())) : Q instanceof ZodTuple ? ZodTuple.create(Q.items.map((e) => deepPartialify(e))) : Q; } class ZodObject extends ZodType { 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 = util.objectKeys(e); return this._cached = { shape: e, keys: t }; } _parse(e) { if (this._getType(e) !== ZodParsedType.object) { const c = this._getOrReturnCtx(e); return addIssueToContext(c, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.object, received: c.parsedType }), INVALID; } const { status: s, ctx: n } = this._processInputParams(e), { shape: B, keys: r } = this._getCached(), o = []; if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) for (const c in n.data) r.includes(c) || o.push(c); const l = []; for (const c of r) { const p = B[c], V = n.data[c]; l.push({ key: { status: "valid", value: c }, value: p._parse(new ParseInputLazyPath(n, V, n.path, c)), alwaysSet: c in n.data }); } if (this._def.catchall instanceof ZodNever) { const c = this._def.unknownKeys; if (c === "passthrough") for (const p of o) l.push({ key: { status: "valid", value: p }, value: { status: "valid", value: n.data[p] } }); else if (c === "strict") o.length > 0 && (addIssueToContext(n, { code: ZodIssueCode.unrecognized_keys, keys: o }), s.dirty()); else if (c !== "strip") throw new Error("Internal ZodObject error: invalid unknownKeys value."); } else { const c = this._def.catchall; for (const p of o) { const V = n.data[p]; l.push({ key: { status: "valid", value: p }, value: c._parse( new ParseInputLazyPath(n, V, n.path, p) //, ctx.child(key), value, getParsedType(value) ), alwaysSet: p in n.data }); } } return n.common.async ? Promise.resolve().then(async () => { const c = []; for (const p of l) { const V = await p.key; c.push({ key: V, value: await p.value, alwaysSet: p.alwaysSet }); } return c; }).then((c) => ParseStatus.mergeObjectSync(s, c)) : ParseStatus.mergeObjectSync(s, l); } get shape() { return this._def.shape(); } strict(e) { return errorUtil.errToObj, new ZodObject({ ...this._def, unknownKeys: "strict", ...e !== void 0 ? { errorMap: (t, s) => { var n, B, r, o; const l = (r = (B = (n = this._def).errorMap) === null || B === void 0 ? void 0 : B.call(n, t, s).message) !== null && r !== void 0 ? r : s.defaultError; return t.code === "unrecognized_keys" ? { message: (o = errorUtil.errToObj(e).message) !== null && o !== void 0 ? o : l } : { message: l }; } } : {} }); } strip() { return new ZodObject({ ...this._def, unknownKeys: "strip" }); } passthrough() { return new ZodObject({ ...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 ZodObject({ ...this._def, shape: () => ({ ...this._def.shape(), ...e }) }); } /** * Prior to zod@1.0.12