@ptolemy2002/zod-utils 1.4.0 → 1.6.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.
@@ -1,5 +1,6 @@
1
1
  import z, { ZodArray, ZodType, ZodUnknown } from "zod";
2
2
  import { $ZodFunctionArgs, $ZodFunctionOut } from "zod/v4/core";
3
+ import { ZodPath } from "./types";
3
4
  export type TrialErrorMode = "allow" | "forbid" | "require" | ((e: unknown) => boolean) | {
4
5
  require: (e: unknown) => boolean;
5
6
  };
@@ -13,8 +14,8 @@ export type FunctionTrial<Input extends unknown[]> = {
13
14
  export type ZodFunctionParseOptions<In extends $ZodFunctionArgs, Out extends $ZodFunctionOut> = {
14
15
  input?: In;
15
16
  output?: Out;
16
- inputPath?: PropertyKey | PropertyKey[];
17
- outputPath?: PropertyKey | PropertyKey[];
17
+ inputPath?: ZodPath;
18
+ outputPath?: ZodPath;
18
19
  };
19
20
  export type ZodFunctionSchemaOptions<In extends $ZodFunctionArgs, Out extends $ZodFunctionOut> = {
20
21
  trials?: FunctionTrial<z.infer<In>>[];
package/dist/function.js CHANGED
@@ -101,7 +101,7 @@ function zodFunctionSchema({ trials = [], inputPath = "args", outputPath = "retu
101
101
  const id = (_a = trial.id) !== null && _a !== void 0 ? _a : `trial_${i}`;
102
102
  const { outputSchema = zod_1.default.unknown(), error, errorStringify = (e) => {
103
103
  if ((0, typeGuards_1.isZodError)(e))
104
- return (0, interpret_1.interpretZodError)(e);
104
+ return (0, interpret_1.interpretZodError)(e, { multiline: false });
105
105
  if (e instanceof Error)
106
106
  return e.message;
107
107
  return String(e);
@@ -1,3 +1,8 @@
1
- import { ZodError } from 'zod';
2
- import { $ZodError } from 'zod/v4/core';
3
- export declare function interpretZodError(err: ZodError | $ZodError, prefix?: PropertyKey | PropertyKey[]): string;
1
+ import { InterpretableZodError, InterpretableZodIssue, ZodPath } from './types';
2
+ export type InterpretZodErrorOptions = {
3
+ prefix?: ZodPath;
4
+ multiline?: boolean;
5
+ includeCode?: boolean;
6
+ };
7
+ export declare function interpretZodError(err: InterpretableZodError, options?: ZodPath | InterpretZodErrorOptions): string;
8
+ export declare function interpretZodIssue(issue: InterpretableZodIssue, options?: ZodPath | InterpretZodErrorOptions): string;
package/dist/interpret.js CHANGED
@@ -1,42 +1,34 @@
1
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
- })();
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
35
5
  Object.defineProperty(exports, "__esModule", { value: true });
36
6
  exports.interpretZodError = interpretZodError;
37
- const zod_1 = __importStar(require("zod"));
7
+ exports.interpretZodIssue = interpretZodIssue;
8
+ const zod_1 = __importDefault(require("zod"));
38
9
  const prefixIssuePath_1 = require("./prefixIssuePath");
39
- function interpretZodError(err, prefix = "") {
40
- const modifiedErr = new zod_1.ZodError(err.issues.map(issue => (0, prefixIssuePath_1.prefixZodIssuePath)(issue, prefix)));
41
- return zod_1.default.prettifyError(modifiedErr);
10
+ function interpretZodError(err, options = "") {
11
+ if (typeof options !== "object" || Array.isArray(options))
12
+ options = { prefix: options };
13
+ const { multiline = true, includeCode = false, prefix = "" } = options;
14
+ const modifiedErr = {
15
+ issues: err.issues.map(issue => {
16
+ const modifiedIssue = (0, prefixIssuePath_1.prefixZodIssuePath)(issue, prefix);
17
+ if (includeCode && modifiedIssue.code) {
18
+ return {
19
+ ...modifiedIssue,
20
+ message: `[${modifiedIssue.code}] ${modifiedIssue.message}`
21
+ };
22
+ }
23
+ return modifiedIssue;
24
+ })
25
+ };
26
+ let result = zod_1.default.prettifyError(modifiedErr);
27
+ if (!multiline) {
28
+ result = result.replace(/\n\s*→/g, " →");
29
+ }
30
+ return result;
31
+ }
32
+ function interpretZodIssue(issue, options = "") {
33
+ return interpretZodError({ issues: [issue] }, options);
42
34
  }
@@ -1 +1,2 @@
1
- export declare function issuePathStartsWith(issuePath: PropertyKey[], prefix: PropertyKey | PropertyKey[]): boolean;
1
+ import { ZodPath } from "./types";
2
+ export declare function issuePathStartsWith(issuePath: PropertyKey[], prefix: ZodPath): boolean;
@@ -1,2 +1,2 @@
1
- import z from "zod";
2
- export declare function prefixZodIssuePath(issue: z.core.$ZodIssue, prefix: PropertyKey | PropertyKey[]): z.core.$ZodIssue;
1
+ import { InterpretableZodIssue, ZodPath } from "./types";
2
+ export declare function prefixZodIssuePath<I extends InterpretableZodIssue>(issue: I, prefix: ZodPath): I;
@@ -2,9 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.prefixZodIssuePath = prefixZodIssuePath;
4
4
  function prefixZodIssuePath(issue, prefix) {
5
+ var _a;
5
6
  if (!Array.isArray(prefix))
6
7
  prefix = [prefix];
7
- const newPath = [...prefix, ...issue.path]
8
+ const newPath = [...prefix, ...((_a = issue.path) !== null && _a !== void 0 ? _a : [])]
8
9
  .filter(p => typeof p !== "string" || p.length > 0);
9
10
  if (newPath.length === 0)
10
11
  newPath.push("(root)");
package/dist/types.d.ts CHANGED
@@ -18,3 +18,12 @@ export type ZodValidateWithErrorsOptions = {
18
18
  prefix?: string | string[];
19
19
  };
20
20
  export type MaybeZodOptional<ZT extends ZodType> = ZT | ZodOptional<ZT>;
21
+ export type ZodPath = PropertyKey | PropertyKey[];
22
+ export type InterpretableZodIssue = Readonly<{
23
+ code?: string;
24
+ message: string;
25
+ path?: PropertyKey[];
26
+ }>;
27
+ export type InterpretableZodError = Readonly<{
28
+ issues: InterpretableZodIssue[];
29
+ }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/zod-utils",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",