@ptolemy2002/regex-utils 2.4.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
@@ -18,11 +18,21 @@ type TransformRegexOptions = {
18
18
  accentInsensitive?: boolean;
19
19
  matchWhole?: boolean;
20
20
  };
21
- export type ZodValidator<O> = (v: O) => boolean;
22
- export type ZodValidatorWithErrors<O> = (v: O) => true | string | string[];
23
- export type ZodSafeParseable<O> = {
21
+ type ZodValidator<O> = (v: O) => boolean;
22
+ type ZodValidatorWithErrors<O> = (v: O) => true | string | string[];
23
+ type ZodSafeParseable<O> = {
24
24
  safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
25
25
  };
26
+ type InterpretZodErrorOptions = {
27
+ prefix?: string | string[];
28
+ separator?: string;
29
+ };
30
+ type ZodValidateWithErrorsOptions = {
31
+ _throw?: boolean;
32
+ prefix?: string | string[];
33
+ joinSeparator?: string;
34
+ pathSeparator?: string;
35
+ };
26
36
  ```
27
37
 
28
38
  ## Classes
@@ -33,8 +43,10 @@ The following classes are available in the library:
33
43
  A custom error class that is used to represent errors that have been interpreted from a zod error. It takes in either a single error or an array of errors, combining them in the `message` property, but keeping the original errors in the `zodMessage` property.
34
44
 
35
45
  #### Properties
36
- - `zodMessage` (`string | string[] | null`): The interpreted error message(s). If not specified, this will be `null`, but the `message` property itself will be `undefined`.
37
- - `joinSeparator?` (`string`): The separator to be used when joining multiple errors. Default is `'\n'`.
46
+ - `zodMessage` (`string | string[] | null`): The interpreted error message(s). If not specified, this will be `null`, but the `message` property itself will be `undefined`. The class can be constructed with a `ZodError` object to automatically interpret the error.
47
+ - `prefix` (`string | string[]`): A prefix to be added to the path. If an array is passed, the elements will be joined by `pathSeparator`. If not specified, the path will be used as is.
48
+ - `separator` (`string`): The separator to be used when joining multiple errors. Default is `'\n'`.
49
+
38
50
 
39
51
  ## Functions
40
52
  The following functions are available in the library:
@@ -141,11 +153,13 @@ Checks if a string is a valid set of regular expression flags.
141
153
 
142
154
  ### interpretZodError
143
155
  #### Description
144
- 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.
145
157
 
146
158
  #### Parameters
147
159
  - `e` (`ZodError`): The zod error to be interpreted.
148
- - `prefix?` (`string | string[]`): A prefix to be added to the path. If an array is passed, the elements will be joined by a period. If not specified, the path will be used as is.
160
+ - `options` (`InterpretZodErrorOptions`): Used to specify optional arguments.
161
+ - `prefix?` (`string | string[]`): A prefix to be added to the path. If an array is passed, the elements will be joined by `separator`. If not specified, the path will be used as is.
162
+ - `separator` (`string`): The separator to be used when joining the path nodes. Default is `'.'`.
149
163
 
150
164
  #### Returns
151
165
  `string | string[] | null` - The interpreted error message(s).
@@ -166,8 +180,10 @@ This is a simple function that takes a zod schema, returning a function that tak
166
180
 
167
181
  #### Parameters
168
182
  - `p` (`ZodSafeParseable<O>`): The zod schema to be used for validation.
169
- - `_throw` (`boolean`): Whether to throw a `ZodInterpretedError` if the value does not match the schema. If true, errors will be delimited by the `joinSeparator` value. Default is `false`.
170
- - `joinSeparator` (`string`): The separator to be used when joining multiple errors. Default is `'\n'`.
183
+ - `options` (`ZodValidateWithErrorsOptions`): Used to specify optional arguments.
184
+ - `_throw` (`boolean`): Whether to throw a `ZodInterpretedError` if the value does not match the schema. If true, errors will be delimited by the `joinSeparator` value. Default is `false`.
185
+ - `prefix?` (`string | string[]`): A prefix to be added to the path. If an array is passed, the elements will be joined by a period. If not specified, the path will be used as is.
186
+ - `joinSeparator` (`string`): The separator to be used when joining multiple errors. Default is `'\n'`.
171
187
 
172
188
  #### Returns
173
189
  `ZodValidatorWithErrors<O>` - A function that takes a value and returns `true` if the value matches the schema, an error message if there is a single error, or an array of error messages if there are multiple errors.
package/dist/index.d.ts CHANGED
@@ -17,17 +17,28 @@ export declare function isValidRegex(value: string, flags?: string): boolean;
17
17
  export declare function isValidRegexFlags(value: string): boolean;
18
18
  export type ZodValidator<O> = (v: O) => boolean;
19
19
  export type ZodValidatorWithErrors<O> = (v: O) => true | string | string[];
20
- export declare function interpretZodError(e: ZodError, prefix?: string | string[]): string | string[] | null;
20
+ export type InterpretZodErrorOptions = {
21
+ prefix?: string | string[];
22
+ separator?: string;
23
+ };
24
+ export declare function interpretZodError(e: ZodError, { prefix, separator }?: InterpretZodErrorOptions): string | string[] | null;
21
25
  export type ZodSafeParseable<O> = {
22
26
  safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
23
27
  };
24
28
  export declare function zodValidate<O>(p: ZodSafeParseable<O>): ZodValidator<O>;
29
+ export type ZodValidateWithErrorsOptions = {
30
+ _throw?: boolean;
31
+ prefix?: string | string[];
32
+ joinSeparator?: string;
33
+ pathSeparator?: string;
34
+ };
25
35
  export declare class ZodInterpretedError extends Error {
26
36
  zodMessage: string | string[] | null;
37
+ prefix: string | string[] | undefined;
27
38
  joinSeparator: string;
28
- constructor(message?: string | string[] | null, joinSeparator?: string);
39
+ constructor(message?: ZodError | string | string[] | null, { prefix, joinSeparator, pathSeparator }?: ZodValidateWithErrorsOptions);
29
40
  }
30
- export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>, _throw?: boolean, joinSeparator?: string): ZodValidatorWithErrors<O>;
41
+ export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>, { _throw, prefix, joinSeparator, pathSeparator }?: ZodValidateWithErrorsOptions): ZodValidatorWithErrors<O>;
31
42
  export declare function isAlphanumeric(str: string): boolean;
32
43
  export declare function toAlphanumeric(str: string, separator?: string): string;
33
44
  export declare function isValidEmail(v: string): boolean;
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) {
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,9 +122,19 @@ function interpretZodError(e, prefix) {
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
- return `${path.join(".")}: ${message}`;
137
+ return `${path.join(separator)}: ${message}`;
128
138
  }
129
139
  if (errors.length === 0)
130
140
  return null;
@@ -139,22 +149,25 @@ function zodValidate(p) {
139
149
  };
140
150
  }
141
151
  class ZodInterpretedError extends Error {
142
- constructor(message = null, joinSeparator = "\n") {
152
+ constructor(message = null, { prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
153
+ if (message instanceof zod_1.ZodError)
154
+ message = interpretZodError(message, { prefix, separator: pathSeparator });
143
155
  super(Array.isArray(message) ? message.join(joinSeparator) : message ?? undefined);
144
156
  this.zodMessage = message;
157
+ this.prefix = prefix;
145
158
  this.joinSeparator = joinSeparator;
146
159
  }
147
160
  }
148
161
  exports.ZodInterpretedError = ZodInterpretedError;
149
- function zodValidateWithErrors(p, _throw = false, joinSeparator = "\n") {
162
+ function zodValidateWithErrors(p, { _throw = false, prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
150
163
  return (v) => {
151
- const result = p.safeParse(v);
152
- if (result.success)
164
+ const { success, error: error } = p.safeParse(v);
165
+ if (success)
153
166
  return true;
154
- const error = interpretZodError(result.error);
167
+ const zError = new ZodInterpretedError(error, { prefix, joinSeparator, pathSeparator });
155
168
  if (_throw)
156
- throw new ZodInterpretedError(error, joinSeparator);
157
- return error;
169
+ throw zError;
170
+ return zError.zodMessage ?? "";
158
171
  };
159
172
  }
160
173
  const alphanumericPattern = /[\w_-]+/i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",