@layerzerolabs/zod-utils 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +26 -0
- package/.turbo/turbo-lint.log +4 -0
- package/dist/CAXEKYBD.cjs +35 -0
- package/dist/CAXEKYBD.cjs.map +1 -0
- package/dist/NIWFGLF4.js +30 -0
- package/dist/NIWFGLF4.js.map +1 -0
- package/dist/index.cjs +135 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +121 -0
- package/dist/index.js.map +1 -0
- package/dist/schema.cjs +20 -0
- package/dist/schema.cjs.map +1 -0
- package/dist/schema.d.ts +10 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +3 -0
- package/dist/schema.js.map +1 -0
- package/package.json +35 -0
- package/src/index.ts +146 -0
- package/src/schema.ts +55 -0
- package/tsconfig.json +28 -0
- package/tsup.config.ts +8 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
> @layerzerolabs/zod-utils@0.0.0 build /home/runner/work/monorepo-internal/monorepo-internal/packages/zod-utils
|
|
3
|
+
> tsup
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts, src/schema.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.4.0
|
|
8
|
+
[34mCLI[39m Using tsup config: /home/runner/work/monorepo-internal/monorepo-internal/packages/zod-utils/tsup.config.ts
|
|
9
|
+
[34mCLI[39m Target: es2022
|
|
10
|
+
[34mCLI[39m Cleaning output folder
|
|
11
|
+
[34mCJS[39m Build start
|
|
12
|
+
[34mESM[39m Build start
|
|
13
|
+
[32mCJS[39m [1mdist/schema.cjs [22m[32m549.00 B[39m
|
|
14
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m4.41 KB[39m
|
|
15
|
+
[32mCJS[39m [1mdist/CAXEKYBD.cjs [22m[32m1.07 KB[39m
|
|
16
|
+
[32mCJS[39m [1mdist/schema.cjs.map [22m[32m71.00 B[39m
|
|
17
|
+
[32mCJS[39m [1mdist/index.cjs.map [22m[32m10.70 KB[39m
|
|
18
|
+
[32mCJS[39m [1mdist/CAXEKYBD.cjs.map [22m[32m3.00 KB[39m
|
|
19
|
+
[32mCJS[39m ⚡️ Build success in 196ms
|
|
20
|
+
[32mESM[39m [1mdist/schema.js [22m[32m155.00 B[39m
|
|
21
|
+
[32mESM[39m [1mdist/index.js [22m[32m3.88 KB[39m
|
|
22
|
+
[32mESM[39m [1mdist/NIWFGLF4.js [22m[32m985.00 B[39m
|
|
23
|
+
[32mESM[39m [1mdist/schema.js.map [22m[32m70.00 B[39m
|
|
24
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m10.70 KB[39m
|
|
25
|
+
[32mESM[39m [1mdist/NIWFGLF4.js.map [22m[32m2.99 KB[39m
|
|
26
|
+
[32mESM[39m ⚡️ Build success in 197ms
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var v4 = require('zod/v4');
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var functionSchemaTag = Symbol("__FUNCTION_SCHEMA_TAG");
|
|
8
|
+
var functionSchema = /* @__PURE__ */ __name(({ input, output }) => {
|
|
9
|
+
const obj = v4.z.object({
|
|
10
|
+
input,
|
|
11
|
+
output
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(obj, functionSchemaTag, {
|
|
14
|
+
value: true,
|
|
15
|
+
writable: false
|
|
16
|
+
});
|
|
17
|
+
obj.parse = (i) => {
|
|
18
|
+
if (typeof i !== "function") {
|
|
19
|
+
throw new Error(`${i} is not a function`);
|
|
20
|
+
}
|
|
21
|
+
return i;
|
|
22
|
+
};
|
|
23
|
+
return obj;
|
|
24
|
+
}, "functionSchema");
|
|
25
|
+
var schemaIsFunctionSchema = /* @__PURE__ */ __name((schema) => {
|
|
26
|
+
return Object.hasOwn(schema, functionSchemaTag);
|
|
27
|
+
}, "schemaIsFunctionSchema");
|
|
28
|
+
var customSchema = /* @__PURE__ */ __name((validate) => v4.z.custom(validate), "customSchema");
|
|
29
|
+
|
|
30
|
+
exports.__name = __name;
|
|
31
|
+
exports.customSchema = customSchema;
|
|
32
|
+
exports.functionSchema = functionSchema;
|
|
33
|
+
exports.schemaIsFunctionSchema = schemaIsFunctionSchema;
|
|
34
|
+
//# sourceMappingURL=CAXEKYBD.cjs.map
|
|
35
|
+
//# sourceMappingURL=CAXEKYBD.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema.ts"],"names":["functionSchemaTag","Symbol","functionSchema","input","output","obj","z","object","Object","defineProperty","value","writable","parse","i","Error","schemaIsFunctionSchema","schema","hasOwn","customSchema","validate","custom"],"mappings":";;;;;;AAaA,IAAMA,iBAAAA,GAAoBC,OAAO,uBAAA,CAAA;AAG1B,IAAMC,cAAAA,mBAAiB,MAAA,CAAA,CAAqD,EAC/EC,KAAAA,EACAC,QAAM,KAIT;AACG,EAAA,MAAMC,GAAAA,GAAMC,KAAEC,MAAAA,CAAO;AACjBJ,IAAAA,KAAAA;AACAC,IAAAA;GACJ,CAAA;AACAI,EAAAA,MAAAA,CAAOC,cAAAA,CAAeJ,KAAKL,iBAAAA,EAAmB;IAAEU,KAAAA,EAAO,IAAA;IAAMC,QAAAA,EAAU;GAAM,CAAA;AAI7EN,EAAAA,GAAAA,CAAIO,KAAAA,GAAQ,CAACC,CAAAA,KAAAA;AACT,IAAA,IAAI,OAAOA,MAAM,UAAA,EAAY;AACzB,MAAA,MAAM,IAAIC,KAAAA,CAAM,CAAA,EAAGD,CAAAA,CAAAA,kBAAAA,CAAqB,CAAA;AAC5C,IAAA;AAGA,IAAA,OAAOA,CAAAA;AACX,EAAA,CAAA;AACA,EAAA,OAAOR,GAAAA;AAMX,CAAA,EA7B8B,gBAAA;AA+BvB,IAAMU,sBAAAA,2BACTC,MAAAA,KAAAA;AAEA,EAAA,OAAOR,MAAAA,CAAOS,MAAAA,CAAOD,MAAAA,EAAQhB,iBAAAA,CAAAA;AACjC,CAAA,EAJsC,wBAAA;AAM/B,IAAMkB,+BAAe,MAAA,CAAA,CAAIC,QAAAA,KAC5Bb,IAAAA,CAAEc,MAAAA,CAAUD,QAAAA,CAAAA,EADY,cAAA","file":"CAXEKYBD.cjs","sourcesContent":["import { z } from 'zod/v4';\n\ntype IsPromise<T> =\n T extends z.ZodOptional<infer I>\n ? IsPromise<I>\n : T extends z.ZodDefault<infer I>\n ? IsPromise<I>\n : T extends z.ZodNullable<infer I>\n ? IsPromise<I>\n : T extends z.ZodPromise<any>\n ? true\n : false;\n\nconst functionSchemaTag = Symbol('__FUNCTION_SCHEMA_TAG');\n\n//this essentially replicates the functionality of z.function in zod v3\nexport const functionSchema = <Input extends z.ZodTuple, Output extends z.ZodType>({\n input,\n output,\n}: {\n input: Input;\n output: Output;\n}) => {\n const obj = z.object({\n input,\n output,\n });\n Object.defineProperty(obj, functionSchemaTag, { value: true, writable: false });\n //we don't expect the function to actually be an object with an input field and an output field\n //so we override the parse function. we can't actually parse functions since input and output\n //types are lost at runtime, but we can check the runtime type of the object\n obj.parse = (i) => {\n if (typeof i !== 'function') {\n throw new Error(`${i} is not a function`);\n }\n //the return type here just has to match the inference of z.object,\n //the actual inference of the schema will be the z.ZodType custom\n return i as any;\n };\n return obj as unknown as z.ZodType<\n (\n ...args: z.infer<Input>\n //z.ZodPromise<T> infers as T, not Promise<T>\n ) => IsPromise<Output> extends true ? Promise<z.infer<Output>> : z.infer<Output>\n >;\n};\n\nexport const schemaIsFunctionSchema = (\n schema: z.ZodType,\n): schema is ReturnType<typeof functionSchema> => {\n return Object.hasOwn(schema, functionSchemaTag);\n};\n\nexport const customSchema = <T>(validate?: (data: any) => T) =>\n z.custom<T>(validate) as z.ZodType<T>;\n"]}
|
package/dist/NIWFGLF4.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
var functionSchemaTag = Symbol("__FUNCTION_SCHEMA_TAG");
|
|
6
|
+
var functionSchema = /* @__PURE__ */ __name(({ input, output }) => {
|
|
7
|
+
const obj = z.object({
|
|
8
|
+
input,
|
|
9
|
+
output
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(obj, functionSchemaTag, {
|
|
12
|
+
value: true,
|
|
13
|
+
writable: false
|
|
14
|
+
});
|
|
15
|
+
obj.parse = (i) => {
|
|
16
|
+
if (typeof i !== "function") {
|
|
17
|
+
throw new Error(`${i} is not a function`);
|
|
18
|
+
}
|
|
19
|
+
return i;
|
|
20
|
+
};
|
|
21
|
+
return obj;
|
|
22
|
+
}, "functionSchema");
|
|
23
|
+
var schemaIsFunctionSchema = /* @__PURE__ */ __name((schema) => {
|
|
24
|
+
return Object.hasOwn(schema, functionSchemaTag);
|
|
25
|
+
}, "schemaIsFunctionSchema");
|
|
26
|
+
var customSchema = /* @__PURE__ */ __name((validate) => z.custom(validate), "customSchema");
|
|
27
|
+
|
|
28
|
+
export { __name, customSchema, functionSchema, schemaIsFunctionSchema };
|
|
29
|
+
//# sourceMappingURL=NIWFGLF4.js.map
|
|
30
|
+
//# sourceMappingURL=NIWFGLF4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema.ts"],"names":["functionSchemaTag","Symbol","functionSchema","input","output","obj","z","object","Object","defineProperty","value","writable","parse","i","Error","schemaIsFunctionSchema","schema","hasOwn","customSchema","validate","custom"],"mappings":";;;;AAaA,IAAMA,iBAAAA,GAAoBC,OAAO,uBAAA,CAAA;AAG1B,IAAMC,cAAAA,mBAAiB,MAAA,CAAA,CAAqD,EAC/EC,KAAAA,EACAC,QAAM,KAIT;AACG,EAAA,MAAMC,GAAAA,GAAMC,EAAEC,MAAAA,CAAO;AACjBJ,IAAAA,KAAAA;AACAC,IAAAA;GACJ,CAAA;AACAI,EAAAA,MAAAA,CAAOC,cAAAA,CAAeJ,KAAKL,iBAAAA,EAAmB;IAAEU,KAAAA,EAAO,IAAA;IAAMC,QAAAA,EAAU;GAAM,CAAA;AAI7EN,EAAAA,GAAAA,CAAIO,KAAAA,GAAQ,CAACC,CAAAA,KAAAA;AACT,IAAA,IAAI,OAAOA,MAAM,UAAA,EAAY;AACzB,MAAA,MAAM,IAAIC,KAAAA,CAAM,CAAA,EAAGD,CAAAA,CAAAA,kBAAAA,CAAqB,CAAA;AAC5C,IAAA;AAGA,IAAA,OAAOA,CAAAA;AACX,EAAA,CAAA;AACA,EAAA,OAAOR,GAAAA;AAMX,CAAA,EA7B8B,gBAAA;AA+BvB,IAAMU,sBAAAA,2BACTC,MAAAA,KAAAA;AAEA,EAAA,OAAOR,MAAAA,CAAOS,MAAAA,CAAOD,MAAAA,EAAQhB,iBAAAA,CAAAA;AACjC,CAAA,EAJsC,wBAAA;AAM/B,IAAMkB,+BAAe,MAAA,CAAA,CAAIC,QAAAA,KAC5Bb,CAAAA,CAAEc,MAAAA,CAAUD,QAAAA,CAAAA,EADY,cAAA","file":"NIWFGLF4.js","sourcesContent":["import { z } from 'zod/v4';\n\ntype IsPromise<T> =\n T extends z.ZodOptional<infer I>\n ? IsPromise<I>\n : T extends z.ZodDefault<infer I>\n ? IsPromise<I>\n : T extends z.ZodNullable<infer I>\n ? IsPromise<I>\n : T extends z.ZodPromise<any>\n ? true\n : false;\n\nconst functionSchemaTag = Symbol('__FUNCTION_SCHEMA_TAG');\n\n//this essentially replicates the functionality of z.function in zod v3\nexport const functionSchema = <Input extends z.ZodTuple, Output extends z.ZodType>({\n input,\n output,\n}: {\n input: Input;\n output: Output;\n}) => {\n const obj = z.object({\n input,\n output,\n });\n Object.defineProperty(obj, functionSchemaTag, { value: true, writable: false });\n //we don't expect the function to actually be an object with an input field and an output field\n //so we override the parse function. we can't actually parse functions since input and output\n //types are lost at runtime, but we can check the runtime type of the object\n obj.parse = (i) => {\n if (typeof i !== 'function') {\n throw new Error(`${i} is not a function`);\n }\n //the return type here just has to match the inference of z.object,\n //the actual inference of the schema will be the z.ZodType custom\n return i as any;\n };\n return obj as unknown as z.ZodType<\n (\n ...args: z.infer<Input>\n //z.ZodPromise<T> infers as T, not Promise<T>\n ) => IsPromise<Output> extends true ? Promise<z.infer<Output>> : z.infer<Output>\n >;\n};\n\nexport const schemaIsFunctionSchema = (\n schema: z.ZodType,\n): schema is ReturnType<typeof functionSchema> => {\n return Object.hasOwn(schema, functionSchemaTag);\n};\n\nexport const customSchema = <T>(validate?: (data: any) => T) =>\n z.custom<T>(validate) as z.ZodType<T>;\n"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var CAXEKYBD_cjs = require('./CAXEKYBD.cjs');
|
|
4
|
+
var v4 = require('zod/v4');
|
|
5
|
+
|
|
6
|
+
var RESOLVING = Symbol("mapOnSchema/resolving");
|
|
7
|
+
function mapOnSchema(schema, fn) {
|
|
8
|
+
const results = /* @__PURE__ */ new Map();
|
|
9
|
+
function mapElement(s) {
|
|
10
|
+
const value = results.get(s);
|
|
11
|
+
if (value === RESOLVING) {
|
|
12
|
+
throw new Error("Recursive schema access detected");
|
|
13
|
+
} else if (value !== void 0) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
results.set(s, RESOLVING);
|
|
17
|
+
const result = mapOnSchema(s, fn);
|
|
18
|
+
results.set(s, result);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
CAXEKYBD_cjs.__name(mapElement, "mapElement");
|
|
22
|
+
function mapInner() {
|
|
23
|
+
if (schema instanceof v4.z.ZodObject) {
|
|
24
|
+
const newShape = {};
|
|
25
|
+
for (const [key, value] of Object.entries(schema.shape)) {
|
|
26
|
+
newShape[key] = mapElement(value);
|
|
27
|
+
}
|
|
28
|
+
return new v4.z.ZodObject({
|
|
29
|
+
...schema.def,
|
|
30
|
+
shape: newShape
|
|
31
|
+
});
|
|
32
|
+
} else if (schema instanceof v4.z.ZodArray) {
|
|
33
|
+
return new v4.z.ZodArray({
|
|
34
|
+
...schema.def,
|
|
35
|
+
element: mapElement(schema.def.element)
|
|
36
|
+
});
|
|
37
|
+
} else if (schema instanceof v4.z.ZodMap) {
|
|
38
|
+
return new v4.z.ZodMap({
|
|
39
|
+
...schema.def,
|
|
40
|
+
keyType: mapElement(schema.def.keyType),
|
|
41
|
+
valueType: mapElement(schema.def.valueType)
|
|
42
|
+
});
|
|
43
|
+
} else if (schema instanceof v4.z.ZodSet) {
|
|
44
|
+
return new v4.z.ZodSet({
|
|
45
|
+
...schema.def,
|
|
46
|
+
valueType: mapElement(schema.def.valueType)
|
|
47
|
+
});
|
|
48
|
+
} else if (schema instanceof v4.z.ZodOptional) {
|
|
49
|
+
return new v4.z.ZodOptional({
|
|
50
|
+
...schema.def,
|
|
51
|
+
innerType: mapElement(schema.def.innerType)
|
|
52
|
+
});
|
|
53
|
+
} else if (schema instanceof v4.z.ZodNullable) {
|
|
54
|
+
return new v4.z.ZodNullable({
|
|
55
|
+
...schema.def,
|
|
56
|
+
innerType: mapElement(schema.def.innerType)
|
|
57
|
+
});
|
|
58
|
+
} else if (schema instanceof v4.z.ZodDefault) {
|
|
59
|
+
return new v4.z.ZodDefault({
|
|
60
|
+
...schema.def,
|
|
61
|
+
innerType: mapElement(schema.def.innerType)
|
|
62
|
+
});
|
|
63
|
+
} else if (schema instanceof v4.z.ZodReadonly) {
|
|
64
|
+
return new v4.z.ZodReadonly({
|
|
65
|
+
...schema.def,
|
|
66
|
+
innerType: mapElement(schema.def.innerType)
|
|
67
|
+
});
|
|
68
|
+
} else if (schema instanceof v4.z.ZodLazy) {
|
|
69
|
+
return new v4.z.ZodLazy({
|
|
70
|
+
...schema.def,
|
|
71
|
+
// NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas
|
|
72
|
+
getter: /* @__PURE__ */ CAXEKYBD_cjs.__name(() => mapElement(schema.def.getter()), "getter")
|
|
73
|
+
});
|
|
74
|
+
} else if (schema instanceof v4.z.ZodPromise) {
|
|
75
|
+
return new v4.z.ZodPromise({
|
|
76
|
+
...schema.def,
|
|
77
|
+
innerType: mapElement(schema.def.innerType)
|
|
78
|
+
});
|
|
79
|
+
} else if (schema instanceof v4.z.ZodCatch) {
|
|
80
|
+
return new v4.z.ZodCatch({
|
|
81
|
+
...schema.def,
|
|
82
|
+
innerType: mapElement(schema.def.innerType)
|
|
83
|
+
});
|
|
84
|
+
} else if (schema instanceof v4.z.ZodTuple) {
|
|
85
|
+
return new v4.z.ZodTuple({
|
|
86
|
+
...schema.def,
|
|
87
|
+
items: schema.def.items.map((item) => mapElement(item)),
|
|
88
|
+
rest: schema.def.rest && mapElement(schema.def.rest)
|
|
89
|
+
});
|
|
90
|
+
} else if (schema instanceof v4.z.ZodUnion) {
|
|
91
|
+
return new v4.z.ZodUnion({
|
|
92
|
+
...schema.def,
|
|
93
|
+
options: schema.def.options.map((option) => mapElement(option))
|
|
94
|
+
});
|
|
95
|
+
} else if (schema instanceof v4.z.ZodIntersection) {
|
|
96
|
+
return new v4.z.ZodIntersection({
|
|
97
|
+
...schema.def,
|
|
98
|
+
right: mapElement(schema.def.right),
|
|
99
|
+
left: mapElement(schema.def.left)
|
|
100
|
+
});
|
|
101
|
+
} else if (schema instanceof v4.z.ZodRecord) {
|
|
102
|
+
return new v4.z.ZodRecord({
|
|
103
|
+
...schema.def,
|
|
104
|
+
keyType: schema.def.keyType,
|
|
105
|
+
valueType: mapElement(schema.def.valueType)
|
|
106
|
+
});
|
|
107
|
+
} else {
|
|
108
|
+
return schema;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
CAXEKYBD_cjs.__name(mapInner, "mapInner");
|
|
112
|
+
return fn(mapInner());
|
|
113
|
+
}
|
|
114
|
+
CAXEKYBD_cjs.__name(mapOnSchema, "mapOnSchema");
|
|
115
|
+
function zodDeepPartial(schema) {
|
|
116
|
+
return mapOnSchema(schema, (s) => s?.optional());
|
|
117
|
+
}
|
|
118
|
+
CAXEKYBD_cjs.__name(zodDeepPartial, "zodDeepPartial");
|
|
119
|
+
|
|
120
|
+
Object.defineProperty(exports, "customSchema", {
|
|
121
|
+
enumerable: true,
|
|
122
|
+
get: function () { return CAXEKYBD_cjs.customSchema; }
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(exports, "functionSchema", {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
get: function () { return CAXEKYBD_cjs.functionSchema; }
|
|
127
|
+
});
|
|
128
|
+
Object.defineProperty(exports, "schemaIsFunctionSchema", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
get: function () { return CAXEKYBD_cjs.schemaIsFunctionSchema; }
|
|
131
|
+
});
|
|
132
|
+
exports.mapOnSchema = mapOnSchema;
|
|
133
|
+
exports.zodDeepPartial = zodDeepPartial;
|
|
134
|
+
//# sourceMappingURL=index.cjs.map
|
|
135
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["RESOLVING","Symbol","mapOnSchema","schema","fn","results","Map","mapElement","s","value","get","Error","undefined","set","result","mapInner","z","ZodObject","newShape","key","Object","entries","shape","def","ZodArray","element","ZodMap","keyType","valueType","ZodSet","ZodOptional","innerType","ZodNullable","ZodDefault","ZodReadonly","ZodLazy","getter","ZodPromise","ZodCatch","ZodTuple","items","map","item","rest","ZodUnion","options","option","ZodIntersection","right","left","ZodRecord","zodDeepPartial","optional"],"mappings":";;;;;AAoBA,IAAMA,SAAAA,GAAYC,OAAO,uBAAA,CAAA;AAWlB,SAASC,WAAAA,CAAYC,QAAkBC,EAAAA,EAAkC;AAE5E,EAAA,MA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B,GAAAA,CAAIK,SAAS;OAC9C,CAAA;IACJ,CAAA,MAAO;AACH,MAAA,OAAOzB,MAAAA;AACX,IAAA;AACJ,EAAA;AAzFSY,EAAAA,mBAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CAAAA;AA2FT,EAAA,OAAOX,EAAAA,CAAGW,UAAAA,CAAAA;AACd;AA9GgBb,mBAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAgHT,SAASiD,eAAuChD,MAAAA,EAAS;AAC5D,EAAA,OAAOD,YAAYC,MAAAA,EAAQ,CAACK,CAAAA,KAAOA,CAAAA,EAAW4C,UAAAA,CAAAA;AAClD;AAFgBD,mBAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA","file":"index.cjs","sourcesContent":["export * from './schema';\n\n/*\n Copyright 2024, Jaen - https://github.com/jaens\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n http://www.apache.org/licenses/LICENSE-2.0\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n\n https://gist.github.com/jaens/7e15ae1984bb338c86eb5e452dee3010\n*/\n\nimport { z } from 'zod/v4';\nimport type { $ZodType } from 'zod/v4/core';\n\nconst RESOLVING = Symbol('mapOnSchema/resolving');\n\nexport function mapOnSchema<T extends $ZodType, TResult extends $ZodType>(\n schema: T,\n fn: (schema: $ZodType) => TResult,\n): TResult;\n\n/**\n * Applies {@link fn} to each element of the schema recursively, replacing every schema with its return value.\n * The rewriting is applied bottom-up (ie. {@link fn} will get called on \"children\" first).\n */\nexport function mapOnSchema(schema: $ZodType, fn: (schema: $ZodType) => $ZodType): $ZodType {\n // Cache results to support recursive schemas\n const results = new Map<$ZodType, $ZodType | typeof RESOLVING>();\n\n function mapElement(s: $ZodType) {\n const value = results.get(s);\n if (value === RESOLVING) {\n throw new Error('Recursive schema access detected');\n } else if (value !== undefined) {\n return value;\n }\n\n results.set(s, RESOLVING);\n const result = mapOnSchema(s, fn);\n results.set(s, result);\n return result;\n }\n\n function mapInner() {\n if (schema instanceof z.ZodObject) {\n const newShape: Record<string, $ZodType> = {};\n for (const [key, value] of Object.entries(schema.shape)) {\n newShape[key] = mapElement(value as $ZodType);\n }\n\n return new z.ZodObject({\n ...schema.def,\n shape: newShape,\n });\n } else if (schema instanceof z.ZodArray) {\n return new z.ZodArray({\n ...schema.def,\n element: mapElement(schema.def.element),\n });\n } else if (schema instanceof z.ZodMap) {\n return new z.ZodMap({\n ...schema.def,\n keyType: mapElement(schema.def.keyType),\n valueType: mapElement(schema.def.valueType),\n });\n } else if (schema instanceof z.ZodSet) {\n return new z.ZodSet({\n ...schema.def,\n valueType: mapElement(schema.def.valueType),\n });\n } else if (schema instanceof z.ZodOptional) {\n return new z.ZodOptional({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodNullable) {\n return new z.ZodNullable({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodDefault) {\n return new z.ZodDefault({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodReadonly) {\n return new z.ZodReadonly({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodLazy) {\n return new z.ZodLazy({\n ...schema.def,\n // NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas\n getter: () => mapElement(schema.def.getter()),\n });\n } else if (schema instanceof z.ZodPromise) {\n return new z.ZodPromise({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodCatch) {\n return new z.ZodCatch({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodTuple) {\n return new z.ZodTuple({\n ...schema.def,\n items: schema.def.items.map((item: $ZodType) => mapElement(item)),\n rest: schema.def.rest && mapElement(schema.def.rest),\n });\n } else if (schema instanceof z.ZodUnion) {\n return new z.ZodUnion({\n ...schema.def,\n options: schema.def.options.map((option: $ZodType) => mapElement(option)),\n });\n } else if (schema instanceof z.ZodIntersection) {\n return new z.ZodIntersection({\n ...schema.def,\n right: mapElement(schema.def.right),\n left: mapElement(schema.def.left),\n });\n } else if (schema instanceof z.ZodRecord) {\n return new z.ZodRecord({\n ...schema.def,\n keyType: schema.def.keyType as any,\n valueType: mapElement(schema.def.valueType),\n });\n } else {\n return schema;\n }\n }\n\n return fn(mapInner());\n}\n\nexport function zodDeepPartial<T extends z.ZodTypeAny>(schema: T): T {\n return mapOnSchema(schema, (s) => (s as any)?.optional()) as T;\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './schema';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
import type { $ZodType } from 'zod/v4/core';
|
|
4
|
+
export declare function mapOnSchema<T extends $ZodType, TResult extends $ZodType>(schema: T, fn: (schema: $ZodType) => TResult): TResult;
|
|
5
|
+
export declare function zodDeepPartial<T extends z.ZodTypeAny>(schema: T): T;
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AAiBzB,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAC3B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAI5C,wBAAgB,WAAW,CAAC,CAAC,SAAS,QAAQ,EAAE,OAAO,SAAS,QAAQ,EACpE,MAAM,EAAE,CAAC,EACT,EAAE,EAAE,CAAC,MAAM,EAAE,QAAQ,KAAK,OAAO,GAClC,OAAO,CAAC;AAsHX,wBAAgB,cAAc,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAEnE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { __name } from './NIWFGLF4.js';
|
|
2
|
+
export { customSchema, functionSchema, schemaIsFunctionSchema } from './NIWFGLF4.js';
|
|
3
|
+
import { z } from 'zod/v4';
|
|
4
|
+
|
|
5
|
+
var RESOLVING = Symbol("mapOnSchema/resolving");
|
|
6
|
+
function mapOnSchema(schema, fn) {
|
|
7
|
+
const results = /* @__PURE__ */ new Map();
|
|
8
|
+
function mapElement(s) {
|
|
9
|
+
const value = results.get(s);
|
|
10
|
+
if (value === RESOLVING) {
|
|
11
|
+
throw new Error("Recursive schema access detected");
|
|
12
|
+
} else if (value !== void 0) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
results.set(s, RESOLVING);
|
|
16
|
+
const result = mapOnSchema(s, fn);
|
|
17
|
+
results.set(s, result);
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
__name(mapElement, "mapElement");
|
|
21
|
+
function mapInner() {
|
|
22
|
+
if (schema instanceof z.ZodObject) {
|
|
23
|
+
const newShape = {};
|
|
24
|
+
for (const [key, value] of Object.entries(schema.shape)) {
|
|
25
|
+
newShape[key] = mapElement(value);
|
|
26
|
+
}
|
|
27
|
+
return new z.ZodObject({
|
|
28
|
+
...schema.def,
|
|
29
|
+
shape: newShape
|
|
30
|
+
});
|
|
31
|
+
} else if (schema instanceof z.ZodArray) {
|
|
32
|
+
return new z.ZodArray({
|
|
33
|
+
...schema.def,
|
|
34
|
+
element: mapElement(schema.def.element)
|
|
35
|
+
});
|
|
36
|
+
} else if (schema instanceof z.ZodMap) {
|
|
37
|
+
return new z.ZodMap({
|
|
38
|
+
...schema.def,
|
|
39
|
+
keyType: mapElement(schema.def.keyType),
|
|
40
|
+
valueType: mapElement(schema.def.valueType)
|
|
41
|
+
});
|
|
42
|
+
} else if (schema instanceof z.ZodSet) {
|
|
43
|
+
return new z.ZodSet({
|
|
44
|
+
...schema.def,
|
|
45
|
+
valueType: mapElement(schema.def.valueType)
|
|
46
|
+
});
|
|
47
|
+
} else if (schema instanceof z.ZodOptional) {
|
|
48
|
+
return new z.ZodOptional({
|
|
49
|
+
...schema.def,
|
|
50
|
+
innerType: mapElement(schema.def.innerType)
|
|
51
|
+
});
|
|
52
|
+
} else if (schema instanceof z.ZodNullable) {
|
|
53
|
+
return new z.ZodNullable({
|
|
54
|
+
...schema.def,
|
|
55
|
+
innerType: mapElement(schema.def.innerType)
|
|
56
|
+
});
|
|
57
|
+
} else if (schema instanceof z.ZodDefault) {
|
|
58
|
+
return new z.ZodDefault({
|
|
59
|
+
...schema.def,
|
|
60
|
+
innerType: mapElement(schema.def.innerType)
|
|
61
|
+
});
|
|
62
|
+
} else if (schema instanceof z.ZodReadonly) {
|
|
63
|
+
return new z.ZodReadonly({
|
|
64
|
+
...schema.def,
|
|
65
|
+
innerType: mapElement(schema.def.innerType)
|
|
66
|
+
});
|
|
67
|
+
} else if (schema instanceof z.ZodLazy) {
|
|
68
|
+
return new z.ZodLazy({
|
|
69
|
+
...schema.def,
|
|
70
|
+
// NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas
|
|
71
|
+
getter: /* @__PURE__ */ __name(() => mapElement(schema.def.getter()), "getter")
|
|
72
|
+
});
|
|
73
|
+
} else if (schema instanceof z.ZodPromise) {
|
|
74
|
+
return new z.ZodPromise({
|
|
75
|
+
...schema.def,
|
|
76
|
+
innerType: mapElement(schema.def.innerType)
|
|
77
|
+
});
|
|
78
|
+
} else if (schema instanceof z.ZodCatch) {
|
|
79
|
+
return new z.ZodCatch({
|
|
80
|
+
...schema.def,
|
|
81
|
+
innerType: mapElement(schema.def.innerType)
|
|
82
|
+
});
|
|
83
|
+
} else if (schema instanceof z.ZodTuple) {
|
|
84
|
+
return new z.ZodTuple({
|
|
85
|
+
...schema.def,
|
|
86
|
+
items: schema.def.items.map((item) => mapElement(item)),
|
|
87
|
+
rest: schema.def.rest && mapElement(schema.def.rest)
|
|
88
|
+
});
|
|
89
|
+
} else if (schema instanceof z.ZodUnion) {
|
|
90
|
+
return new z.ZodUnion({
|
|
91
|
+
...schema.def,
|
|
92
|
+
options: schema.def.options.map((option) => mapElement(option))
|
|
93
|
+
});
|
|
94
|
+
} else if (schema instanceof z.ZodIntersection) {
|
|
95
|
+
return new z.ZodIntersection({
|
|
96
|
+
...schema.def,
|
|
97
|
+
right: mapElement(schema.def.right),
|
|
98
|
+
left: mapElement(schema.def.left)
|
|
99
|
+
});
|
|
100
|
+
} else if (schema instanceof z.ZodRecord) {
|
|
101
|
+
return new z.ZodRecord({
|
|
102
|
+
...schema.def,
|
|
103
|
+
keyType: schema.def.keyType,
|
|
104
|
+
valueType: mapElement(schema.def.valueType)
|
|
105
|
+
});
|
|
106
|
+
} else {
|
|
107
|
+
return schema;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
__name(mapInner, "mapInner");
|
|
111
|
+
return fn(mapInner());
|
|
112
|
+
}
|
|
113
|
+
__name(mapOnSchema, "mapOnSchema");
|
|
114
|
+
function zodDeepPartial(schema) {
|
|
115
|
+
return mapOnSchema(schema, (s) => s?.optional());
|
|
116
|
+
}
|
|
117
|
+
__name(zodDeepPartial, "zodDeepPartial");
|
|
118
|
+
|
|
119
|
+
export { mapOnSchema, zodDeepPartial };
|
|
120
|
+
//# sourceMappingURL=index.js.map
|
|
121
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["RESOLVING","Symbol","mapOnSchema","schema","fn","results","Map","mapElement","s","value","get","Error","undefined","set","result","mapInner","z","ZodObject","newShape","key","Object","entries","shape","def","ZodArray","element","ZodMap","keyType","valueType","ZodSet","ZodOptional","innerType","ZodNullable","ZodDefault","ZodReadonly","ZodLazy","getter","ZodPromise","ZodCatch","ZodTuple","items","map","item","rest","ZodUnion","options","option","ZodIntersection","right","left","ZodRecord","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GAAG/C,MAAAA,CAAOoB,GAAAA;AACVI,QAAAA,OAAAA,EAASxB,OAAOoB,GAAAA,CAAII,OAAAA;QACpBC,SAAAA,EAAWrB,UAAAA,CAAWJ,MAAAA,CAAOoB,GAAAA,CAAIK,SAAS;OAC9C,CAAA;IACJ,CAAA,MAAO;AACH,MAAA,OAAOzB,MAAAA;AACX,IAAA;AACJ,EAAA;AAzFSY,EAAAA,MAAAA,CAAAA,QAAAA,EAAAA,UAAAA,CAAAA;AA2FT,EAAA,OAAOX,EAAAA,CAAGW,UAAAA,CAAAA;AACd;AA9GgBb,MAAAA,CAAAA,WAAAA,EAAAA,aAAAA,CAAAA;AAgHT,SAASiD,eAAuChD,MAAAA,EAAS;AAC5D,EAAA,OAAOD,YAAYC,MAAAA,EAAQ,CAACK,CAAAA,KAAOA,CAAAA,EAAW4C,UAAAA,CAAAA;AAClD;AAFgBD,MAAAA,CAAAA,cAAAA,EAAAA,gBAAAA,CAAAA","file":"index.js","sourcesContent":["export * from './schema';\n\n/*\n Copyright 2024, Jaen - https://github.com/jaens\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n http://www.apache.org/licenses/LICENSE-2.0\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License.\n\n https://gist.github.com/jaens/7e15ae1984bb338c86eb5e452dee3010\n*/\n\nimport { z } from 'zod/v4';\nimport type { $ZodType } from 'zod/v4/core';\n\nconst RESOLVING = Symbol('mapOnSchema/resolving');\n\nexport function mapOnSchema<T extends $ZodType, TResult extends $ZodType>(\n schema: T,\n fn: (schema: $ZodType) => TResult,\n): TResult;\n\n/**\n * Applies {@link fn} to each element of the schema recursively, replacing every schema with its return value.\n * The rewriting is applied bottom-up (ie. {@link fn} will get called on \"children\" first).\n */\nexport function mapOnSchema(schema: $ZodType, fn: (schema: $ZodType) => $ZodType): $ZodType {\n // Cache results to support recursive schemas\n const results = new Map<$ZodType, $ZodType | typeof RESOLVING>();\n\n function mapElement(s: $ZodType) {\n const value = results.get(s);\n if (value === RESOLVING) {\n throw new Error('Recursive schema access detected');\n } else if (value !== undefined) {\n return value;\n }\n\n results.set(s, RESOLVING);\n const result = mapOnSchema(s, fn);\n results.set(s, result);\n return result;\n }\n\n function mapInner() {\n if (schema instanceof z.ZodObject) {\n const newShape: Record<string, $ZodType> = {};\n for (const [key, value] of Object.entries(schema.shape)) {\n newShape[key] = mapElement(value as $ZodType);\n }\n\n return new z.ZodObject({\n ...schema.def,\n shape: newShape,\n });\n } else if (schema instanceof z.ZodArray) {\n return new z.ZodArray({\n ...schema.def,\n element: mapElement(schema.def.element),\n });\n } else if (schema instanceof z.ZodMap) {\n return new z.ZodMap({\n ...schema.def,\n keyType: mapElement(schema.def.keyType),\n valueType: mapElement(schema.def.valueType),\n });\n } else if (schema instanceof z.ZodSet) {\n return new z.ZodSet({\n ...schema.def,\n valueType: mapElement(schema.def.valueType),\n });\n } else if (schema instanceof z.ZodOptional) {\n return new z.ZodOptional({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodNullable) {\n return new z.ZodNullable({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodDefault) {\n return new z.ZodDefault({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodReadonly) {\n return new z.ZodReadonly({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodLazy) {\n return new z.ZodLazy({\n ...schema.def,\n // NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas\n getter: () => mapElement(schema.def.getter()),\n });\n } else if (schema instanceof z.ZodPromise) {\n return new z.ZodPromise({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodCatch) {\n return new z.ZodCatch({\n ...schema.def,\n innerType: mapElement(schema.def.innerType),\n });\n } else if (schema instanceof z.ZodTuple) {\n return new z.ZodTuple({\n ...schema.def,\n items: schema.def.items.map((item: $ZodType) => mapElement(item)),\n rest: schema.def.rest && mapElement(schema.def.rest),\n });\n } else if (schema instanceof z.ZodUnion) {\n return new z.ZodUnion({\n ...schema.def,\n options: schema.def.options.map((option: $ZodType) => mapElement(option)),\n });\n } else if (schema instanceof z.ZodIntersection) {\n return new z.ZodIntersection({\n ...schema.def,\n right: mapElement(schema.def.right),\n left: mapElement(schema.def.left),\n });\n } else if (schema instanceof z.ZodRecord) {\n return new z.ZodRecord({\n ...schema.def,\n keyType: schema.def.keyType as any,\n valueType: mapElement(schema.def.valueType),\n });\n } else {\n return schema;\n }\n }\n\n return fn(mapInner());\n}\n\nexport function zodDeepPartial<T extends z.ZodTypeAny>(schema: T): T {\n return mapOnSchema(schema, (s) => (s as any)?.optional()) as T;\n}\n"]}
|
package/dist/schema.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var CAXEKYBD_cjs = require('./CAXEKYBD.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "customSchema", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return CAXEKYBD_cjs.customSchema; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "functionSchema", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return CAXEKYBD_cjs.functionSchema; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "schemaIsFunctionSchema", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return CAXEKYBD_cjs.schemaIsFunctionSchema; }
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=schema.cjs.map
|
|
20
|
+
//# sourceMappingURL=schema.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"schema.cjs"}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
type IsPromise<T> = T extends z.ZodOptional<infer I> ? IsPromise<I> : T extends z.ZodDefault<infer I> ? IsPromise<I> : T extends z.ZodNullable<infer I> ? IsPromise<I> : T extends z.ZodPromise<any> ? true : false;
|
|
3
|
+
export declare const functionSchema: <Input extends z.ZodTuple, Output extends z.ZodType>({ input, output, }: {
|
|
4
|
+
input: Input;
|
|
5
|
+
output: Output;
|
|
6
|
+
}) => z.ZodType<(...args: z.infer<Input>) => IsPromise<Output> extends true ? Promise<z.infer<Output>> : z.infer<Output>>;
|
|
7
|
+
export declare const schemaIsFunctionSchema: (schema: z.ZodType) => schema is ReturnType<typeof functionSchema>;
|
|
8
|
+
export declare const customSchema: <T>(validate?: (data: any) => T) => z.ZodType<T>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAE3B,KAAK,SAAS,CAAC,CAAC,IACZ,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,GAC1B,SAAS,CAAC,CAAC,CAAC,GACZ,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,GAC7B,SAAS,CAAC,CAAC,CAAC,GACZ,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,GAC9B,SAAS,CAAC,CAAC,CAAC,GACZ,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,GACzB,IAAI,GACJ,KAAK,CAAC;AAKtB,eAAO,MAAM,cAAc,GAAI,KAAK,SAAS,CAAC,CAAC,QAAQ,EAAE,MAAM,SAAS,CAAC,CAAC,OAAO,EAAE,oBAGhF;IACC,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAClB,KAiB4B,CAAC,CAAC,OAAO,CAC9B,CACI,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,KAEtB,SAAS,CAAC,MAAM,CAAC,SAAS,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAEvF,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAC/B,QAAQ,CAAC,CAAC,OAAO,KAClB,MAAM,IAAI,UAAU,CAAC,OAAO,cAAc,CAE5C,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,CAAC,EAAE,WAAW,CAAC,IAAI,EAAE,GAAG,KAAK,CAAC,KAC9B,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"schema.js"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@layerzerolabs/zod-utils",
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"require": "./dist/index.cjs",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"zod": "^3.23.8"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"tsup": "^8.4.0",
|
|
21
|
+
"@layerzerolabs/tsup-configuration": "0.0.8",
|
|
22
|
+
"@layerzerolabs/typescript-configuration": "0.0.8"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "restricted",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"clean": "rm -rf ./node_modules .turbo ./dist",
|
|
31
|
+
"dev": "tsup --watch",
|
|
32
|
+
"lint": "eslint . --max-warnings 0",
|
|
33
|
+
"lint:fix": "eslint . --fix --max-warnings 0"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
export * from './schema';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Copyright 2024, Jaen - https://github.com/jaens
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
|
14
|
+
|
|
15
|
+
https://gist.github.com/jaens/7e15ae1984bb338c86eb5e452dee3010
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { z } from 'zod/v4';
|
|
19
|
+
import type { $ZodType } from 'zod/v4/core';
|
|
20
|
+
|
|
21
|
+
const RESOLVING = Symbol('mapOnSchema/resolving');
|
|
22
|
+
|
|
23
|
+
export function mapOnSchema<T extends $ZodType, TResult extends $ZodType>(
|
|
24
|
+
schema: T,
|
|
25
|
+
fn: (schema: $ZodType) => TResult,
|
|
26
|
+
): TResult;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Applies {@link fn} to each element of the schema recursively, replacing every schema with its return value.
|
|
30
|
+
* The rewriting is applied bottom-up (ie. {@link fn} will get called on "children" first).
|
|
31
|
+
*/
|
|
32
|
+
export function mapOnSchema(schema: $ZodType, fn: (schema: $ZodType) => $ZodType): $ZodType {
|
|
33
|
+
// Cache results to support recursive schemas
|
|
34
|
+
const results = new Map<$ZodType, $ZodType | typeof RESOLVING>();
|
|
35
|
+
|
|
36
|
+
function mapElement(s: $ZodType) {
|
|
37
|
+
const value = results.get(s);
|
|
38
|
+
if (value === RESOLVING) {
|
|
39
|
+
throw new Error('Recursive schema access detected');
|
|
40
|
+
} else if (value !== undefined) {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
results.set(s, RESOLVING);
|
|
45
|
+
const result = mapOnSchema(s, fn);
|
|
46
|
+
results.set(s, result);
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function mapInner() {
|
|
51
|
+
if (schema instanceof z.ZodObject) {
|
|
52
|
+
const newShape: Record<string, $ZodType> = {};
|
|
53
|
+
for (const [key, value] of Object.entries(schema.shape)) {
|
|
54
|
+
newShape[key] = mapElement(value as $ZodType);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return new z.ZodObject({
|
|
58
|
+
...schema.def,
|
|
59
|
+
shape: newShape,
|
|
60
|
+
});
|
|
61
|
+
} else if (schema instanceof z.ZodArray) {
|
|
62
|
+
return new z.ZodArray({
|
|
63
|
+
...schema.def,
|
|
64
|
+
element: mapElement(schema.def.element),
|
|
65
|
+
});
|
|
66
|
+
} else if (schema instanceof z.ZodMap) {
|
|
67
|
+
return new z.ZodMap({
|
|
68
|
+
...schema.def,
|
|
69
|
+
keyType: mapElement(schema.def.keyType),
|
|
70
|
+
valueType: mapElement(schema.def.valueType),
|
|
71
|
+
});
|
|
72
|
+
} else if (schema instanceof z.ZodSet) {
|
|
73
|
+
return new z.ZodSet({
|
|
74
|
+
...schema.def,
|
|
75
|
+
valueType: mapElement(schema.def.valueType),
|
|
76
|
+
});
|
|
77
|
+
} else if (schema instanceof z.ZodOptional) {
|
|
78
|
+
return new z.ZodOptional({
|
|
79
|
+
...schema.def,
|
|
80
|
+
innerType: mapElement(schema.def.innerType),
|
|
81
|
+
});
|
|
82
|
+
} else if (schema instanceof z.ZodNullable) {
|
|
83
|
+
return new z.ZodNullable({
|
|
84
|
+
...schema.def,
|
|
85
|
+
innerType: mapElement(schema.def.innerType),
|
|
86
|
+
});
|
|
87
|
+
} else if (schema instanceof z.ZodDefault) {
|
|
88
|
+
return new z.ZodDefault({
|
|
89
|
+
...schema.def,
|
|
90
|
+
innerType: mapElement(schema.def.innerType),
|
|
91
|
+
});
|
|
92
|
+
} else if (schema instanceof z.ZodReadonly) {
|
|
93
|
+
return new z.ZodReadonly({
|
|
94
|
+
...schema.def,
|
|
95
|
+
innerType: mapElement(schema.def.innerType),
|
|
96
|
+
});
|
|
97
|
+
} else if (schema instanceof z.ZodLazy) {
|
|
98
|
+
return new z.ZodLazy({
|
|
99
|
+
...schema.def,
|
|
100
|
+
// NB: This leaks `fn` into the schema, but there is no other way to support recursive schemas
|
|
101
|
+
getter: () => mapElement(schema.def.getter()),
|
|
102
|
+
});
|
|
103
|
+
} else if (schema instanceof z.ZodPromise) {
|
|
104
|
+
return new z.ZodPromise({
|
|
105
|
+
...schema.def,
|
|
106
|
+
innerType: mapElement(schema.def.innerType),
|
|
107
|
+
});
|
|
108
|
+
} else if (schema instanceof z.ZodCatch) {
|
|
109
|
+
return new z.ZodCatch({
|
|
110
|
+
...schema.def,
|
|
111
|
+
innerType: mapElement(schema.def.innerType),
|
|
112
|
+
});
|
|
113
|
+
} else if (schema instanceof z.ZodTuple) {
|
|
114
|
+
return new z.ZodTuple({
|
|
115
|
+
...schema.def,
|
|
116
|
+
items: schema.def.items.map((item: $ZodType) => mapElement(item)),
|
|
117
|
+
rest: schema.def.rest && mapElement(schema.def.rest),
|
|
118
|
+
});
|
|
119
|
+
} else if (schema instanceof z.ZodUnion) {
|
|
120
|
+
return new z.ZodUnion({
|
|
121
|
+
...schema.def,
|
|
122
|
+
options: schema.def.options.map((option: $ZodType) => mapElement(option)),
|
|
123
|
+
});
|
|
124
|
+
} else if (schema instanceof z.ZodIntersection) {
|
|
125
|
+
return new z.ZodIntersection({
|
|
126
|
+
...schema.def,
|
|
127
|
+
right: mapElement(schema.def.right),
|
|
128
|
+
left: mapElement(schema.def.left),
|
|
129
|
+
});
|
|
130
|
+
} else if (schema instanceof z.ZodRecord) {
|
|
131
|
+
return new z.ZodRecord({
|
|
132
|
+
...schema.def,
|
|
133
|
+
keyType: schema.def.keyType as any,
|
|
134
|
+
valueType: mapElement(schema.def.valueType),
|
|
135
|
+
});
|
|
136
|
+
} else {
|
|
137
|
+
return schema;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return fn(mapInner());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function zodDeepPartial<T extends z.ZodTypeAny>(schema: T): T {
|
|
145
|
+
return mapOnSchema(schema, (s) => (s as any)?.optional()) as T;
|
|
146
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
type IsPromise<T> =
|
|
4
|
+
T extends z.ZodOptional<infer I>
|
|
5
|
+
? IsPromise<I>
|
|
6
|
+
: T extends z.ZodDefault<infer I>
|
|
7
|
+
? IsPromise<I>
|
|
8
|
+
: T extends z.ZodNullable<infer I>
|
|
9
|
+
? IsPromise<I>
|
|
10
|
+
: T extends z.ZodPromise<any>
|
|
11
|
+
? true
|
|
12
|
+
: false;
|
|
13
|
+
|
|
14
|
+
const functionSchemaTag = Symbol('__FUNCTION_SCHEMA_TAG');
|
|
15
|
+
|
|
16
|
+
//this essentially replicates the functionality of z.function in zod v3
|
|
17
|
+
export const functionSchema = <Input extends z.ZodTuple, Output extends z.ZodType>({
|
|
18
|
+
input,
|
|
19
|
+
output,
|
|
20
|
+
}: {
|
|
21
|
+
input: Input;
|
|
22
|
+
output: Output;
|
|
23
|
+
}) => {
|
|
24
|
+
const obj = z.object({
|
|
25
|
+
input,
|
|
26
|
+
output,
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(obj, functionSchemaTag, { value: true, writable: false });
|
|
29
|
+
//we don't expect the function to actually be an object with an input field and an output field
|
|
30
|
+
//so we override the parse function. we can't actually parse functions since input and output
|
|
31
|
+
//types are lost at runtime, but we can check the runtime type of the object
|
|
32
|
+
obj.parse = (i) => {
|
|
33
|
+
if (typeof i !== 'function') {
|
|
34
|
+
throw new Error(`${i} is not a function`);
|
|
35
|
+
}
|
|
36
|
+
//the return type here just has to match the inference of z.object,
|
|
37
|
+
//the actual inference of the schema will be the z.ZodType custom
|
|
38
|
+
return i as any;
|
|
39
|
+
};
|
|
40
|
+
return obj as unknown as z.ZodType<
|
|
41
|
+
(
|
|
42
|
+
...args: z.infer<Input>
|
|
43
|
+
//z.ZodPromise<T> infers as T, not Promise<T>
|
|
44
|
+
) => IsPromise<Output> extends true ? Promise<z.infer<Output>> : z.infer<Output>
|
|
45
|
+
>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const schemaIsFunctionSchema = (
|
|
49
|
+
schema: z.ZodType,
|
|
50
|
+
): schema is ReturnType<typeof functionSchema> => {
|
|
51
|
+
return Object.hasOwn(schema, functionSchemaTag);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const customSchema = <T>(validate?: (data: any) => T) =>
|
|
55
|
+
z.custom<T>(validate) as z.ZodType<T>;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@layerzerolabs/typescript-configuration/tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"strictPropertyInitialization": false,
|
|
7
|
+
"noUnusedLocals": false,
|
|
8
|
+
"noUnusedParameters": false,
|
|
9
|
+
"jsx": "react-jsx"
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"node_modules",
|
|
13
|
+
"**/__mocks__/*",
|
|
14
|
+
"**/__tests__/*",
|
|
15
|
+
"**/*.spec.ts",
|
|
16
|
+
"**/*.test.ts",
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"include": ["src/**/*"],
|
|
20
|
+
"ts-node": {
|
|
21
|
+
"files": true,
|
|
22
|
+
"experimentalResolverFeatures": true,
|
|
23
|
+
"compilerOptions": {
|
|
24
|
+
"module": "CommonJS",
|
|
25
|
+
"types": ["node"]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|