79 lines
2.5 KiB
Plaintext
79 lines
2.5 KiB
Plaintext
var _a;
|
|
/** A special constant with type `never` */
|
|
export const NEVER = /*@__PURE__*/ Object.freeze({
|
|
status: "aborted",
|
|
});
|
|
export /*@__NO_SIDE_EFFECTS__*/ function $constructor(name, initializer, params) {
|
|
function init(inst, def) {
|
|
if (!inst._zod) {
|
|
Object.defineProperty(inst, "_zod", {
|
|
value: {
|
|
def,
|
|
constr: _,
|
|
traits: new Set(),
|
|
},
|
|
enumerable: false,
|
|
});
|
|
}
|
|
if (inst._zod.traits.has(name)) {
|
|
return;
|
|
}
|
|
inst._zod.traits.add(name);
|
|
initializer(inst, def);
|
|
// support prototype modifications
|
|
const proto = _.prototype;
|
|
const keys = Object.keys(proto);
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const k = keys[i];
|
|
if (!(k in inst)) {
|
|
inst[k] = proto[k].bind(inst);
|
|
}
|
|
}
|
|
}
|
|
// doesn't work if Parent has a constructor with arguments
|
|
const Parent = params?.Parent ?? Object;
|
|
class Definition extends Parent {
|
|
}
|
|
Object.defineProperty(Definition, "name", { value: name });
|
|
function _(def) {
|
|
var _a;
|
|
const inst = params?.Parent ? new Definition() : this;
|
|
init(inst, def);
|
|
(_a = inst._zod).deferred ?? (_a.deferred = []);
|
|
for (const fn of inst._zod.deferred) {
|
|
fn();
|
|
}
|
|
return inst;
|
|
}
|
|
Object.defineProperty(_, "init", { value: init });
|
|
Object.defineProperty(_, Symbol.hasInstance, {
|
|
value: (inst) => {
|
|
if (params?.Parent && inst instanceof params.Parent)
|
|
return true;
|
|
return inst?._zod?.traits?.has(name);
|
|
},
|
|
});
|
|
Object.defineProperty(_, "name", { value: name });
|
|
return _;
|
|
}
|
|
////////////////////////////// UTILITIES ///////////////////////////////////////
|
|
export const $brand = Symbol("zod_brand");
|
|
export class $ZodAsyncError extends Error {
|
|
constructor() {
|
|
super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`);
|
|
}
|
|
}
|
|
export class $ZodEncodeError extends Error {
|
|
constructor(name) {
|
|
super(`Encountered unidirectional transform during encode: ${name}`);
|
|
this.name = "ZodEncodeError";
|
|
}
|
|
}
|
|
(_a = globalThis).__zod_globalConfig ?? (_a.__zod_globalConfig = {});
|
|
export const globalConfig = globalThis.__zod_globalConfig;
|
|
export function config(newConfig) {
|
|
if (newConfig)
|
|
Object.assign(globalConfig, newConfig);
|
|
return globalConfig;
|
|
}
|