import type * as errors from "./errors.js"; import type * as schemas from "./schemas.js"; import type { Class } from "./util.js"; ////////////////////////////// CONSTRUCTORS /////////////////////////////////////// type ZodTrait = { _zod: { def: any; [k: string]: any } }; export interface $constructor { new (def: D): T; init(inst: T, def: D): asserts inst is T; } export /*@__NO_SIDE_EFFECTS__*/ function $constructor( name: string, initializer: (inst: T, def: D) => void, params?: { Parent?: typeof Class } ): $constructor { function init(inst: T, def: D) { Object.defineProperty(inst, "_zod", { value: inst._zod ?? {}, enumerable: false, }); // inst._zod ??= {} as any;a inst._zod.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 as any)[k].bind(inst) }); } inst._zod.constr = _; inst._zod.def = def; } function construct(instance: any, def: D) { init(instance, def); instance._zod.deferred ??= []; for (const fn of instance._zod.deferred) { fn(); } return instance; } const Parent = params?.Parent ?? Object; class Definition extends Parent {} function _(this: any, def: D) { if (params?.Parent) { return construct(new Definition(), def); } return construct(this, def); } Object.defineProperty(_, "init", { value: init }); Object.defineProperty(_, Symbol.hasInstance, { value: (inst: any) => { if (params?.Parent && inst instanceof params.Parent) return true; return inst?._zod?.traits?.has(name); }, }); Object.defineProperty(_, "name", { value: name }); return _ as any; } ////////////////////////////// UTILITIES /////////////////////////////////////// export const $brand: unique symbol = Symbol("zod_brand"); export type $brand = { [$brand]: { [k in T]: true }; }; export type $ZodBranded = T & Record<"_zod", Record<"~output", output & $brand>>; export class $ZodAsyncError extends Error { constructor() { super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`); } } //////////////////////////// TYPE HELPERS /////////////////////////////////// export type input = T["_zod"] extends { "~input": any } ? T["_zod"]["~input"] : T["_zod"]["input"]; export type output = T["_zod"] extends { "~output": any } ? T["_zod"]["~output"] : T["_zod"]["output"]; export type { output as infer }; ////////////////////////////// CONFIG /////////////////////////////////////// export interface $ZodConfig { /** Custom error map. Overrides `config().localeError`. */ customError?: errors.$ZodErrorMap | undefined; /** Localized error map. Lowest priority. */ localeError?: errors.$ZodErrorMap | undefined; /** Disable JIT schema compilation. Useful in environments that disallow `eval`. */ jitless?: boolean | undefined; } export const globalConfig: $ZodConfig = {}; export function config(newConfig?: Partial<$ZodConfig>): $ZodConfig { if (newConfig) Object.assign(globalConfig, newConfig); return globalConfig; }