@ptolemy2002/regex-utils 2.2.4 → 2.4.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
@@ -25,6 +25,17 @@ export type ZodSafeParseable<O> = {
25
25
  };
26
26
  ```
27
27
 
28
+ ## Classes
29
+ The following classes are available in the library:
30
+
31
+ ### ZodInterpretedError
32
+ #### Description
33
+ 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
+
35
+ #### 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'`.
38
+
28
39
  ## Functions
29
40
  The following functions are available in the library:
30
41
 
@@ -134,6 +145,7 @@ Given a zod error, interprets it to `null` if no error is found, a single error
134
145
 
135
146
  #### Parameters
136
147
  - `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.
137
149
 
138
150
  #### Returns
139
151
  `string | string[] | null` - The interpreted error message(s).
@@ -154,6 +166,8 @@ This is a simple function that takes a zod schema, returning a function that tak
154
166
 
155
167
  #### Parameters
156
168
  - `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'`.
157
171
 
158
172
  #### Returns
159
173
  `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,17 @@ 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 declare function interpretZodError(e: ZodError, prefix?: string | string[]): string | string[] | null;
21
21
  export type ZodSafeParseable<O> = {
22
22
  safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
23
23
  };
24
24
  export declare function zodValidate<O>(p: ZodSafeParseable<O>): ZodValidator<O>;
25
- export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>): ZodValidatorWithErrors<O>;
25
+ export declare class ZodInterpretedError extends Error {
26
+ zodMessage: string | string[] | null;
27
+ joinSeparator: string;
28
+ constructor(message?: string | string[] | null, joinSeparator?: string);
29
+ }
30
+ export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>, _throw?: boolean, joinSeparator?: string): ZodValidatorWithErrors<O>;
26
31
  export declare function isAlphanumeric(str: string): boolean;
27
32
  export declare function toAlphanumeric(str: string, separator?: string): string;
28
33
  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,10 +111,17 @@ 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) {
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
127
  return `${path.join(".")}: ${message}`;
@@ -131,12 +138,23 @@ function zodValidate(p) {
131
138
  return result.success;
132
139
  };
133
140
  }
134
- function zodValidateWithErrors(p) {
141
+ class ZodInterpretedError extends Error {
142
+ constructor(message = null, joinSeparator = "\n") {
143
+ super(Array.isArray(message) ? message.join(joinSeparator) : message ?? undefined);
144
+ this.zodMessage = message;
145
+ this.joinSeparator = joinSeparator;
146
+ }
147
+ }
148
+ exports.ZodInterpretedError = ZodInterpretedError;
149
+ function zodValidateWithErrors(p, _throw = false, joinSeparator = "\n") {
135
150
  return (v) => {
136
151
  const result = p.safeParse(v);
137
152
  if (result.success)
138
153
  return true;
139
- return interpretZodError(result.error);
154
+ const error = interpretZodError(result.error);
155
+ if (_throw)
156
+ throw new ZodInterpretedError(error, joinSeparator);
157
+ return error;
140
158
  };
141
159
  }
142
160
  const alphanumericPattern = /[\w_-]+/i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.2.4",
3
+ "version": "2.4.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",