@ptolemy2002/zod-utils 1.2.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 CHANGED
@@ -5,6 +5,7 @@ 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
9
10
  - [prefixIssuePath](docs/util/prefixIssuePath.md) - Utility for prepending a path prefix to a Zod issue
10
11
  - [typeGuards](docs/util/typeGuards.md) - Type guards for Zod-related values
@@ -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>>;
@@ -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
@@ -1,3 +1,4 @@
1
+ export * from './function';
1
2
  export * from './types';
2
3
  export * from './typeGuards';
3
4
  export * from './interpret';
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ 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);
@@ -1,2 +1,3 @@
1
1
  import { ZodError } from 'zod';
2
- export declare function interpretZodError(err: ZodError, prefix?: PropertyKey | PropertyKey[]): string;
2
+ import { $ZodError } from 'zod/v4/core';
3
+ export declare function interpretZodError(err: ZodError | $ZodError, prefix?: PropertyKey | PropertyKey[]): string;
@@ -1,2 +1,3 @@
1
1
  import { ZodError } from 'zod';
2
- export declare function isZodError(err: unknown): err is ZodError;
2
+ import { $ZodError } from 'zod/v4/core';
3
+ export declare function isZodError(err: unknown): err is ZodError | $ZodError;
@@ -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.2.0",
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
  }