registries.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var _a;
  2. export const $output = Symbol("ZodOutput");
  3. export const $input = Symbol("ZodInput");
  4. export class $ZodRegistry {
  5. constructor() {
  6. this._map = new WeakMap();
  7. this._idmap = new Map();
  8. }
  9. add(schema, ..._meta) {
  10. const meta = _meta[0];
  11. this._map.set(schema, meta);
  12. if (meta && typeof meta === "object" && "id" in meta) {
  13. this._idmap.set(meta.id, schema);
  14. }
  15. return this;
  16. }
  17. clear() {
  18. this._map = new WeakMap();
  19. this._idmap = new Map();
  20. return this;
  21. }
  22. remove(schema) {
  23. const meta = this._map.get(schema);
  24. if (meta && typeof meta === "object" && "id" in meta) {
  25. this._idmap.delete(meta.id);
  26. }
  27. this._map.delete(schema);
  28. return this;
  29. }
  30. get(schema) {
  31. // return this._map.get(schema) as any;
  32. // inherit metadata
  33. const p = schema._zod.parent;
  34. if (p) {
  35. const pm = { ...(this.get(p) ?? {}) };
  36. delete pm.id; // do not inherit id
  37. const f = { ...pm, ...this._map.get(schema) };
  38. return Object.keys(f).length ? f : undefined;
  39. }
  40. return this._map.get(schema);
  41. }
  42. has(schema) {
  43. return this._map.has(schema);
  44. }
  45. }
  46. // registries
  47. export function registry() {
  48. return new $ZodRegistry();
  49. }
  50. (_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
  51. export const globalRegistry = globalThis.__zod_globalRegistry;