UNPKG

@zod/core

Version:

TypeScript-first schema declaration and validation library with static type inference

52 lines (51 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.globalConfig = exports.$ZodAsyncError = exports.$brand = void 0; exports.$constructor = $constructor; exports.config = config; function $constructor(name, initializer) { class _ { constructor(def) { var _a; const th = this; _.init(th, def); (_a = th._zod).deferred ?? (_a.deferred = []); for (const fn of th._zod.deferred) { fn(); } } static init(inst, def) { var _a; inst._zod ?? (inst._zod = {}); (_a = inst._zod).traits ?? (_a.traits = new Set()); // const seen = inst._zod.traits.has(name); inst._zod.traits.add(name); initializer(inst, def); // support prototype modifications for (const k in _.prototype) { Object.defineProperty(inst, k, { value: _.prototype[k].bind(inst) }); } inst._zod.constr = _; inst._zod.def = def; } static [Symbol.hasInstance](inst) { return inst?._zod?.traits?.has(name); } } Object.defineProperty(_, "name", { value: name }); return _; } ////////////////////////////// UTILITIES /////////////////////////////////////// exports.$brand = Symbol("zod_brand"); class $ZodAsyncError extends Error { constructor() { super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`); } } exports.$ZodAsyncError = $ZodAsyncError; exports.globalConfig = {}; function config(config) { if (config) Object.assign(exports.globalConfig, config); return exports.globalConfig; }