|
1 | | -import * as R from "ramda"; |
2 | | -import { globalRegistry, z } from "zod"; |
3 | | -import { $EZBrandCheck } from "./brand-check"; |
4 | | -import { name } from "./package.json"; |
5 | | -import { Intact, Remap } from "./remap"; |
| 1 | +import type { z } from "zod"; |
| 2 | +import type { Intact, Remap } from "./remap"; |
6 | 3 |
|
7 | 4 | declare module "zod/v4/core" { |
8 | 5 | interface GlobalMeta { |
@@ -56,81 +53,3 @@ declare module "zod" { |
56 | 53 | ): z.ZodPipe<z.ZodPipe<this, z.ZodTransform>, z.ZodObject<U>>; // internal type simplified |
57 | 54 | } |
58 | 55 | } |
59 | | - |
60 | | -const exampleSetter = function (this: z.ZodType, value: z.output<typeof this>) { |
61 | | - const examples = globalRegistry.get(this)?.examples?.slice() || []; |
62 | | - examples.push(value); |
63 | | - return this.meta({ examples }); |
64 | | -}; |
65 | | - |
66 | | -const deprecationSetter = function (this: z.ZodType) { |
67 | | - return this.meta({ deprecated: true }); |
68 | | -}; |
69 | | - |
70 | | -const labelSetter = function (this: z.ZodDefault, defaultLabel: string) { |
71 | | - return this.meta({ default: defaultLabel }); |
72 | | -}; |
73 | | - |
74 | | -const brandSetter = function ( |
75 | | - this: z.ZodType, |
76 | | - brand?: string | number | symbol, |
77 | | -) { |
78 | | - return this.check(new $EZBrandCheck({ brand, check: "$EZBrandCheck" })); |
79 | | -}; |
80 | | - |
81 | | -type _Mapper = <T extends Record<string, unknown>>( |
82 | | - subject: T, |
83 | | -) => { [P in string | keyof T]: T[keyof T] }; |
84 | | - |
85 | | -const objectMapper = function ( |
86 | | - this: z.ZodObject, |
87 | | - tool: Record<string, string> | _Mapper, |
88 | | -) { |
89 | | - const transformer = |
90 | | - typeof tool === "function" ? tool : R.renameKeys(R.reject(R.isNil, tool)); // rejecting undefined |
91 | | - const nextShape = transformer( |
92 | | - R.map(R.invoker(0, "clone"), this._zod.def.shape), // immutable, changed from R.clone due to failure |
93 | | - ); |
94 | | - const hasPassThrough = this._zod.def.catchall instanceof z.ZodUnknown; |
95 | | - const output = (hasPassThrough ? z.looseObject : z.object)(nextShape); // proxies unknown keys when set to "passthrough" |
96 | | - return this.transform(transformer).pipe(output); |
97 | | -}; |
98 | | - |
99 | | -const pluginFlag = Symbol.for(name); |
100 | | - |
101 | | -if (!(pluginFlag in globalThis)) { |
102 | | - (globalThis as Record<symbol, unknown>)[pluginFlag] = true; |
103 | | - for (const entry of Object.keys(z)) { |
104 | | - if (!entry.startsWith("Zod")) continue; |
105 | | - if (/(Success|Error|Function)$/.test(entry)) continue; |
106 | | - const Cls = z[entry as keyof typeof z]; |
107 | | - if (typeof Cls !== "function") continue; |
108 | | - Object.defineProperties(Cls.prototype, { |
109 | | - ["example" satisfies keyof z.ZodType]: { |
110 | | - value: exampleSetter, |
111 | | - writable: false, |
112 | | - }, |
113 | | - ["deprecated" satisfies keyof z.ZodType]: { |
114 | | - value: deprecationSetter, |
115 | | - writable: false, |
116 | | - }, |
117 | | - ["brand" satisfies keyof z.ZodType]: { |
118 | | - set() {}, // this is required to override the existing method |
119 | | - get() { |
120 | | - return brandSetter.bind(this) as z.ZodType["brand"]; |
121 | | - }, |
122 | | - }, |
123 | | - }); |
124 | | - } |
125 | | - |
126 | | - Object.defineProperty( |
127 | | - z.ZodDefault.prototype, |
128 | | - "label" satisfies keyof z.ZodDefault, |
129 | | - { value: labelSetter, writable: false }, |
130 | | - ); |
131 | | - Object.defineProperty( |
132 | | - z.ZodObject.prototype, |
133 | | - "remap" satisfies keyof z.ZodObject, |
134 | | - { value: objectMapper, writable: false }, |
135 | | - ); |
136 | | -} |
0 commit comments