@ptolemy2002/regex-utils 2.3.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,13 +18,36 @@ 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
 
38
+ ## Classes
39
+ The following classes are available in the library:
40
+
41
+ ### ZodInterpretedError
42
+ #### Description
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.
44
+
45
+ #### Properties
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
+
50
+
28
51
  ## Functions
29
52
  The following functions are available in the library:
30
53
 
@@ -134,6 +157,9 @@ Given a zod error, interprets it to `null` if no error is found, a single error
134
157
 
135
158
  #### Parameters
136
159
  - `e` (`ZodError`): The zod error to be interpreted.
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 `'.'`.
137
163
 
138
164
  #### Returns
139
165
  `string | string[] | null` - The interpreted error message(s).
@@ -154,7 +180,10 @@ This is a simple function that takes a zod schema, returning a function that tak
154
180
 
155
181
  #### Parameters
156
182
  - `p` (`ZodSafeParseable<O>`): The zod schema to be used for validation.
157
- - `_throw` (`boolean`): Whether to throw an error if the value does not match the schema. If true, errors will be delimited by newlines. Default is `false`.
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'`.
158
187
 
159
188
  #### Returns
160
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,12 +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): 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>;
25
- export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>, _throw?: boolean): ZodValidatorWithErrors<O>;
29
+ export type ZodValidateWithErrorsOptions = {
30
+ _throw?: boolean;
31
+ prefix?: string | string[];
32
+ joinSeparator?: string;
33
+ pathSeparator?: string;
34
+ };
35
+ export declare class ZodInterpretedError extends Error {
36
+ zodMessage: string | string[] | null;
37
+ prefix: string | string[] | undefined;
38
+ joinSeparator: string;
39
+ constructor(message?: ZodError | string | string[] | null, { prefix, joinSeparator, pathSeparator }?: ZodValidateWithErrorsOptions);
40
+ }
41
+ export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>, { _throw, prefix, joinSeparator, pathSeparator }?: ZodValidateWithErrorsOptions): ZodValidatorWithErrors<O>;
26
42
  export declare function isAlphanumeric(str: string): boolean;
27
43
  export declare function toAlphanumeric(str: string, separator?: string): string;
28
44
  export declare function isValidEmail(v: string): boolean;
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum = void 0;
3
+ exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum = exports.ZodInterpretedError = void 0;
4
4
  exports.combineFlags = combineFlags;
5
5
  exports.escapeRegex = escapeRegex;
6
6
  exports.regexAccentInsensitive = regexAccentInsensitive;
@@ -111,13 +111,20 @@ function isValidRegex(value, flags = "") {
111
111
  function isValidRegexFlags(value) {
112
112
  return /^[gimsuy]*$/.test(value);
113
113
  }
114
- function interpretZodError(e) {
114
+ function interpretZodError(e, { prefix, separator = "." }) {
115
115
  const { errors } = e;
116
116
  function formatIssue(issue) {
117
- const { path, message } = issue;
117
+ const { path: _path, message } = issue;
118
+ let path = _path;
119
+ if (typeof prefix === "string") {
120
+ path = [prefix, ...path];
121
+ }
122
+ else if (Array.isArray(prefix)) {
123
+ path = [...prefix, ...path];
124
+ }
118
125
  if (path.length === 0)
119
126
  return message;
120
- return `${path.join(".")}: ${message}`;
127
+ return `${path.join(separator)}: ${message}`;
121
128
  }
122
129
  if (errors.length === 0)
123
130
  return null;
@@ -131,21 +138,26 @@ function zodValidate(p) {
131
138
  return result.success;
132
139
  };
133
140
  }
134
- function zodValidateWithErrors(p, _throw = false) {
141
+ class ZodInterpretedError extends Error {
142
+ constructor(message = null, { prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
143
+ if (message instanceof zod_1.ZodError)
144
+ message = interpretZodError(message, { prefix, separator: pathSeparator });
145
+ super(Array.isArray(message) ? message.join(joinSeparator) : message ?? undefined);
146
+ this.zodMessage = message;
147
+ this.prefix = prefix;
148
+ this.joinSeparator = joinSeparator;
149
+ }
150
+ }
151
+ exports.ZodInterpretedError = ZodInterpretedError;
152
+ function zodValidateWithErrors(p, { _throw = false, prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
135
153
  return (v) => {
136
- const result = p.safeParse(v);
137
- if (result.success)
154
+ const { success, error: error } = p.safeParse(v);
155
+ if (success)
138
156
  return true;
139
- const error = interpretZodError(result.error);
140
- if (_throw) {
141
- if (Array.isArray(error)) {
142
- throw new Error(error.join("\n"));
143
- }
144
- else {
145
- throw new Error(error);
146
- }
147
- }
148
- return error;
157
+ const zError = new ZodInterpretedError(error, { prefix, joinSeparator, pathSeparator });
158
+ if (_throw)
159
+ throw zError;
160
+ return zError.zodMessage ?? "";
149
161
  };
150
162
  }
151
163
  const alphanumericPattern = /[\w_-]+/i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.3.0",
3
+ "version": "2.5.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",