registries.cjs 1.7 KB

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