@ptolemy2002/zod-utils 1.1.0 → 1.3.0
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/README.md +2 -0
- package/dist/function.d.ts +9 -0
- package/dist/function.js +88 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/interpret.d.ts +2 -1
- package/dist/interpret.js +2 -9
- package/dist/prefixIssuePath.d.ts +2 -0
- package/dist/prefixIssuePath.js +12 -0
- package/dist/typeGuards.d.ts +2 -1
- package/dist/typeGuards.js +2 -1
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -5,7 +5,9 @@ Various utilities for working with Zod schemas.
|
|
|
5
5
|
- [Type Reference](docs/type-reference.md) - Complete type definitions for all exported types
|
|
6
6
|
- `util` - Utilities for working with Zod schemas and errors
|
|
7
7
|
- [clone](docs/util/clone.md) - Utility for cloning Zod schemas without affecting the original
|
|
8
|
+
- [function](docs/util/function.md) - Schema factory for validating callable functions with input/output schemas
|
|
8
9
|
- [interpret](docs/util/interpret.md) - Utilities for formatting Zod errors as strings
|
|
10
|
+
- [prefixIssuePath](docs/util/prefixIssuePath.md) - Utility for prepending a path prefix to a Zod issue
|
|
9
11
|
- [typeGuards](docs/util/typeGuards.md) - Type guards for Zod-related values
|
|
10
12
|
- [validate](docs/util/validate.md) - Schema-wrapping factories for creating validators
|
|
11
13
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import z, { ZodArray, ZodUnknown } from "zod";
|
|
2
|
+
import { $ZodFunctionArgs, $ZodFunctionOut } from "zod/v4/core";
|
|
3
|
+
export type ZodFunctionParseOptions<In extends $ZodFunctionArgs, Out extends $ZodFunctionOut> = {
|
|
4
|
+
input?: In;
|
|
5
|
+
output?: Out;
|
|
6
|
+
inputPath?: PropertyKey | PropertyKey[];
|
|
7
|
+
outputPath?: PropertyKey | PropertyKey[];
|
|
8
|
+
};
|
|
9
|
+
export declare function zodFunctionSchema<In extends $ZodFunctionArgs = ZodArray<ZodUnknown>, Out extends $ZodFunctionOut = ZodUnknown>(options?: ZodFunctionParseOptions<In, Out>): z.ZodPipe<z.ZodAny, z.ZodTransform<(...args: z.core.output<In>) => z.core.output<Out>, any>>;
|
package/dist/function.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.zodFunctionSchema = zodFunctionSchema;
|
|
40
|
+
const zod_1 = __importStar(require("zod"));
|
|
41
|
+
const typeGuards_1 = require("./typeGuards");
|
|
42
|
+
const prefixIssuePath_1 = require("./prefixIssuePath");
|
|
43
|
+
const is_callable_1 = __importDefault(require("is-callable"));
|
|
44
|
+
function zodFunctionParse(func, { inputPath = "args", outputPath = "return", input = zod_1.default.array(zod_1.default.unknown()), output = zod_1.default.unknown() }) {
|
|
45
|
+
if (!(0, is_callable_1.default)(func)) {
|
|
46
|
+
throw new zod_1.ZodError([{
|
|
47
|
+
message: `Expected a callable function, but received ${typeof func}`,
|
|
48
|
+
path: [],
|
|
49
|
+
code: "invalid_type",
|
|
50
|
+
expected: "function"
|
|
51
|
+
}]);
|
|
52
|
+
}
|
|
53
|
+
const functionFactory = zod_1.default.function({ input, output });
|
|
54
|
+
const wrappedFunction = (setReachedCaller, ...args) => {
|
|
55
|
+
setReachedCaller(true);
|
|
56
|
+
return func(...args);
|
|
57
|
+
};
|
|
58
|
+
return (...args) => {
|
|
59
|
+
// This is how we will differentiate between argument and
|
|
60
|
+
// parameter validation errors
|
|
61
|
+
let reachedCaller = false;
|
|
62
|
+
try {
|
|
63
|
+
const implementedFunction = functionFactory.implement(((...args) => wrappedFunction((v) => (reachedCaller = v), ...args)));
|
|
64
|
+
return implementedFunction(...args);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
if ((0, typeGuards_1.isZodError)(e)) {
|
|
68
|
+
e = new zod_1.ZodError(e.issues.map(i => (0, prefixIssuePath_1.prefixZodIssuePath)(i, reachedCaller ? outputPath : inputPath)));
|
|
69
|
+
}
|
|
70
|
+
throw e;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function zodFunctionSchema(options = {}) {
|
|
75
|
+
// Transform the input value using zodFunctionParse
|
|
76
|
+
// so that this schema can be used to validate functions
|
|
77
|
+
return zod_1.default.any().transform((v, ctx) => {
|
|
78
|
+
try {
|
|
79
|
+
return zodFunctionParse(v, options);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
if ((0, typeGuards_1.isZodError)(e)) {
|
|
83
|
+
e.issues.forEach(issue => ctx.addIssue(issue));
|
|
84
|
+
}
|
|
85
|
+
return zod_1.default.NEVER;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,8 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./function"), exports);
|
|
17
18
|
__exportStar(require("./types"), exports);
|
|
18
19
|
__exportStar(require("./typeGuards"), exports);
|
|
19
20
|
__exportStar(require("./interpret"), exports);
|
|
20
21
|
__exportStar(require("./validate"), exports);
|
|
21
22
|
__exportStar(require("./clone"), exports);
|
|
23
|
+
__exportStar(require("./prefixIssuePath"), exports);
|
package/dist/interpret.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { ZodError } from 'zod';
|
|
2
|
-
|
|
2
|
+
import { $ZodError } from 'zod/v4/core';
|
|
3
|
+
export declare function interpretZodError(err: ZodError | $ZodError, prefix?: PropertyKey | PropertyKey[]): string;
|
package/dist/interpret.js
CHANGED
|
@@ -35,15 +35,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.interpretZodError = interpretZodError;
|
|
37
37
|
const zod_1 = __importStar(require("zod"));
|
|
38
|
+
const prefixIssuePath_1 = require("./prefixIssuePath");
|
|
38
39
|
function interpretZodError(err, prefix = "") {
|
|
39
|
-
|
|
40
|
-
prefix = [prefix];
|
|
41
|
-
const modifiedErr = new zod_1.ZodError(err.issues.map(issue => {
|
|
42
|
-
const newPath = [...prefix, ...issue.path]
|
|
43
|
-
.filter(p => typeof p !== "string" || p.length > 0);
|
|
44
|
-
if (newPath.length === 0)
|
|
45
|
-
newPath.push("(root)");
|
|
46
|
-
return { ...issue, path: newPath };
|
|
47
|
-
}));
|
|
40
|
+
const modifiedErr = new zod_1.ZodError(err.issues.map(issue => (0, prefixIssuePath_1.prefixZodIssuePath)(issue, prefix)));
|
|
48
41
|
return zod_1.default.prettifyError(modifiedErr);
|
|
49
42
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prefixZodIssuePath = prefixZodIssuePath;
|
|
4
|
+
function prefixZodIssuePath(issue, prefix) {
|
|
5
|
+
if (!Array.isArray(prefix))
|
|
6
|
+
prefix = [prefix];
|
|
7
|
+
const newPath = [...prefix, ...issue.path]
|
|
8
|
+
.filter(p => typeof p !== "string" || p.length > 0);
|
|
9
|
+
if (newPath.length === 0)
|
|
10
|
+
newPath.push("(root)");
|
|
11
|
+
return { ...issue, path: newPath };
|
|
12
|
+
}
|
package/dist/typeGuards.d.ts
CHANGED
package/dist/typeGuards.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isZodError = isZodError;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
|
+
const core_1 = require("zod/v4/core");
|
|
5
6
|
function isZodError(err) {
|
|
6
|
-
return !!err && (err instanceof zod_1.ZodError || (err instanceof Error && err.name === 'ZodError'));
|
|
7
|
+
return !!err && (err instanceof zod_1.ZodError || err instanceof core_1.$ZodError || (err instanceof Error && (err.name === 'ZodError' || err.name === '$ZodError')));
|
|
7
8
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptolemy2002/zod-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"zod": "^4.3.6"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@types/is-callable": "^1.1.2",
|
|
35
36
|
"@types/jest": "^29.5.0",
|
|
36
37
|
"@types/node": "^25.3.5",
|
|
37
38
|
"jest": "^29.5.0",
|
|
@@ -40,5 +41,8 @@
|
|
|
40
41
|
"tsconfig-paths": "^4.2.0",
|
|
41
42
|
"typescript-transform-paths": "^3.5.3",
|
|
42
43
|
"zod": "^4.3.6"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"is-callable": "^1.2.7"
|
|
43
47
|
}
|
|
44
48
|
}
|