UNPKG

@etherna/sdk-js

Version:

Etherna SDKs for operations on the network

1,539 lines 674 kB
var Nt = Object.defineProperty; var St = (t, e, r) => e in t ? Nt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r; var te = (t, e, r) => (St(t, typeof e != "symbol" ? e + "" : e, r), r); import { immerable } from "immer"; var util; (function(t) { t.assertEqual = (n) => n; function e(n) { } t.assertIs = e; function r(n) { throw new Error(); } t.assertNever = r, t.arrayToEnum = (n) => { const a = {}; for (const o of n) a[o] = o; return a; }, t.getValidEnumValues = (n) => { const a = t.objectKeys(n).filter((c) => typeof n[n[c]] != "number"), o = {}; for (const c of a) o[c] = n[c]; return t.objectValues(o); }, t.objectValues = (n) => t.objectKeys(n).map(function(a) { return n[a]; }), t.objectKeys = typeof Object.keys == "function" ? (n) => Object.keys(n) : (n) => { const a = []; for (const o in n) Object.prototype.hasOwnProperty.call(n, o) && a.push(o); return a; }, t.find = (n, a) => { for (const o of n) if (a(o)) return o; }, t.isInteger = typeof Number.isInteger == "function" ? (n) => Number.isInteger(n) : (n) => typeof n == "number" && isFinite(n) && Math.floor(n) === n; function s(n, a = " | ") { return n.map((o) => typeof o == "string" ? `'${o}'` : o).join(a); } t.joinValues = s, t.jsonStringifyReplacer = (n, a) => typeof a == "bigint" ? a.toString() : a; })(util || (util = {})); var objectUtil; (function(t) { t.mergeShapes = (e, r) => ({ ...e, ...r // 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 = (t) => { switch (typeof t) { case "undefined": return ZodParsedType.undefined; case "string": return ZodParsedType.string; case "number": return isNaN(t) ? 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(t) ? ZodParsedType.array : t === null ? ZodParsedType.null : t.then && typeof t.then == "function" && t.catch && typeof t.catch == "function" ? ZodParsedType.promise : typeof Map < "u" && t instanceof Map ? ZodParsedType.map : typeof Set < "u" && t instanceof Set ? ZodParsedType.set : typeof Date < "u" && t 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 = (t) => JSON.stringify(t, 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 r = new.target.prototype; Object.setPrototypeOf ? Object.setPrototypeOf(this, r) : this.__proto__ = r, this.name = "ZodError", this.issues = e; } get errors() { return this.issues; } format(e) { const r = e || function(a) { return a.message; }, s = { _errors: [] }, n = (a) => { for (const o of a.issues) if (o.code === "invalid_union") o.unionErrors.map(n); else if (o.code === "invalid_return_type") n(o.returnTypeError); else if (o.code === "invalid_arguments") n(o.argumentsError); else if (o.path.length === 0) s._errors.push(r(o)); else { let c = s, l = 0; for (; l < o.path.length; ) { const Q = o.path[l]; l === o.path.length - 1 ? (c[Q] = c[Q] || { _errors: [] }, c[Q]._errors.push(r(o))) : c[Q] = c[Q] || { _errors: [] }, c = c[Q], 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 = (r) => r.message) { const r = {}, s = []; for (const n of this.issues) n.path.length > 0 ? (r[n.path[0]] = r[n.path[0]] || [], r[n.path[0]].push(e(n))) : s.push(e(n)); return { formErrors: s, fieldErrors: r }; } get formErrors() { return this.flatten(); } } ZodError.create = (t) => new ZodError(t); const errorMap = (t, e) => { let r; switch (t.code) { case ZodIssueCode.invalid_type: t.received === ZodParsedType.undefined ? r = "Required" : r = `Expected ${t.expected}, received ${t.received}`; break; case ZodIssueCode.invalid_literal: r = `Invalid literal value, expected ${JSON.stringify(t.expected, util.jsonStringifyReplacer)}`; break; case ZodIssueCode.unrecognized_keys: r = `Unrecognized key(s) in object: ${util.joinValues(t.keys, ", ")}`; break; case ZodIssueCode.invalid_union: r = "Invalid input"; break; case ZodIssueCode.invalid_union_discriminator: r = `Invalid discriminator value. Expected ${util.joinValues(t.options)}`; break; case ZodIssueCode.invalid_enum_value: r = `Invalid enum value. Expected ${util.joinValues(t.options)}, received '${t.received}'`; break; case ZodIssueCode.invalid_arguments: r = "Invalid function arguments"; break; case ZodIssueCode.invalid_return_type: r = "Invalid function return type"; break; case ZodIssueCode.invalid_date: r = "Invalid date"; break; case ZodIssueCode.invalid_string: typeof t.validation == "object" ? "includes" in t.validation ? (r = `Invalid input: must include "${t.validation.includes}"`, typeof t.validation.position == "number" && (r = `${r} at one or more positions greater than or equal to ${t.validation.position}`)) : "startsWith" in t.validation ? r = `Invalid input: must start with "${t.validation.startsWith}"` : "endsWith" in t.validation ? r = `Invalid input: must end with "${t.validation.endsWith}"` : util.assertNever(t.validation) : t.validation !== "regex" ? r = `Invalid ${t.validation}` : r = "Invalid"; break; case ZodIssueCode.too_small: t.type === "array" ? r = `Array must contain ${t.exact ? "exactly" : t.inclusive ? "at least" : "more than"} ${t.minimum} element(s)` : t.type === "string" ? r = `String must contain ${t.exact ? "exactly" : t.inclusive ? "at least" : "over"} ${t.minimum} character(s)` : t.type === "number" ? r = `Number must be ${t.exact ? "exactly equal to " : t.inclusive ? "greater than or equal to " : "greater than "}${t.minimum}` : t.type === "date" ? r = `Date must be ${t.exact ? "exactly equal to " : t.inclusive ? "greater than or equal to " : "greater than "}${new Date(Number(t.minimum))}` : r = "Invalid input"; break; case ZodIssueCode.too_big: t.type === "array" ? r = `Array must contain ${t.exact ? "exactly" : t.inclusive ? "at most" : "less than"} ${t.maximum} element(s)` : t.type === "string" ? r = `String must contain ${t.exact ? "exactly" : t.inclusive ? "at most" : "under"} ${t.maximum} character(s)` : t.type === "number" ? r = `Number must be ${t.exact ? "exactly" : t.inclusive ? "less than or equal to" : "less than"} ${t.maximum}` : t.type === "bigint" ? r = `BigInt must be ${t.exact ? "exactly" : t.inclusive ? "less than or equal to" : "less than"} ${t.maximum}` : t.type === "date" ? r = `Date must be ${t.exact ? "exactly" : t.inclusive ? "smaller than or equal to" : "smaller than"} ${new Date(Number(t.maximum))}` : r = "Invalid input"; break; case ZodIssueCode.custom: r = "Invalid input"; break; case ZodIssueCode.invalid_intersection_types: r = "Intersection results could not be merged"; break; case ZodIssueCode.not_multiple_of: r = `Number must be a multiple of ${t.multipleOf}`; break; case ZodIssueCode.not_finite: r = "Number must be finite"; break; default: r = e.defaultError, util.assertNever(t); } return { message: r }; }; let overrideErrorMap = errorMap; function setErrorMap(t) { overrideErrorMap = t; } function getErrorMap() { return overrideErrorMap; } const makeIssue = (t) => { const { data: e, path: r, errorMaps: s, issueData: n } = t, a = [...r, ...n.path || []], o = { ...n, path: a }; let c = ""; const l = s.filter((Q) => !!Q).slice().reverse(); for (const Q of l) c = Q(o, { data: e, defaultError: c }).message; return { ...n, path: a, message: n.message || c }; }, EMPTY_PATH = []; function addIssueToContext(t, e) { const r = makeIssue({ issueData: e, data: t.data, path: t.path, errorMaps: [ t.common.contextualErrorMap, t.schemaErrorMap, getErrorMap(), errorMap // then global default map ].filter((s) => !!s) }); t.common.issues.push(r); } class ParseStatus { constructor() { this.value = "valid"; } dirty() { this.value === "valid" && (this.value = "dirty"); } abort() { this.value !== "aborted" && (this.value = "aborted"); } static mergeArray(e, r) { const s = []; for (const n of r) { 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, r) { const s = []; for (const n of r) s.push({ key: await n.key, value: await n.value }); return ParseStatus.mergeObjectSync(e, s); } static mergeObjectSync(e, r) { const s = {}; for (const n of r) { const { key: a, value: o } = n; if (a.status === "aborted" || o.status === "aborted") return INVALID; a.status === "dirty" && e.dirty(), o.status === "dirty" && e.dirty(), a.value !== "__proto__" && (typeof o.value < "u" || n.alwaysSet) && (s[a.value] = o.value); } return { status: e.value, value: s }; } } const INVALID = Object.freeze({ status: "aborted" }), DIRTY = (t) => ({ status: "dirty", value: t }), OK = (t) => ({ status: "valid", value: t }), isAborted = (t) => t.status === "aborted", isDirty = (t) => t.status === "dirty", isValid = (t) => t.status === "valid", isAsync = (t) => typeof Promise < "u" && t instanceof Promise; var errorUtil; (function(t) { t.errToObj = (e) => typeof e == "string" ? { message: e } : e || {}, t.toString = (e) => typeof e == "string" ? e : e?.message; })(errorUtil || (errorUtil = {})); class ParseInputLazyPath { constructor(e, r, s, n) { this._cachedPath = [], this.parent = e, this.data = r, 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 = (t, e) => { if (isValid(e)) return { success: !0, data: e.value }; if (!t.common.issues.length) throw new Error("Validation failed but no issues detected."); return { success: !1, get error() { if (this._error) return this._error; const r = new ZodError(t.common.issues); return this._error = r, this._error; } }; }; function processCreateParams(t) { if (!t) return {}; const { errorMap: e, invalid_type_error: r, required_error: s, description: n } = t; if (e && (r || 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: (o, c) => o.code !== "invalid_type" ? { message: c.defaultError } : typeof c.data > "u" ? { message: s ?? c.defaultError } : { message: r ?? c.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, r) { return r || { 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 r = this._parse(e); if (isAsync(r)) throw new Error("Synchronous parse encountered promise."); return r; } _parseAsync(e) { const r = this._parse(e); return Promise.resolve(r); } parse(e, r) { const s = this.safeParse(e, r); if (s.success) return s.data; throw s.error; } safeParse(e, r) { var s; const n = { common: { issues: [], async: (s = r?.async) !== null && s !== void 0 ? s : !1, contextualErrorMap: r?.errorMap }, path: r?.path || [], schemaErrorMap: this._def.errorMap, parent: null, data: e, parsedType: getParsedType(e) }, a = this._parseSync({ data: e, path: n.path, parent: n }); return handleResult(n, a); } async parseAsync(e, r) { const s = await this.safeParseAsync(e, r); if (s.success) return s.data; throw s.error; } async safeParseAsync(e, r) { const s = { common: { issues: [], contextualErrorMap: r?.errorMap, async: !0 }, path: r?.path || [], schemaErrorMap: this._def.errorMap, parent: null, data: e, parsedType: getParsedType(e) }, n = this._parse({ data: e, path: s.path, parent: s }), a = await (isAsync(n) ? n : Promise.resolve(n)); return handleResult(s, a); } refine(e, r) { const s = (n) => typeof r == "string" || typeof r > "u" ? { message: r } : typeof r == "function" ? r(n) : r; return this._refinement((n, a) => { const o = e(n), c = () => a.addIssue({ code: ZodIssueCode.custom, ...s(n) }); return typeof Promise < "u" && o instanceof Promise ? o.then((l) => l ? !0 : (c(), !1)) : o ? !0 : (c(), !1); }); } refinement(e, r) { return this._refinement((s, n) => e(s) ? !0 : (n.addIssue(typeof r == "function" ? r(s, n) : r), !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 r = typeof e == "function" ? e : () => e; return new ZodDefault({ ...processCreateParams(this._def), innerType: this, defaultValue: r, typeName: ZodFirstPartyTypeKind.ZodDefault }); } brand() { return new ZodBranded({ typeName: ZodFirstPartyTypeKind.ZodBranded, type: this, ...processCreateParams(this._def) }); } catch(e) { const r = typeof e == "function" ? e : () => e; return new ZodCatch({ ...processCreateParams(this._def), innerType: this, catchValue: r, typeName: ZodFirstPartyTypeKind.ZodCatch }); } describe(e) { const r = this.constructor; return new r({ ...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 = (t) => t.precision ? t.offset ? new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${t.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`) : new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${t.precision}}Z$`) : t.precision === 0 ? t.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$") : t.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(t, e) { return !!((e === "v4" || !e) && ipv4Regex.test(t) || (e === "v6" || !e) && ipv6Regex.test(t)); } class ZodString extends ZodType { _parse(e) { if (this._def.coerce && (e.data = String(e.data)), this._getType(e) !== ZodParsedType.string) { const a = this._getOrReturnCtx(e); return addIssueToContext( a, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.string, received: a.parsedType } // ), INVALID; } const s = new ParseStatus(); let n; for (const a of this._def.checks) if (a.kind === "min") e.data.length < a.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_small, minimum: a.value, type: "string", inclusive: !0, exact: !1, message: a.message }), s.dirty()); else if (a.kind === "max") e.data.length > a.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_big, maximum: a.value, type: "string", inclusive: !0, exact: !1, message: a.message }), s.dirty()); else if (a.kind === "length") { const o = e.data.length > a.value, c = e.data.length < a.value; (o || c) && (n = this._getOrReturnCtx(e, n), o ? addIssueToContext(n, { code: ZodIssueCode.too_big, maximum: a.value, type: "string", inclusive: !0, exact: !0, message: a.message }) : c && addIssueToContext(n, { code: ZodIssueCode.too_small, minimum: a.value, type: "string", inclusive: !0, exact: !0, message: a.message }), s.dirty()); } else if (a.kind === "email") emailRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "email", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty()); else if (a.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: a.message }), s.dirty()); else if (a.kind === "uuid") uuidRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "uuid", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty()); else if (a.kind === "cuid") cuidRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "cuid", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty()); else if (a.kind === "cuid2") cuid2Regex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "cuid2", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty()); else if (a.kind === "ulid") ulidRegex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "ulid", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty()); else if (a.kind === "url") try { new URL(e.data); } catch { n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "url", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty(); } else a.kind === "regex" ? (a.regex.lastIndex = 0, a.regex.test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "regex", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty())) : a.kind === "trim" ? e.data = e.data.trim() : a.kind === "includes" ? e.data.includes(a.value, a.position) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: { includes: a.value, position: a.position }, message: a.message }), s.dirty()) : a.kind === "toLowerCase" ? e.data = e.data.toLowerCase() : a.kind === "toUpperCase" ? e.data = e.data.toUpperCase() : a.kind === "startsWith" ? e.data.startsWith(a.value) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: { startsWith: a.value }, message: a.message }), s.dirty()) : a.kind === "endsWith" ? e.data.endsWith(a.value) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: { endsWith: a.value }, message: a.message }), s.dirty()) : a.kind === "datetime" ? datetimeRegex(a).test(e.data) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.invalid_string, validation: "datetime", message: a.message }), s.dirty()) : a.kind === "ip" ? isValidIP(e.data, a.version) || (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { validation: "ip", code: ZodIssueCode.invalid_string, message: a.message }), s.dirty()) : util.assertNever(a); return { status: s.value, value: e.data }; } _regex(e, r, s) { return this.refinement((n) => e.test(n), { validation: r, 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 r; 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: (r = e?.offset) !== null && r !== void 0 ? r : !1, ...errorUtil.errToObj(e?.message) }); } regex(e, r) { return this._addCheck({ kind: "regex", regex: e, ...errorUtil.errToObj(r) }); } includes(e, r) { return this._addCheck({ kind: "includes", value: e, position: r?.position, ...errorUtil.errToObj(r?.message) }); } startsWith(e, r) { return this._addCheck({ kind: "startsWith", value: e, ...errorUtil.errToObj(r) }); } endsWith(e, r) { return this._addCheck({ kind: "endsWith", value: e, ...errorUtil.errToObj(r) }); } min(e, r) { return this._addCheck({ kind: "min", value: e, ...errorUtil.errToObj(r) }); } max(e, r) { return this._addCheck({ kind: "max", value: e, ...errorUtil.errToObj(r) }); } length(e, r) { return this._addCheck({ kind: "length", value: e, ...errorUtil.errToObj(r) }); } /** * @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 r of this._def.checks) r.kind === "min" && (e === null || r.value > e) && (e = r.value); return e; } get maxLength() { let e = null; for (const r of this._def.checks) r.kind === "max" && (e === null || r.value < e) && (e = r.value); return e; } } ZodString.create = (t) => { var e; return new ZodString({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString, coerce: (e = t?.coerce) !== null && e !== void 0 ? e : !1, ...processCreateParams(t) }); }; function floatSafeRemainder(t, e) { const r = (t.toString().split(".")[1] || "").length, s = (e.toString().split(".")[1] || "").length, n = r > s ? r : s, a = parseInt(t.toFixed(n).replace(".", "")), o = parseInt(e.toFixed(n).replace(".", "")); return a % o / 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 a = this._getOrReturnCtx(e); return addIssueToContext(a, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.number, received: a.parsedType }), INVALID; } let s; const n = new ParseStatus(); for (const a of this._def.checks) a.kind === "int" ? util.isInteger(e.data) || (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.invalid_type, expected: "integer", received: "float", message: a.message }), n.dirty()) : a.kind === "min" ? (a.inclusive ? e.data < a.value : e.data <= a.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_small, minimum: a.value, type: "number", inclusive: a.inclusive, exact: !1, message: a.message }), n.dirty()) : a.kind === "max" ? (a.inclusive ? e.data > a.value : e.data >= a.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_big, maximum: a.value, type: "number", inclusive: a.inclusive, exact: !1, message: a.message }), n.dirty()) : a.kind === "multipleOf" ? floatSafeRemainder(e.data, a.value) !== 0 && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.not_multiple_of, multipleOf: a.value, message: a.message }), n.dirty()) : a.kind === "finite" ? Number.isFinite(e.data) || (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.not_finite, message: a.message }), n.dirty()) : util.assertNever(a); return { status: n.value, value: e.data }; } gte(e, r) { return this.setLimit("min", e, !0, errorUtil.toString(r)); } gt(e, r) { return this.setLimit("min", e, !1, errorUtil.toString(r)); } lte(e, r) { return this.setLimit("max", e, !0, errorUtil.toString(r)); } lt(e, r) { return this.setLimit("max", e, !1, errorUtil.toString(r)); } setLimit(e, r, s, n) { return new ZodNumber({ ...this._def, checks: [ ...this._def.checks, { kind: e, value: r, 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, r) { return this._addCheck({ kind: "multipleOf", value: e, message: errorUtil.toString(r) }); } 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 r of this._def.checks) r.kind === "min" && (e === null || r.value > e) && (e = r.value); return e; } get maxValue() { let e = null; for (const r of this._def.checks) r.kind === "max" && (e === null || r.value < e) && (e = r.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, r = null; for (const s of this._def.checks) { if (s.kind === "finite" || s.kind === "int" || s.kind === "multipleOf") return !0; s.kind === "min" ? (r === null || s.value > r) && (r = s.value) : s.kind === "max" && (e === null || s.value < e) && (e = s.value); } return Number.isFinite(r) && Number.isFinite(e); } } ZodNumber.create = (t) => new ZodNumber({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber, coerce: t?.coerce || !1, ...processCreateParams(t) }); 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 a = this._getOrReturnCtx(e); return addIssueToContext(a, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.bigint, received: a.parsedType }), INVALID; } let s; const n = new ParseStatus(); for (const a of this._def.checks) a.kind === "min" ? (a.inclusive ? e.data < a.value : e.data <= a.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_small, type: "bigint", minimum: a.value, inclusive: a.inclusive, message: a.message }), n.dirty()) : a.kind === "max" ? (a.inclusive ? e.data > a.value : e.data >= a.value) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.too_big, type: "bigint", maximum: a.value, inclusive: a.inclusive, message: a.message }), n.dirty()) : a.kind === "multipleOf" ? e.data % a.value !== BigInt(0) && (s = this._getOrReturnCtx(e, s), addIssueToContext(s, { code: ZodIssueCode.not_multiple_of, multipleOf: a.value, message: a.message }), n.dirty()) : util.assertNever(a); return { status: n.value, value: e.data }; } gte(e, r) { return this.setLimit("min", e, !0, errorUtil.toString(r)); } gt(e, r) { return this.setLimit("min", e, !1, errorUtil.toString(r)); } lte(e, r) { return this.setLimit("max", e, !0, errorUtil.toString(r)); } lt(e, r) { return this.setLimit("max", e, !1, errorUtil.toString(r)); } setLimit(e, r, s, n) { return new ZodBigInt({ ...this._def, checks: [ ...this._def.checks, { kind: e, value: r, 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, r) { return this._addCheck({ kind: "multipleOf", value: e, message: errorUtil.toString(r) }); } get minValue() { let e = null; for (const r of this._def.checks) r.kind === "min" && (e === null || r.value > e) && (e = r.value); return e; } get maxValue() { let e = null; for (const r of this._def.checks) r.kind === "max" && (e === null || r.value < e) && (e = r.value); return e; } } ZodBigInt.create = (t) => { var e; return new ZodBigInt({ checks: [], typeName: ZodFirstPartyTypeKind.ZodBigInt, coerce: (e = t?.coerce) !== null && e !== void 0 ? e : !1, ...processCreateParams(t) }); }; 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 = (t) => new ZodBoolean({ typeName: ZodFirstPartyTypeKind.ZodBoolean, coerce: t?.coerce || !1, ...processCreateParams(t) }); class ZodDate extends ZodType { _parse(e) { if (this._def.coerce && (e.data = new Date(e.data)), this._getType(e) !== ZodParsedType.date) { const a = this._getOrReturnCtx(e); return addIssueToContext(a, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.date, received: a.parsedType }), INVALID; } if (isNaN(e.data.getTime())) { const a = this._getOrReturnCtx(e); return addIssueToContext(a, { code: ZodIssueCode.invalid_date }), INVALID; } const s = new ParseStatus(); let n; for (const a of this._def.checks) a.kind === "min" ? e.data.getTime() < a.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_small, message: a.message, inclusive: !0, exact: !1, minimum: a.value, type: "date" }), s.dirty()) : a.kind === "max" ? e.data.getTime() > a.value && (n = this._getOrReturnCtx(e, n), addIssueToContext(n, { code: ZodIssueCode.too_big, message: a.message, inclusive: !0, exact: !1, maximum: a.value, type: "date" }), s.dirty()) : util.assertNever(a); return { status: s.value, value: new Date(e.data.getTime()) }; } _addCheck(e) { return new ZodDate({ ...this._def, checks: [...this._def.checks, e] }); } min(e, r) { return this._addCheck({ kind: "min", value: e.getTime(), message: errorUtil.toString(r) }); } max(e, r) { return this._addCheck({ kind: "max", value: e.getTime(), message: errorUtil.toString(r) }); } get minDate() { let e = null; for (const r of this._def.checks) r.kind === "min" && (e === null || r.value > e) && (e = r.value); return e != null ? new Date(e) : null; } get maxDate() { let e = null; for (const r of this._def.checks) r.kind === "max" && (e === null || r.value < e) && (e = r.value); return e != null ? new Date(e) : null; } } ZodDate.create = (t) => new ZodDate({ checks: [], coerce: t?.coerce || !1, typeName: ZodFirstPartyTypeKind.ZodDate, ...processCreateParams(t) }); 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 = (t) => new ZodSymbol({ typeName: ZodFirstPartyTypeKind.ZodSymbol, ...processCreateParams(t) }); 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 = (t) => new ZodUndefined({ typeName: ZodFirstPartyTypeKind.ZodUndefined, ...processCreateParams(t) }); 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 = (t) => new ZodNull({ typeName: ZodFirstPartyTypeKind.ZodNull, ...processCreateParams(t) }); class ZodAny extends ZodType { constructor() { super(...arguments), this._any = !0; } _parse(e) { return OK(e.data); } } ZodAny.create = (t) => new ZodAny({ typeName: ZodFirstPartyTypeKind.ZodAny, ...processCreateParams(t) }); class ZodUnknown extends ZodType { constructor() { super(...arguments), this._unknown = !0; } _parse(e) { return OK(e.data); } } ZodUnknown.create = (t) => new ZodUnknown({ typeName: ZodFirstPartyTypeKind.ZodUnknown, ...processCreateParams(t) }); class ZodNever extends ZodType { _parse(e) { const r = this._getOrReturnCtx(e); return addIssueToContext(r, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.never, received: r.parsedType }), INVALID; } } ZodNever.create = (t) => new ZodNever({ typeName: ZodFirstPartyTypeKind.ZodNever, ...processCreateParams(t) }); 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 = (t) => new ZodVoid({ typeName: ZodFirstPartyTypeKind.ZodVoid, ...processCreateParams(t) }); class ZodArray extends ZodType { _parse(e) { const { ctx: r, status: s } = this._processInputParams(e), n = this._def; if (r.parsedType !== ZodParsedType.array) return addIssueToContext(r, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.array, received: r.parsedType }), INVALID; if (n.exactLength !== null) { const o = r.data.length > n.exactLength.value, c = r.data.length < n.exactLength.value; (o || c) && (addIssueToContext(r, { code: o ? ZodIssueCode.too_big : ZodIssueCode.too_small, minimum: c ? n.exactLength.value : void 0, maximum: o ? n.exactLength.value : void 0, type: "array", inclusive: !0, exact: !0, message: n.exactLength.message }), s.dirty()); } if (n.minLength !== null && r.data.length < n.minLength.value && (addIssueToContext(r, { code: ZodIssueCode.too_small, minimum: n.minLength.value, type: "array", inclusive: !0, exact: !1, message: n.minLength.message }), s.dirty()), n.maxLength !== null && r.data.length > n.maxLength.value && (addIssueToContext(r, { code: ZodIssueCode.too_big, maximum: n.maxLength.value, type: "array", inclusive: !0, exact: !1, message: n.maxLength.message }), s.dirty()), r.common.async) return Promise.all([...r.data].map((o, c) => n.type._parseAsync(new ParseInputLazyPath(r, o, r.path, c)))).then((o) => ParseStatus.mergeArray(s, o)); const a = [...r.data].map((o, c) => n.type._parseSync(new ParseInputLazyPath(r, o, r.path, c))); return ParseStatus.mergeArray(s, a); } get element() { return this._def.type; } min(e, r) { return new ZodArray({ ...this._def, minLength: { value: e, message: errorUtil.toString(r) } }); } max(e, r) { return new ZodArray({ ...this._def, maxLength: { value: e, message: errorUtil.toString(r) } }); } length(e, r) { return new ZodArray({ ...this._def, exactLength: { value: e, message: errorUtil.toString(r) } }); } nonempty(e) { return this.min(1, e); } } ZodArray.create = (t, e) => new ZodArray({ type: t, minLength: null, maxLength: null, exactLength: null, typeName: ZodFirstPartyTypeKind.ZodArray, ...processCreateParams(e) }); function deepPartialify(t) { if (t instanceof ZodObject) { const e = {}; for (const r in t.shape) { const s = t.shape[r]; e[r] = ZodOptional.create(deepPartialify(s)); } return new ZodObject({ ...t._def, shape: () => e }); } else return t instanceof ZodArray ? new ZodArray({ ...t._def, type: deepPartialify(t.element) }) : t instanceof ZodOptional ? ZodOptional.create(deepPartialify(t.unwrap())) : t instanceof ZodNullable ? ZodNullable.create(deepPartialify(t.unwrap())) : t instanceof ZodTuple ? ZodTuple.create(t.items.map((e) => deepPartialify(e))) : t; } 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(), r = util.objectKeys(e); return this._cached = { shape: e, keys: r }; } _parse(e) { if (this._getType(e) !== ZodParsedType.object) { const Q = this._getOrReturnCtx(e); return addIssueToContext(Q, { code: ZodIssueCode.invalid_type, expected: ZodParsedType.object, received: Q.parsedType }), INVALID; } const { status: s, ctx: n } = this._processInputParams(e), { shape: a, keys: o } = this._getCached(), c = []; if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) for (const Q in n.data) o.includes(Q) || c.push(Q); const l = []; for (const Q of o) { const B = a[Q], g = n.data[Q]; l.push({ key: { status: "valid", value: Q }, value: B._parse(new ParseInputLazyPath(n, g, n.path, Q)), alwaysSet: Q in n.data }); } if (this._def.catchall instanceof ZodNever) { const Q = this._def.unknownKeys; if (Q === "passthrough") for (const B of c) l.push({ key: { status: "valid", value: B }, value: { status: "valid", value: n.data[B] } }); else if (Q === "strict") c.length > 0 && (addIssueToContext(n, { code: ZodIssueCode.unrecognized_keys, keys: c }), s.dirty()); else if (Q !== "strip") throw new Error("Internal ZodObject error: invalid unknownKeys value."); } else { const Q = this._def.catchall; for (const B of c) { const g = n.data[B]; l.push({ key: { status: "valid", value: B }, value: Q._parse( new ParseInputLazyPath(n, g, n.path, B) //, ctx.child(key), value, getParsedType(value) ), alwaysSet: B in n.data }); } } return n.common.async ? Promise.resolve().then(async () => { const Q = []; for (const B of l) { const g = await B.key; Q.push({ key: g, value: await B.value, alwaysSet: B.alwaysSet }); } return Q; }).then((Q) => ParseStatus.mergeObjectSync(s, Q)) : 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: (r, s) => { var n, a, o, c; const l = (o = (a = (n = this._def).errorMap) === null || a === void 0 ? void 0 : a.call(n, r, s).message) !== null && o !== void 0 ? o : s.defaultError; return r.code === "unrecognized_keys" ? { message: (c = errorUtil.errToObj(e).message) !== null && c !== void 0 ? c : 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(),