@ptolemy2002/regex-utils 2.5.0 → 2.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.
package/README.md CHANGED
@@ -153,7 +153,7 @@ Checks if a string is a valid set of regular expression flags.
153
153
 
154
154
  ### interpretZodError
155
155
  #### Description
156
- Given a zod error, interprets it to `null` if no error is found, a single error message if there is a single error, or an array of error messages if there are multiple errors. The error messages will be in the format `<path>: <message>`.
156
+ Given a zod error, interprets it to `null` if no error is found, a single error message if there is a single error, or an array of error messages if there are multiple errors. The error messages will be in the format `<path>: <message>`. If the error is with function arguments, "arguments.<index>" will be appended to the end of the path. Similarly, if the error is with a function return value, "returnValue" will be appended to the end of the path. Note that for function arguments, only the first seen error will be reported.
157
157
 
158
158
  #### Parameters
159
159
  - `e` (`ZodError`): The zod error to be interpreted.
package/dist/index.d.ts CHANGED
@@ -21,7 +21,7 @@ export type InterpretZodErrorOptions = {
21
21
  prefix?: string | string[];
22
22
  separator?: string;
23
23
  };
24
- export declare function interpretZodError(e: ZodError, { prefix, separator }: InterpretZodErrorOptions): string | string[] | null;
24
+ export declare function interpretZodError(e: ZodError, { prefix, separator }?: InterpretZodErrorOptions): string | string[] | null;
25
25
  export type ZodSafeParseable<O> = {
26
26
  safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
27
27
  };
package/dist/index.js CHANGED
@@ -111,10 +111,10 @@ function isValidRegex(value, flags = "") {
111
111
  function isValidRegexFlags(value) {
112
112
  return /^[gimsuy]*$/.test(value);
113
113
  }
114
- function interpretZodError(e, { prefix, separator = "." }) {
114
+ function interpretZodError(e, { prefix, separator = "." } = {}) {
115
115
  const { errors } = e;
116
116
  function formatIssue(issue) {
117
- const { path: _path, message } = issue;
117
+ const { code, path: _path, message } = issue;
118
118
  let path = _path;
119
119
  if (typeof prefix === "string") {
120
120
  path = [prefix, ...path];
@@ -122,6 +122,16 @@ function interpretZodError(e, { prefix, separator = "." }) {
122
122
  else if (Array.isArray(prefix)) {
123
123
  path = [...prefix, ...path];
124
124
  }
125
+ if (code === "invalid_return_type") {
126
+ const returnTypeIssue = issue.returnTypeError.errors[0];
127
+ path = [...path, "returnType", ...returnTypeIssue.path];
128
+ return `${path.join(separator)}: ${returnTypeIssue.message}`;
129
+ }
130
+ else if (code === "invalid_arguments") {
131
+ const argumentsIssue = issue.argumentsError.errors[0];
132
+ path = [...path, "arguments", ...argumentsIssue.path];
133
+ return `${path.join(separator)}: ${argumentsIssue.message}`;
134
+ }
125
135
  if (path.length === 0)
126
136
  return message;
127
137
  return `${path.join(separator)}: ${message}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",