@ptolemy2002/zod-utils 1.0.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 ADDED
@@ -0,0 +1,46 @@
1
+ # Zod Utils
2
+ Various utilities for working with Zod schemas.
3
+
4
+ ## Table of Contents
5
+ - [Type Reference](docs/type-reference.md) - Complete type definitions for all exported types
6
+ - `util` - Utilities for working with Zod schemas and errors
7
+ - [interpret](docs/util/interpret.md) - Utilities for formatting Zod errors as strings
8
+ - [typeGuards](docs/util/typeGuards.md) - Type guards for Zod-related values
9
+ - [validate](docs/util/validate.md) - Schema-wrapping factories for creating validators
10
+
11
+ ## Peer Dependencies
12
+ - `zod^4.3.6`
13
+
14
+ ## Commands
15
+ The following commands exist in the project:
16
+
17
+ - `npm run uninstall` - Uninstalls all dependencies for the library
18
+ - `npm run reinstall` - Uninstalls and then Reinstalls all dependencies for the library
19
+ - `npm run build` - Builds the library
20
+ - `npm run release` - Publishes the library to npm without changing the version
21
+ - `npm run release-patch` - Publishes the library to npm with a patch version bump
22
+ - `npm run release-minor` - Publishes the library to npm with a minor version bump
23
+ - `npm run release-major` - Publishes the library to npm with a major version bump
24
+ - `npm run test` - Runs the tests for the library
25
+ - `npm run test:coverage` - Runs the tests for the library and generates a coverage report
26
+ - `npm run test:watch` - Runs the tests for the library in watch mode
27
+ - `npm run typecheck` - Identifies any type errors in the library
28
+
29
+ ## Testing Guide
30
+
31
+ This library uses [Jest](https://jestjs.io/) with [ts-jest](https://kulshekhar.github.io/ts-jest/) for testing TypeScript code.
32
+
33
+ ### Running Tests
34
+
35
+ ```bash
36
+ # Run all tests
37
+ npm test
38
+
39
+ # Run tests in watch mode (re-runs on file changes)
40
+ npm run test:watch
41
+
42
+ # Run tests with coverage report
43
+ npm run test:coverage
44
+ ```
45
+
46
+ Test files are located in `./test` and any `__tests__` directories within the `./src` directory. Generally, test files should be named with a `.test.ts` suffix.
@@ -0,0 +1,4 @@
1
+ export * from './types';
2
+ export * from './typeGuards';
3
+ export * from './interpret';
4
+ export * from './validate';
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./typeGuards"), exports);
19
+ __exportStar(require("./interpret"), exports);
20
+ __exportStar(require("./validate"), exports);
@@ -0,0 +1,2 @@
1
+ import { ZodError } from 'zod';
2
+ export declare function interpretZodError(err: ZodError, prefix?: PropertyKey | PropertyKey[]): string;
@@ -0,0 +1,49 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.interpretZodError = interpretZodError;
37
+ const zod_1 = __importStar(require("zod"));
38
+ function interpretZodError(err, prefix = "") {
39
+ if (!Array.isArray(prefix))
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
+ }));
48
+ return zod_1.default.prettifyError(modifiedErr);
49
+ }
@@ -0,0 +1,2 @@
1
+ import { ZodError } from 'zod';
2
+ export declare function isZodError(err: unknown): err is ZodError;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isZodError = isZodError;
4
+ const zod_1 = require("zod");
5
+ function isZodError(err) {
6
+ return !!err && (err instanceof zod_1.ZodError || (err instanceof Error && err.name === 'ZodError'));
7
+ }
@@ -0,0 +1,20 @@
1
+ import { ZodOptional, ZodSafeParseResult, ZodType } from 'zod';
2
+ export type ZodSafeParseable<O> = {
3
+ safeParse: (data: unknown) => ZodSafeParseResult<O>;
4
+ };
5
+ export type ZodValidator<O> = (v: unknown) => v is O;
6
+ export type ZodValidationResult<O> = {
7
+ success: true;
8
+ value: O;
9
+ error?: never;
10
+ } | {
11
+ success: false;
12
+ value?: never;
13
+ error: string;
14
+ };
15
+ export type ZodValidatorWithErrors<O> = (v: unknown) => ZodValidationResult<O>;
16
+ export type ZodValidateWithErrorsOptions = {
17
+ _throw?: boolean;
18
+ prefix?: string | string[];
19
+ };
20
+ export type MaybeZodOptional<ZT extends ZodType> = ZT | ZodOptional<ZT>;
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import * as t from './types';
2
+ export declare function zodValidate<O>(p: t.ZodSafeParseable<O>): t.ZodValidator<O>;
3
+ export declare function zodValidateWithErrors<O>(p: t.ZodSafeParseable<O>, { _throw, prefix, }?: t.ZodValidateWithErrorsOptions): t.ZodValidatorWithErrors<O>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zodValidate = zodValidate;
4
+ exports.zodValidateWithErrors = zodValidateWithErrors;
5
+ const interpret_1 = require("./interpret");
6
+ function zodValidate(p) {
7
+ return (v) => {
8
+ const result = p.safeParse(v);
9
+ return result.success;
10
+ };
11
+ }
12
+ function zodValidateWithErrors(p, { _throw = false, prefix = "", } = {}) {
13
+ return (_v) => {
14
+ const { success, error, data: v } = p.safeParse(_v);
15
+ if (success)
16
+ return { success, value: v };
17
+ if (_throw)
18
+ throw error;
19
+ return { success, error: (0, interpret_1.interpretZodError)(error, prefix) };
20
+ };
21
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@ptolemy2002/zod-utils",
3
+ "version": "1.0.0",
4
+ "private": false,
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "/dist"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/Ptolemy2002/zod-utils",
13
+ "directory": "lib"
14
+ },
15
+ "scripts": {
16
+ "prepare": "npx ts-patch install -s",
17
+ "build": "bash ./scripts/build.sh",
18
+ "_build": "tsc --project ./tsconfig.build.json",
19
+ "typecheck": "tsc --noEmit",
20
+ "test": "jest",
21
+ "test:watch": "jest --watch",
22
+ "test:coverage": "jest --coverage",
23
+ "postinstall": "npx typesync",
24
+ "uninstall": "bash ./scripts/uninstall.sh",
25
+ "reinstall": "bash ./scripts/reinstall.sh",
26
+ "release": "bash ./scripts/release.sh",
27
+ "release-patch": "bash ./scripts/release.sh patch",
28
+ "release-minor": "bash ./scripts/release.sh minor",
29
+ "release-major": "bash ./scripts/release.sh major"
30
+ },
31
+
32
+ "peerDependencies": {
33
+ "zod": "^4.3.6"
34
+ },
35
+
36
+ "devDependencies": {
37
+ "@types/jest": "^29.5.0",
38
+ "@types/node": "^25.3.5",
39
+ "jest": "^29.5.0",
40
+ "ts-jest": "^29.1.0",
41
+ "ts-patch": "^3.3.0",
42
+ "tsconfig-paths": "^4.2.0",
43
+ "typescript-transform-paths": "^3.5.3",
44
+ "zod": "^4.3.6"
45
+ }
46
+ }