@ptolemy2002/regex-utils 2.0.1 → 2.2.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 +20 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +37 -3
- package/package.json +1 -1
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
|
|
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.
|
|
@@ -206,6 +216,15 @@ Uses `zodValidate` to check if a string is a valid Social Security Number (SSN).
|
|
|
206
216
|
#### Returns
|
|
207
217
|
`boolean` - `true` if the string is a valid SSN, `false` otherwise.
|
|
208
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
|
+
|
|
209
228
|
## Peer Dependencies
|
|
210
229
|
These should be installed in order to use the library, as npm does not automatically add peer dependencies to your project.
|
|
211
230
|
- zod: ^3.23.8
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { 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;
|
|
@@ -17,6 +17,7 @@ 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;
|
|
@@ -25,3 +26,5 @@ export declare function isValidEmail(v: string): boolean;
|
|
|
25
26
|
export declare function isValidURL(v: string): boolean;
|
|
26
27
|
export declare function isValidPhoneNumber(v: string): boolean;
|
|
27
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;
|
|
@@ -9,6 +10,7 @@ exports.regexMatchWhole = regexMatchWhole;
|
|
|
9
10
|
exports.transformRegex = transformRegex;
|
|
10
11
|
exports.isValidRegex = isValidRegex;
|
|
11
12
|
exports.isValidRegexFlags = isValidRegexFlags;
|
|
13
|
+
exports.interpretZodError = interpretZodError;
|
|
12
14
|
exports.zodValidate = zodValidate;
|
|
13
15
|
exports.zodValidateWithErrors = zodValidateWithErrors;
|
|
14
16
|
exports.isAlphanumeric = isAlphanumeric;
|
|
@@ -109,6 +111,14 @@ function isValidRegex(value, flags = "") {
|
|
|
109
111
|
function isValidRegexFlags(value) {
|
|
110
112
|
return /^[gimsuy]*$/.test(value);
|
|
111
113
|
}
|
|
114
|
+
function interpretZodError(e) {
|
|
115
|
+
const { errors } = e;
|
|
116
|
+
if (errors.length === 0)
|
|
117
|
+
return null;
|
|
118
|
+
if (errors.length === 1)
|
|
119
|
+
return e.errors[0].message;
|
|
120
|
+
return errors.map((e) => e.message);
|
|
121
|
+
}
|
|
112
122
|
function zodValidate(p) {
|
|
113
123
|
return (v) => {
|
|
114
124
|
const result = p.safeParse(v);
|
|
@@ -120,9 +130,7 @@ function zodValidateWithErrors(p) {
|
|
|
120
130
|
const result = p.safeParse(v);
|
|
121
131
|
if (result.success)
|
|
122
132
|
return true;
|
|
123
|
-
|
|
124
|
-
return result.error.errors[0].message;
|
|
125
|
-
return result.error.errors.map((e) => e.message);
|
|
133
|
+
return interpretZodError(result.error);
|
|
126
134
|
};
|
|
127
135
|
}
|
|
128
136
|
const alphanumericPattern = /[\w_-]+/i;
|
|
@@ -150,3 +158,29 @@ function isValidPhoneNumber(v) {
|
|
|
150
158
|
function isValidSSN(v) {
|
|
151
159
|
return zodValidate(zod_1.z.coerce.string().regex(/^\d{3}-?\d{2}-?\d{4}$/))(v);
|
|
152
160
|
}
|
|
161
|
+
exports.ZodCoercedBooleanEnum = zod_1.z.enum([
|
|
162
|
+
"true", "false",
|
|
163
|
+
"t", "f",
|
|
164
|
+
"yes", "no",
|
|
165
|
+
"y", "n",
|
|
166
|
+
"1", "0",
|
|
167
|
+
"on", "off"
|
|
168
|
+
]);
|
|
169
|
+
exports.ZodCoercedBoolean = exports.ZodCoercedBooleanEnum.transform((val) => {
|
|
170
|
+
switch (val) {
|
|
171
|
+
case "true":
|
|
172
|
+
case "t":
|
|
173
|
+
case "yes":
|
|
174
|
+
case "y":
|
|
175
|
+
case "1":
|
|
176
|
+
case "on":
|
|
177
|
+
return true;
|
|
178
|
+
case "false":
|
|
179
|
+
case "f":
|
|
180
|
+
case "no":
|
|
181
|
+
case "n":
|
|
182
|
+
case "0":
|
|
183
|
+
case "off":
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
});
|