@layerzerolabs/zod-utils 1.2.66 → 1.2.68
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/dist/A2ZHOEG7.js +92 -0
- package/dist/A2ZHOEG7.js.map +1 -0
- package/dist/index.js +2 -2
- package/dist/schema.d.ts +11 -2
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +1 -1
- package/package.json +6 -6
- package/dist/CW7ABWSU.js +0 -52
- package/dist/CW7ABWSU.js.map +0 -1
package/dist/A2ZHOEG7.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
var functionSchemaTag = "__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.input = input;
|
|
16
|
+
obj.output = output;
|
|
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) => z.custom(validate), "customSchema");
|
|
29
|
+
var brandedSchemaPropertyKey = "__BRANDED_SCHEMA_NAME";
|
|
30
|
+
var reshapingMethods = [
|
|
31
|
+
"extend",
|
|
32
|
+
"safeExtend",
|
|
33
|
+
"merge",
|
|
34
|
+
"pick",
|
|
35
|
+
"omit",
|
|
36
|
+
"partial",
|
|
37
|
+
"required",
|
|
38
|
+
"catchall",
|
|
39
|
+
"passthrough",
|
|
40
|
+
"loose",
|
|
41
|
+
"strict",
|
|
42
|
+
"strip",
|
|
43
|
+
"rest",
|
|
44
|
+
"extract",
|
|
45
|
+
"exclude"
|
|
46
|
+
];
|
|
47
|
+
var getBrand = /* @__PURE__ */ __name((schema) => schema._zod.def[brandedSchemaPropertyKey], "getBrand");
|
|
48
|
+
var guardBrandedSchema = /* @__PURE__ */ __name((schema) => {
|
|
49
|
+
const inst = schema;
|
|
50
|
+
for (const method of reshapingMethods) {
|
|
51
|
+
if (typeof inst[method] !== "function") continue;
|
|
52
|
+
inst[method] = () => {
|
|
53
|
+
throw new Error(`Cannot "${method}" a branded schema ${getBrand(schema)}`);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (typeof inst.clone === "function") {
|
|
57
|
+
const clone = inst.clone;
|
|
58
|
+
inst.clone = (...args) => {
|
|
59
|
+
const cloned = clone(...args);
|
|
60
|
+
if (getBrand(cloned) !== void 0) {
|
|
61
|
+
guardBrandedSchema(cloned);
|
|
62
|
+
}
|
|
63
|
+
return cloned;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}, "guardBrandedSchema");
|
|
67
|
+
var brandSchema = /* @__PURE__ */ __name((schema, name) => {
|
|
68
|
+
Object.defineProperty(schema._zod.def, brandedSchemaPropertyKey, {
|
|
69
|
+
enumerable: false,
|
|
70
|
+
configurable: false,
|
|
71
|
+
writable: false,
|
|
72
|
+
value: name
|
|
73
|
+
});
|
|
74
|
+
guardBrandedSchema(schema);
|
|
75
|
+
return schema;
|
|
76
|
+
}, "brandSchema");
|
|
77
|
+
var isBrandedWith = /* @__PURE__ */ __name((expected, schema) => {
|
|
78
|
+
const expectedBrand = getBrand(expected);
|
|
79
|
+
if (expectedBrand === void 0) {
|
|
80
|
+
throw new Error("Branded model is not itself branded");
|
|
81
|
+
}
|
|
82
|
+
return getBrand(schema) === expectedBrand;
|
|
83
|
+
}, "isBrandedWith");
|
|
84
|
+
var createFunctionPointerSchema = /* @__PURE__ */ __name(() => (
|
|
85
|
+
// TODO: replace with a concrete zod schema
|
|
86
|
+
brandSchema(customSchema(), "FunctionPointerSchema")
|
|
87
|
+
), "createFunctionPointerSchema");
|
|
88
|
+
var functionPointerSchema = createFunctionPointerSchema();
|
|
89
|
+
|
|
90
|
+
export { __name, brandSchema, createFunctionPointerSchema, customSchema, functionPointerSchema, functionSchema, functionSchemaTag, isBrandedWith, schemaIsFunctionSchema };
|
|
91
|
+
//# sourceMappingURL=A2ZHOEG7.js.map
|
|
92
|
+
//# sourceMappingURL=A2ZHOEG7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema.ts"],"names":["functionSchemaTag","functionSchema","input","output","obj","z","object","Object","defineProperty","value","writable","parse","i","Error","schemaIsFunctionSchema","schema","hasOwn","customSchema","validate","custom","brandedSchemaPropertyKey","reshapingMethods","getBrand","_zod","def","guardBrandedSchema","inst","method","clone","args","cloned","undefined","brandSchema","name","enumerable","configurable","isBrandedWith","expected","expectedBrand","createFunctionPointerSchema","functionPointerSchema"],"mappings":";;;;AAWO,IAAMA,iBAAAA,GAAoB;AAY1B,IAAMC,cAAAA,mBAAiB,MAAA,CAAA,CAAoD,EAC9EC,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,KAAKJ,iBAAAA,EAAmB;IAAES,KAAAA,EAAO,IAAA;IAAMC,QAAAA,EAAU;GAAM,CAAA;AAC5EN,EAAAA,GAAAA,CAAYF,KAAAA,GAAQA,KAAAA;A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AIC,kBAAkBP,MAAAA,EAAW;AAC7B,IAAA,MAAM,IAAIlB,MAAM,qCAAA,CAAA;AACpB,EAAA;AAEA,EAAA,OAAOS,QAAAA,CAASP,MAAAA,CAAAA,KAAYuB,aAAAA;AAChC,CAAA,EAV6B,eAAA;AAYtB,IAAMC,2BAAAA,mBAA8B,MAAA,CAAA;;EAEvCP,WAAAA,CAAYf,YAAAA,IAAmB,uBAAA;AAFQ,CAAA,EAAA,6BAAA;AAIpC,IAAMuB,wBAAwBD,2BAAAA","file":"A2ZHOEG7.js","sourcesContent":["import { z } from 'zod';\nimport type { $ZodType } from 'zod/v4/core';\n\nimport type { FunctionPointer } from '@layerzerolabs/function-pointer';\n\nexport type InferredArray<T extends $ZodType[], Output extends any[] = []> = T extends []\n ? Output\n : T extends [infer Head, ...infer Tail extends $ZodType[]]\n ? InferredArray<Tail, [...Output, z.infer<Head>]>\n : never;\n\nexport const functionSchemaTag = '__FUNCTION_SCHEMA_TAG';\n\n// type assert that the z.ZodType type doesn't have a property `input` or `output` as it would conflict with our function schema type injection\nconst _check: true = null as any as keyof z.ZodType<string> extends infer U\n ? 'input' extends U\n ? false\n : 'output' extends U\n ? false\n : true\n : false;\n\n// This essentially replicates the functionality of z.function in zod v3\nexport const functionSchema = <Input extends z.ZodTuple, Output extends $ZodType>({\n input,\n output,\n}: {\n input: Input;\n output: Output;\n}) => {\n const obj = z.object({\n input,\n output: output as unknown as z.ZodType,\n });\n Object.defineProperty(obj, functionSchemaTag, { value: true, writable: false });\n (obj as any).input = input;\n (obj as any).output = output;\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\n // we have to decide here whether the args should be typed as the input or the output of the schema--\n // neither answer is correct and this is probably why zod dropped the function schema in v4\n // use infer for now because many of our schemata don't have a defined input and will silently produce `unknown`\n return obj as unknown as z.ZodType<(...args: z.infer<Input>) => z.infer<Output>> & {\n // inject these fields so that the original input and output schemata can be recovered pre-inference\n input: Input;\n output: Output;\n __FUNCTION_SCHEMA_TAG: true;\n };\n};\n\n// takes the z.infer of the schema, except for any top-level function schemata,\n// which are introspected to extract their input's output type\nexport type InferSchemaOutputWithInvertedFunctionSchemata<T extends $ZodType> =\n T extends z.ZodObject\n ? z.infer<\n z.ZodObject<{\n [K in keyof T['shape']]: T['shape'][K] extends {\n input: z.ZodTuple;\n output: $ZodType;\n }\n ? z.ZodType<\n (\n ...args: z.infer<T['shape'][K]['input']>\n ) => z.infer<T['shape'][K]['output']>\n >\n : T['shape'][K] extends $ZodType\n ? T['shape'][K]\n : never;\n }>\n >\n : z.infer<T>;\n\nexport const schemaIsFunctionSchema = (\n schema: $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, T>;\n\nexport type BuildZodObject<T extends object> = z.ZodObject<\n {\n [K in keyof T]: z.ZodType<T[K]>;\n },\n z.core.$strip\n>;\n\n// utils for branding schemata with names\nconst brandedSchemaPropertyKey = '__BRANDED_SCHEMA_NAME' as const;\n\nconst reshapingMethods = [\n 'extend',\n 'safeExtend',\n 'merge',\n 'pick',\n 'omit',\n 'partial',\n 'required',\n 'catchall',\n 'passthrough',\n 'loose',\n 'strict',\n 'strip',\n 'rest',\n 'extract',\n 'exclude',\n] as const;\n\nconst getBrand = (schema: $ZodType): string | undefined =>\n (schema._zod.def as { [brandedSchemaPropertyKey]?: string })[brandedSchemaPropertyKey];\n\nconst guardBrandedSchema = (schema: $ZodType) => {\n const inst = schema as unknown as Record<string, unknown>;\n for (const method of reshapingMethods) {\n if (typeof inst[method] !== 'function') continue;\n inst[method] = () => {\n throw new Error(`Cannot \"${method}\" a branded schema ${getBrand(schema)}`);\n };\n }\n\n // override the clone so that the cloned schemata are guarded\n if (typeof inst.clone === 'function') {\n const clone = inst.clone as (...args: unknown[]) => $ZodType;\n inst.clone = (...args: unknown[]) => {\n const cloned = clone(...args);\n if (getBrand(cloned) !== undefined) {\n guardBrandedSchema(cloned);\n }\n\n return cloned;\n };\n }\n};\n\n/**\n * Unavoidably, this mutates the underlying schema. Use with caution. In particular:\n * - `.describe()` and `.meta()` share their definition with the schema they were called on, so\n * branding `x.describe('...')` brands `x` too.\n * - The brand cannot be replaced, so branding a schema derived from a branded one (e.g.\n * `brandSchema(chainNameSchema.describe('...'), 'Other')`) throws.\n *\n * Branded schemata cannot be `extend`ed, `pick`ed, etc, but they can be `describe`d.\n */\nexport const brandSchema = <Schema extends $ZodType>(schema: Schema, name: string): Schema => {\n Object.defineProperty(schema._zod.def, brandedSchemaPropertyKey, {\n enumerable: false,\n configurable: false,\n writable: false,\n value: name,\n });\n\n guardBrandedSchema(schema);\n\n return schema;\n};\n\nexport const isBrandedWith = <Branded extends $ZodType>(\n expected: Branded,\n schema: $ZodType,\n): schema is Branded => {\n const expectedBrand = getBrand(expected);\n if (expectedBrand === undefined) {\n throw new Error('Branded model is not itself branded');\n }\n\n return getBrand(schema) === expectedBrand;\n};\n\nexport const createFunctionPointerSchema = <T extends FunctionPointer>() =>\n // TODO: replace with a concrete zod schema\n brandSchema(customSchema<T>(), 'FunctionPointerSchema');\n\nexport const functionPointerSchema = createFunctionPointerSchema();\n"]}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { __name } from './
|
|
2
|
-
export { brandSchema, createFunctionPointerSchema, customSchema, functionSchema, functionSchemaTag, isBrandedWith, schemaIsFunctionSchema } from './
|
|
1
|
+
import { __name } from './A2ZHOEG7.js';
|
|
2
|
+
export { brandSchema, createFunctionPointerSchema, customSchema, functionPointerSchema, functionSchema, functionSchemaTag, isBrandedWith, schemaIsFunctionSchema } from './A2ZHOEG7.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
5
|
var RESOLVING = /* @__PURE__ */ Symbol("mapOnSchema/resolving");
|
package/dist/schema.d.ts
CHANGED
|
@@ -19,11 +19,20 @@ export type InferSchemaOutputWithInvertedFunctionSchemata<T extends $ZodType> =
|
|
|
19
19
|
}>> : z.infer<T>;
|
|
20
20
|
export declare const schemaIsFunctionSchema: (schema: $ZodType) => schema is ReturnType<typeof functionSchema>;
|
|
21
21
|
export declare const customSchema: <T>(validate?: (data: any) => T) => z.ZodType<T, T>;
|
|
22
|
-
export declare const createFunctionPointerSchema: <T extends FunctionPointer>() => z.ZodType<T, T, z.core.$ZodTypeInternals<T, T>>;
|
|
23
22
|
export type BuildZodObject<T extends object> = z.ZodObject<{
|
|
24
23
|
[K in keyof T]: z.ZodType<T[K]>;
|
|
25
24
|
}, z.core.$strip>;
|
|
26
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Unavoidably, this mutates the underlying schema. Use with caution. In particular:
|
|
27
|
+
* - `.describe()` and `.meta()` share their definition with the schema they were called on, so
|
|
28
|
+
* branding `x.describe('...')` brands `x` too.
|
|
29
|
+
* - The brand cannot be replaced, so branding a schema derived from a branded one (e.g.
|
|
30
|
+
* `brandSchema(chainNameSchema.describe('...'), 'Other')`) throws.
|
|
31
|
+
*
|
|
32
|
+
* Branded schemata cannot be `extend`ed, `pick`ed, etc, but they can be `describe`d.
|
|
33
|
+
*/
|
|
27
34
|
export declare const brandSchema: <Schema extends $ZodType>(schema: Schema, name: string) => Schema;
|
|
28
35
|
export declare const isBrandedWith: <Branded extends $ZodType>(expected: Branded, schema: $ZodType) => schema is Branded;
|
|
36
|
+
export declare const createFunctionPointerSchema: <T extends FunctionPointer>() => z.ZodType<T, T, z.core.$ZodTypeInternals<T, T>>;
|
|
37
|
+
export declare const functionPointerSchema: z.ZodType<FunctionPointer, FunctionPointer, z.core.$ZodTypeInternals<FunctionPointer, FunctionPointer>>;
|
|
29
38
|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Q,CAAC,CAAC,CACxC,GACD,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,GAC5B,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GACb,KAAK;CAChB,CAAC,CACL,GACD,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAErB,eAAO,MAAM,sBAAsB,GAC/B,QAAQ,QAAQ,KACjB,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,EAAE,CAAC,CAAC,CAAC;AAE7C,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,SAAS,CACtD;KACK,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAClC,EACD,CAAC,CAAC,IAAI,CAAC,MAAM,CAChB,CAAC;AAiDF;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,SAAS,QAAQ,EAAE,QAAQ,MAAM,EAAE,MAAM,MAAM,KAAG,MAWnF,CAAC;AAEF,eAAO,MAAM,aAAa,GAAI,OAAO,SAAS,QAAQ,EAClD,UAAU,OAAO,EACjB,QAAQ,QAAQ,KACjB,MAAM,IAAI,OAOZ,CAAC;AAEF,eAAO,MAAM,2BAA2B,GAAI,CAAC,SAAS,eAAe,sDAEV,CAAC;AAE5D,eAAO,MAAM,qBAAqB,yGAAgC,CAAC"}
|
package/dist/schema.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { brandSchema, createFunctionPointerSchema, customSchema, functionSchema, functionSchemaTag, isBrandedWith, schemaIsFunctionSchema } from './
|
|
1
|
+
export { brandSchema, createFunctionPointerSchema, customSchema, functionPointerSchema, functionSchema, functionSchemaTag, isBrandedWith, schemaIsFunctionSchema } from './A2ZHOEG7.js';
|
|
2
2
|
//# sourceMappingURL=schema.js.map
|
|
3
3
|
//# sourceMappingURL=schema.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@layerzerolabs/zod-utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.68",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
"dist/**/*"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
19
|
+
"@layerzerolabs/function-pointer": "1.2.68",
|
|
20
|
+
"zod": "4.3.5"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"tsup": "
|
|
24
|
-
"@layerzerolabs/typescript-configuration": "1.2.
|
|
25
|
-
"
|
|
23
|
+
"@layerzerolabs/tsup-configuration": "1.2.68",
|
|
24
|
+
"@layerzerolabs/typescript-configuration": "1.2.68",
|
|
25
|
+
"tsup": "^8.4.0"
|
|
26
26
|
},
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public",
|
package/dist/CW7ABWSU.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
-
var functionSchemaTag = "__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.input = input;
|
|
16
|
-
obj.output = output;
|
|
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) => z.custom(validate), "customSchema");
|
|
29
|
-
var createFunctionPointerSchema = /* @__PURE__ */ __name(() => (
|
|
30
|
-
// TODO: replace with a concrete zod schema
|
|
31
|
-
customSchema()
|
|
32
|
-
), "createFunctionPointerSchema");
|
|
33
|
-
var brandedSchemaPropertyKey = "__BRANDED_SCHEMA_NAME";
|
|
34
|
-
var brandSchema = /* @__PURE__ */ __name((schema, name) => Object.defineProperty(schema, brandedSchemaPropertyKey, {
|
|
35
|
-
enumerable: false,
|
|
36
|
-
configurable: false,
|
|
37
|
-
writable: false,
|
|
38
|
-
value: name
|
|
39
|
-
}), "brandSchema");
|
|
40
|
-
var isBrandedWith = /* @__PURE__ */ __name((expected, schema) => {
|
|
41
|
-
if (!(brandedSchemaPropertyKey in expected)) {
|
|
42
|
-
throw new Error("Branded model is not itself branded");
|
|
43
|
-
}
|
|
44
|
-
if (!(brandedSchemaPropertyKey in schema)) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
return schema[brandedSchemaPropertyKey] === expected[brandedSchemaPropertyKey];
|
|
48
|
-
}, "isBrandedWith");
|
|
49
|
-
|
|
50
|
-
export { __name, brandSchema, createFunctionPointerSchema, customSchema, functionSchema, functionSchemaTag, isBrandedWith, schemaIsFunctionSchema };
|
|
51
|
-
//# sourceMappingURL=CW7ABWSU.js.map
|
|
52
|
-
//# sourceMappingURL=CW7ABWSU.js.map
|
package/dist/CW7ABWSU.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/schema.ts"],"names":["functionSchemaTag","functionSchema","input","output","obj","z","object","Object","defineProperty","value","writable","parse","i","Error","schemaIsFunctionSchema","schema","hasOwn","customSchema","validate","custom","createFunctionPointerSchema","brandedSchemaPropertyKey","brandSchema","name","enumerable","configurable","isBrandedWith","expected"],"mappings":";;;;AAWO,IAAMA,iBAAAA,GAAoB;AAY1B,IAAMC,cAAAA,mBAAiB,MAAA,CAAA,CAAoD,EAC9EC,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,KAAKJ,iBAAAA,EAAmB;IAAES,KAAAA,EAAO,IAAA;IAAMC,QAAAA,EAAU;GAAM,CAAA;AAC5EN,EAAAA,GAAAA,CAAYF,KAAAA,GAAQA,KAAAA;AACpBE,EAAAA,GAAAA,CAAYD,MAAAA,GAASA,MAAAA;AAItBC,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;AAKA,EAAA,OAAOR,GAAAA;AAMX,CAAA,EAnC8B,gBAAA;AA2DvB,IAAMU,sBAAAA,2BACTC,MAAAA,KAAAA;AAEA,EAAA,OAAOR,MAAAA,CAAOS,MAAAA,CAAOD,MAAAA,EAAQf,iBAAAA,CAAAA;AACjC,CAAA,EAJsC,wBAAA;AAM/B,IAAMiB,+BAAe,MAAA,CAAA,CAAIC,QAAAA,KAC5Bb,CAAAA,CAAEc,MAAAA,CAAUD,QAAAA,CAAAA,EADY,cAAA;AAGrB,IAAME,2BAAAA,mBAA8B,MAAA,CAAA;;EAEvCH,YAAAA;AAFuC,CAAA,EAAA,6BAAA;AAY3C,IAAMI,wBAAAA,GAA2B,uBAAA;AAG1B,IAAMC,8BAAc,MAAA,CAAA,CAA0BP,MAAAA,EAAgBQ,SACjEhB,MAAAA,CAAOC,cAAAA,CAAeO,QAAQM,wBAAAA,EAA0B;EACpDG,UAAAA,EAAY,KAAA;EACZC,YAAAA,EAAc,KAAA;EACdf,QAAAA,EAAU,KAAA;EACVD,KAAAA,EAAOc;AACX,CAAA,CAAA,EANuB,aAAA;AAQpB,IAAMG,aAAAA,mBAAgB,MAAA,CAAA,CACzBC,QAAAA,EACAZ,MAAAA,KAAAA;AAEA,EAAA,IAAI,EAAEM,4BAA4BM,QAAAA,CAAAA,EAAW;AACzC,IAAA,MAAM,IAAId,MAAM,qCAAA,CAAA;AACpB,EAAA;AAEA,EAAA,IAAI,EAAEQ,4BAA4BN,MAAAA,CAAAA,EAAS;AACvC,IAAA,OAAO,KAAA;AACX,EAAA;AAEA,EAAA,OAAOA,MAAAA,CAAOM,wBAAAA,CAAAA,KAA8BM,QAAAA,CAASN,wBAAAA,CAAAA;AACzD,CAAA,EAb6B,eAAA","file":"CW7ABWSU.js","sourcesContent":["import { z } from 'zod';\nimport type { $ZodType } from 'zod/v4/core';\n\nimport type { FunctionPointer } from '@layerzerolabs/function-pointer';\n\nexport type InferredArray<T extends $ZodType[], Output extends any[] = []> = T extends []\n ? Output\n : T extends [infer Head, ...infer Tail extends $ZodType[]]\n ? InferredArray<Tail, [...Output, z.infer<Head>]>\n : never;\n\nexport const functionSchemaTag = '__FUNCTION_SCHEMA_TAG';\n\n// type assert that the z.ZodType type doesn't have a property `input` or `output` as it would conflict with our function schema type injection\nconst _check: true = null as any as keyof z.ZodType<string> extends infer U\n ? 'input' extends U\n ? false\n : 'output' extends U\n ? false\n : true\n : false;\n\n// This essentially replicates the functionality of z.function in zod v3\nexport const functionSchema = <Input extends z.ZodTuple, Output extends $ZodType>({\n input,\n output,\n}: {\n input: Input;\n output: Output;\n}) => {\n const obj = z.object({\n input,\n output: output as unknown as z.ZodType,\n });\n Object.defineProperty(obj, functionSchemaTag, { value: true, writable: false });\n (obj as any).input = input;\n (obj as any).output = output;\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\n // we have to decide here whether the args should be typed as the input or the output of the schema--\n // neither answer is correct and this is probably why zod dropped the function schema in v4\n // use infer for now because many of our schemata don't have a defined input and will silently produce `unknown`\n return obj as unknown as z.ZodType<(...args: z.infer<Input>) => z.infer<Output>> & {\n // inject these fields so that the original input and output schemata can be recovered pre-inference\n input: Input;\n output: Output;\n __FUNCTION_SCHEMA_TAG: true;\n };\n};\n\n// takes the z.infer of the schema, except for any top-level function schemata,\n// which are introspected to extract their input's output type\nexport type InferSchemaOutputWithInvertedFunctionSchemata<T extends $ZodType> =\n T extends z.ZodObject\n ? z.infer<\n z.ZodObject<{\n [K in keyof T['shape']]: T['shape'][K] extends {\n input: z.ZodTuple;\n output: $ZodType;\n }\n ? z.ZodType<\n (\n ...args: z.infer<T['shape'][K]['input']>\n ) => z.infer<T['shape'][K]['output']>\n >\n : T['shape'][K] extends $ZodType\n ? T['shape'][K]\n : never;\n }>\n >\n : z.infer<T>;\n\nexport const schemaIsFunctionSchema = (\n schema: $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, T>;\n\nexport const createFunctionPointerSchema = <T extends FunctionPointer>() =>\n // TODO: replace with a concrete zod schema\n customSchema<T>();\n\nexport type BuildZodObject<T extends object> = z.ZodObject<\n {\n [K in keyof T]: z.ZodType<T[K]>;\n },\n z.core.$strip\n>;\n\n// utils for branding schemata with names\nconst brandedSchemaPropertyKey = '__BRANDED_SCHEMA_NAME' as const;\n\n/** Unavoidably, this mutates the underlying schema. Use with caution. */\nexport const brandSchema = <Schema extends $ZodType>(schema: Schema, name: string): Schema =>\n Object.defineProperty(schema, brandedSchemaPropertyKey, {\n enumerable: false,\n configurable: false,\n writable: false,\n value: name,\n });\n\nexport const isBrandedWith = <Branded extends $ZodType>(\n expected: Branded,\n schema: $ZodType,\n): schema is Branded => {\n if (!(brandedSchemaPropertyKey in expected)) {\n throw new Error('Branded model is not itself branded');\n }\n\n if (!(brandedSchemaPropertyKey in schema)) {\n return false;\n }\n\n return schema[brandedSchemaPropertyKey] === expected[brandedSchemaPropertyKey];\n};\n"]}
|