@ptolemy2002/regex-utils 2.1.0 → 2.2.1

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
@@ -127,7 +127,7 @@ Checks if a string is a valid set of regular expression flags.
127
127
 
128
128
  ### interpretZodError
129
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.
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. The error messages will be in the format `<path>: <message>`.
131
131
 
132
132
  #### Parameters
133
133
  - `e` (`ZodError`): The zod error to be interpreted.
@@ -216,6 +216,15 @@ Uses `zodValidate` to check if a string is a valid Social Security Number (SSN).
216
216
  #### Returns
217
217
  `boolean` - `true` if the string is a valid SSN, `false` otherwise.
218
218
 
219
+ ## Schemas
220
+ The following schemas are available in the library:
221
+
222
+ ### ZodCoercedBooleanEnum
223
+ A zod schema that specifies an enum of strings that can be interpreted as booleans. This is used within `ZodCoercedBoolean` to actually perform the coercion.
224
+
225
+ ### ZodCoercedBoolean
226
+ A zod schema that takes in a value accepted by `ZodCoercedBooleanEnum` and coerces it to a boolean. This is useful when you want to accept multiple values that can be interpreted as booleans.
227
+
219
228
  ## Peer Dependencies
220
229
  These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
221
230
  - zod: ^3.23.8
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ZodError, ZodSchema } from "zod";
1
+ import { z, 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;
@@ -26,3 +26,5 @@ export declare function isValidEmail(v: string): boolean;
26
26
  export declare function isValidURL(v: string): boolean;
27
27
  export declare function isValidPhoneNumber(v: string): boolean;
28
28
  export declare function isValidSSN(v: string): boolean;
29
+ export declare const ZodCoercedBooleanEnum: z.ZodEnum<["true", "false", "t", "f", "yes", "no", "y", "n", "1", "0", "on", "off"]>;
30
+ export declare const ZodCoercedBoolean: z.ZodEffects<z.ZodEnum<["true", "false", "t", "f", "yes", "no", "y", "n", "1", "0", "on", "off"]>, boolean, "0" | "1" | "true" | "false" | "t" | "f" | "yes" | "no" | "y" | "n" | "on" | "off">;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum = void 0;
3
4
  exports.combineFlags = combineFlags;
4
5
  exports.escapeRegex = escapeRegex;
5
6
  exports.regexAccentInsensitive = regexAccentInsensitive;
@@ -112,11 +113,17 @@ function isValidRegexFlags(value) {
112
113
  }
113
114
  function interpretZodError(e) {
114
115
  const { errors } = e;
116
+ function formatIssue(issue) {
117
+ const { path, message } = issue;
118
+ if (path.length === 0)
119
+ return message;
120
+ return `${path.join(".")}: ${message}`;
121
+ }
115
122
  if (errors.length === 0)
116
123
  return null;
117
124
  if (errors.length === 1)
118
- return e.errors[0].message;
119
- return errors.map((e) => e.message);
125
+ return formatIssue(errors[0]);
126
+ return errors.map((e) => formatIssue(e));
120
127
  }
121
128
  function zodValidate(p) {
122
129
  return (v) => {
@@ -157,3 +164,29 @@ function isValidPhoneNumber(v) {
157
164
  function isValidSSN(v) {
158
165
  return zodValidate(zod_1.z.coerce.string().regex(/^\d{3}-?\d{2}-?\d{4}$/))(v);
159
166
  }
167
+ exports.ZodCoercedBooleanEnum = zod_1.z.enum([
168
+ "true", "false",
169
+ "t", "f",
170
+ "yes", "no",
171
+ "y", "n",
172
+ "1", "0",
173
+ "on", "off"
174
+ ]);
175
+ exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum.transform((val) => {
176
+ switch (val) {
177
+ case "true":
178
+ case "t":
179
+ case "yes":
180
+ case "y":
181
+ case "1":
182
+ case "on":
183
+ return true;
184
+ case "false":
185
+ case "f":
186
+ case "no":
187
+ case "n":
188
+ case "0":
189
+ case "off":
190
+ return false;
191
+ }
192
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/regex-utils",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",