@ptolemy2002/regex-utils 2.2.0 → 2.2.2
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 +6 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.js +8 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,9 @@ type TransformRegexOptions = {
|
|
|
20
20
|
};
|
|
21
21
|
export type ZodValidator<O> = (v: O) => boolean;
|
|
22
22
|
export type ZodValidatorWithErrors<O> = (v: O) => true | string | string[];
|
|
23
|
+
export type ZodSafeParseable<O> = {
|
|
24
|
+
safeParse: (v: unknown) => { success: boolean, data?: O, error?: ZodError };
|
|
25
|
+
};
|
|
23
26
|
```
|
|
24
27
|
|
|
25
28
|
## Functions
|
|
@@ -127,7 +130,7 @@ Checks if a string is a valid set of regular expression flags.
|
|
|
127
130
|
|
|
128
131
|
### interpretZodError
|
|
129
132
|
#### 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.
|
|
133
|
+
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
134
|
|
|
132
135
|
#### Parameters
|
|
133
136
|
- `e` (`ZodError`): The zod error to be interpreted.
|
|
@@ -140,7 +143,7 @@ Given a zod error, interprets it to `null` if no error is found, a single error
|
|
|
140
143
|
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.
|
|
141
144
|
|
|
142
145
|
#### Parameters
|
|
143
|
-
- `p` (`
|
|
146
|
+
- `p` (`ZodSafeParseable<O>`): The zod schema to be used for validation.
|
|
144
147
|
|
|
145
148
|
#### Returns
|
|
146
149
|
`ZodValidator<O>` - A function that takes a value and returns `true` if the value matches the schema, `false` otherwise.
|
|
@@ -150,7 +153,7 @@ This is a simple function that takes a zod schema, returning a function that tak
|
|
|
150
153
|
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.
|
|
151
154
|
|
|
152
155
|
#### Parameters
|
|
153
|
-
- `p` (`
|
|
156
|
+
- `p` (`ZodSafeParseable<O>`): The zod schema to be used for validation.
|
|
154
157
|
|
|
155
158
|
#### Returns
|
|
156
159
|
`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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z, ZodError
|
|
1
|
+
import { z, ZodError } 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;
|
|
@@ -18,8 +18,15 @@ 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
20
|
export declare function interpretZodError(e: ZodError): string | string[] | null;
|
|
21
|
-
export
|
|
22
|
-
|
|
21
|
+
export type ZodSafeParseable<O> = {
|
|
22
|
+
safeParse: (v: unknown) => {
|
|
23
|
+
success: boolean;
|
|
24
|
+
data?: O;
|
|
25
|
+
error?: ZodError;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export declare function zodValidate<O>(p: ZodSafeParseable<O>): ZodValidator<O>;
|
|
29
|
+
export declare function zodValidateWithErrors<O>(p: ZodSafeParseable<O>): ZodValidatorWithErrors<O>;
|
|
23
30
|
export declare function isAlphanumeric(str: string): boolean;
|
|
24
31
|
export declare function toAlphanumeric(str: string, separator?: string): string;
|
|
25
32
|
export declare function isValidEmail(v: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -113,11 +113,17 @@ function isValidRegexFlags(value) {
|
|
|
113
113
|
}
|
|
114
114
|
function interpretZodError(e) {
|
|
115
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
|
+
}
|
|
116
122
|
if (errors.length === 0)
|
|
117
123
|
return null;
|
|
118
124
|
if (errors.length === 1)
|
|
119
|
-
return
|
|
120
|
-
return errors.map((e) => e
|
|
125
|
+
return formatIssue(errors[0]);
|
|
126
|
+
return errors.map((e) => formatIssue(e));
|
|
121
127
|
}
|
|
122
128
|
function zodValidate(p) {
|
|
123
129
|
return (v) => {
|