@ptolemy2002/regex-utils 2.0.0 → 2.1.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
@@ -125,6 +125,16 @@ Checks if a string is a valid set of regular expression flags.
125
125
  #### Returns
126
126
  `boolean` - `true` if the string is a valid set of regular expression flags, `false` otherwise.
127
127
 
128
+ ### interpretZodError
129
+ #### Description
130
+ 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.
131
+
132
+ #### Parameters
133
+ - `e` (`ZodError`): The zod error to be interpreted.
134
+
135
+ #### Returns
136
+ `string | string[] | null` - The interpreted error message(s).
137
+
128
138
  ### zodValidate<O>
129
139
  #### Description
130
140
  This is a simple function that takes a zod schema, returning a function that takes a value. If the value matches, the function returns `true`. Otherwise, it returns `false`. `O` refers to the output type of the zod schema, which should be the same as its input type.
@@ -137,7 +147,7 @@ This is a simple function that takes a zod schema, returning a function that tak
137
147
 
138
148
  ### zodValidateWithErrors<O>
139
149
  #### Description
140
- This is a simple function that takes a zod schema, returning a function that takes a value. If the value matches, the function returns `true`. Otherwise, it returns a single error message or an array of error messages if there are multiple errors. `O` refers to the output type of the zod schema, which should be the same as its input type.
150
+ This is a simple function that takes a zod schema, returning a function that takes a value. If the value matches, the function returns `true`. Otherwise, it returns the result of `interpretZodError` on the error. `O` refers to the output type of the zod schema, which should be the same as its input type.
141
151
 
142
152
  #### Parameters
143
153
  - `p` (`ZodSchema<O>`): The zod schema to be used for validation.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ZodSchema } from "zod";
1
+ import { ZodError, ZodSchema } from "zod";
2
2
  export type RegexInput = string | RegExp;
3
3
  export declare function combineFlags(...flags: string[]): string;
4
4
  export declare function escapeRegex(value: RegexInput, flags?: string): RegExp;
@@ -12,11 +12,12 @@ export type TransformRegexOptions = {
12
12
  accentInsensitive?: boolean;
13
13
  matchWhole?: boolean;
14
14
  };
15
- export declare function transformRegex(value: RegexInput, { flags, accentInsensitive, caseInsensitive, matchWhole }: TransformRegexOptions): RegExp;
15
+ export declare function transformRegex(value: RegexInput, { flags, accentInsensitive, caseInsensitive, matchWhole }?: TransformRegexOptions): RegExp;
16
16
  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
21
  export declare function zodValidate<O>(p: ZodSchema<O>): ZodValidator<O>;
21
22
  export declare function zodValidateWithErrors<O>(p: ZodSchema<O>): ZodValidatorWithErrors<O>;
22
23
  export declare function isAlphanumeric(str: string): boolean;
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ exports.regexMatchWhole = regexMatchWhole;
9
9
  exports.transformRegex = transformRegex;
10
10
  exports.isValidRegex = isValidRegex;
11
11
  exports.isValidRegexFlags = isValidRegexFlags;
12
+ exports.interpretZodError = interpretZodError;
12
13
  exports.zodValidate = zodValidate;
13
14
  exports.zodValidateWithErrors = zodValidateWithErrors;
14
15
  exports.isAlphanumeric = isAlphanumeric;
@@ -82,7 +83,7 @@ function regexMatchWhole(value, flags = "") {
82
83
  return regexMatchWhole(value.source, combineFlags(value.flags, flags));
83
84
  }
84
85
  }
85
- function transformRegex(value, { flags = "", accentInsensitive = false, caseInsensitive = false, matchWhole = false }) {
86
+ function transformRegex(value, { flags = "", accentInsensitive = false, caseInsensitive = false, matchWhole = false } = {}) {
86
87
  if (accentInsensitive)
87
88
  value = regexAccentInsensitive(value, flags);
88
89
  if (caseInsensitive)
@@ -109,6 +110,14 @@ function isValidRegex(value, flags = "") {
109
110
  function isValidRegexFlags(value) {
110
111
  return /^[gimsuy]*$/.test(value);
111
112
  }
113
+ function interpretZodError(e) {
114
+ const { errors } = e;
115
+ if (errors.length === 0)
116
+ return null;
117
+ if (errors.length === 1)
118
+ return e.errors[0].message;
119
+ return errors.map((e) => e.message);
120
+ }
112
121
  function zodValidate(p) {
113
122
  return (v) => {
114
123
  const result = p.safeParse(v);
@@ -120,9 +129,7 @@ function zodValidateWithErrors(p) {
120
129
  const result = p.safeParse(v);
121
130
  if (result.success)
122
131
  return true;
123
- if (result.error.errors.length === 1)
124
- return result.error.errors[0].message;
125
- return result.error.errors.map((e) => e.message);
132
+ return interpretZodError(result.error);
126
133
  };
127
134
  }
128
135
  const alphanumericPattern = /[\w_-]+/i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",