import type * as core from "./core.js"; import type { $ZodType } from "./schemas.js"; export const $output: unique symbol = Symbol("ZodOutput"); export type $output = typeof $output; export const $input: unique symbol = Symbol("ZodInput"); export type $input = typeof $input; export type $replace = Meta extends $output ? core.output : Meta extends $input ? core.input : Meta extends (infer M)[] ? $replace[] : // handle objecs Meta extends object ? { [K in keyof Meta]: $replace } : Meta; type MetadataType = Record | undefined; export class $ZodRegistry { _meta!: Meta; _schema!: Schema; _map: WeakMap> = new WeakMap(); _idmap: Map = new Map(); add( schema: S, ..._meta: undefined extends Meta ? [$replace?] : [$replace] ): this { const meta: any = _meta[0]; this._map.set(schema, meta!); if (meta && typeof meta === "object" && "id" in meta) { if (this._idmap.has(meta.id!)) { throw new Error(`ID ${meta.id} already exists in the registry`); } this._idmap.set(meta.id!, schema); } return this as any; } remove(schema: Schema): this { this._map.delete(schema); return this; } get(schema: S): $replace | undefined { // return this._map.get(schema) as any; // inherit metadata const p = schema._zod.parent as Schema; if (p) { const pm: any = { ...(this.get(p) ?? {}) }; delete pm.id; // do not inherit id return { ...pm, ...this._map.get(schema) } as any; } return this._map.get(schema) as any; } has(schema: Schema): boolean { return this._map.has(schema); } } export interface JSONSchemaMeta { id?: string | undefined; title?: string | undefined; description?: string | undefined; examples?: $output[] | undefined; [k: string]: unknown; } // export class $ZodJSONSchemaRegistry< // Meta extends JSONSchemaMeta = JSONSchemaMeta, // Schema extends $ZodType = $ZodType, // > extends $ZodRegistry { // toJSONSchema(_schema: Schema): object { // return {}; // } // } export interface GlobalMeta extends JSONSchemaMeta {} // registries export function registry(): $ZodRegistry { return new $ZodRegistry(); } export const globalRegistry: $ZodRegistry = /*@__PURE__*/ registry();