@ptolemy2002/regex-utils 2.4.0 → 2.5.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:
@@ -145,7 +157,9 @@ Given a zod error, interprets it to `null` if no error is found, a single error
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,7 +111,7 @@ 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
117
  const { path: _path, message } = issue;
@@ -124,7 +124,7 @@ function interpretZodError(e, prefix) {
124
124
  }
125
125
  if (path.length === 0)
126
126
  return message;
127
- return `${path.join(".")}: ${message}`;
127
+ return `${path.join(separator)}: ${message}`;
128
128
  }
129
129
  if (errors.length === 0)
130
130
  return null;
@@ -139,22 +139,25 @@ function zodValidate(p) {
139
139
  };
140
140
  }
141
141
  class ZodInterpretedError extends Error {
142
- constructor(message = null, joinSeparator = "\n") {
142
+ constructor(message = null, { prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
143
+ if (message instanceof zod_1.ZodError)
144
+ message = interpretZodError(message, { prefix, separator: pathSeparator });
143
145
  super(Array.isArray(message) ? message.join(joinSeparator) : message ?? undefined);
144
146
  this.zodMessage = message;
147
+ this.prefix = prefix;
145
148
  this.joinSeparator = joinSeparator;
146
149
  }
147
150
  }
148
151
  exports.ZodInterpretedError = ZodInterpretedError;
149
- function zodValidateWithErrors(p, _throw = false, joinSeparator = "\n") {
152
+ function zodValidateWithErrors(p, { _throw = false, prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
150
153
  return (v) => {
151
- const result = p.safeParse(v);
152
- if (result.success)
154
+ const { success, error: error } = p.safeParse(v);
155
+ if (success)
153
156
  return true;
154
- const error = interpretZodError(result.error);
157
+ const zError = new ZodInterpretedError(error, { prefix, joinSeparator, pathSeparator });
155
158
  if (_throw)
156
- throw new ZodInterpretedError(error, joinSeparator);
157
- return error;
159
+ throw zError;
160
+ return zError.zodMessage ?? "";
158
161
  };
159
162
  }
160
163
  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.5.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",