@ptolemy2002/regex-utils 2.6.0 → 2.6.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 +10 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -151,6 +151,16 @@ Checks if a string is a valid set of regular expression flags.
|
|
|
151
151
|
#### Returns
|
|
152
152
|
`boolean` - `true` if the string is a valid set of regular expression flags, `false` otherwise.
|
|
153
153
|
|
|
154
|
+
### isZodError
|
|
155
|
+
#### Description
|
|
156
|
+
Checks if an unknown value is a ZodError. This is more reliable than using `instanceof` as it also checks for the name property. This is particularly important in projects with multiple versions of zod, where instanceof checks may fail due to different constructor references.
|
|
157
|
+
|
|
158
|
+
#### Parameters
|
|
159
|
+
- `err` (`unknown`): The value to be checked.
|
|
160
|
+
|
|
161
|
+
#### Returns
|
|
162
|
+
`boolean` - `true` if the value is a ZodError, `false` otherwise.
|
|
163
|
+
|
|
154
164
|
### interpretZodError
|
|
155
165
|
#### Description
|
|
156
166
|
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>`. If the error is with function arguments, "arguments.<index>" will be appended to the end of the path. Similarly, if the error is with a function return value, "returnValue" will be appended to the end of the path. Note that for function arguments, only the first seen error will be reported.
|
package/dist/index.d.ts
CHANGED
|
@@ -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 isZodError(err: unknown): err is ZodError;
|
|
20
21
|
export type InterpretZodErrorOptions = {
|
|
21
22
|
prefix?: string | string[];
|
|
22
23
|
separator?: string;
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ exports.regexMatchWhole = regexMatchWhole;
|
|
|
10
10
|
exports.transformRegex = transformRegex;
|
|
11
11
|
exports.isValidRegex = isValidRegex;
|
|
12
12
|
exports.isValidRegexFlags = isValidRegexFlags;
|
|
13
|
+
exports.isZodError = isZodError;
|
|
13
14
|
exports.interpretZodError = interpretZodError;
|
|
14
15
|
exports.zodValidate = zodValidate;
|
|
15
16
|
exports.zodValidateWithErrors = zodValidateWithErrors;
|
|
@@ -111,6 +112,9 @@ function isValidRegex(value, flags = "") {
|
|
|
111
112
|
function isValidRegexFlags(value) {
|
|
112
113
|
return /^[gimsuy]*$/.test(value);
|
|
113
114
|
}
|
|
115
|
+
function isZodError(err) {
|
|
116
|
+
return Boolean(err && (err instanceof zod_1.ZodError || err.name === 'ZodError'));
|
|
117
|
+
}
|
|
114
118
|
function interpretZodError(e, { prefix, separator = "." } = {}) {
|
|
115
119
|
const { errors } = e;
|
|
116
120
|
function formatIssue(issue) {
|
|
@@ -150,7 +154,7 @@ function zodValidate(p) {
|
|
|
150
154
|
}
|
|
151
155
|
class ZodInterpretedError extends Error {
|
|
152
156
|
constructor(message = null, { prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
|
|
153
|
-
if (message
|
|
157
|
+
if (isZodError(message))
|
|
154
158
|
message = interpretZodError(message, { prefix, separator: pathSeparator });
|
|
155
159
|
super(Array.isArray(message) ? message.join(joinSeparator) : message ?? undefined);
|
|
156
160
|
this.zodMessage = message;
|