@ptolemy2002/zod-utils 1.1.0 → 1.2.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 +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interpret.js +2 -9
- package/dist/prefixIssuePath.d.ts +2 -0
- package/dist/prefixIssuePath.js +12 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Various utilities for working with Zod schemas.
|
|
|
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
8
|
- [interpret](docs/util/interpret.md) - Utilities for formatting Zod errors as strings
|
|
9
|
+
- [prefixIssuePath](docs/util/prefixIssuePath.md) - Utility for prepending a path prefix to a Zod issue
|
|
9
10
|
- [typeGuards](docs/util/typeGuards.md) - Type guards for Zod-related values
|
|
10
11
|
- [validate](docs/util/validate.md) - Schema-wrapping factories for creating validators
|
|
11
12
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
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
|
+
}
|