@ptolemy2002/regex-utils 2.5.0 → 2.6.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 +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +17 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -153,7 +153,7 @@ Checks if a string is a valid set of regular expression flags.
|
|
|
153
153
|
|
|
154
154
|
### interpretZodError
|
|
155
155
|
#### Description
|
|
156
|
-
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>`.
|
|
156
|
+
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.
|
|
157
157
|
|
|
158
158
|
#### Parameters
|
|
159
159
|
- `e` (`ZodError`): The zod error to be interpreted.
|
package/dist/index.d.ts
CHANGED
|
@@ -17,11 +17,12 @@ 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;
|
|
23
24
|
};
|
|
24
|
-
export declare function interpretZodError(e: ZodError, { prefix, separator }
|
|
25
|
+
export declare function interpretZodError(e: ZodError, { prefix, separator }?: InterpretZodErrorOptions): string | string[] | null;
|
|
25
26
|
export type ZodSafeParseable<O> = {
|
|
26
27
|
safeParse: (v: unknown) => z.SafeParseReturnType<unknown, O>;
|
|
27
28
|
};
|
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,10 +112,13 @@ function isValidRegex(value, flags = "") {
|
|
|
111
112
|
function isValidRegexFlags(value) {
|
|
112
113
|
return /^[gimsuy]*$/.test(value);
|
|
113
114
|
}
|
|
114
|
-
function
|
|
115
|
+
function isZodError(err) {
|
|
116
|
+
return Boolean(err && (err instanceof zod_1.ZodError || err.name === 'ZodError'));
|
|
117
|
+
}
|
|
118
|
+
function interpretZodError(e, { prefix, separator = "." } = {}) {
|
|
115
119
|
const { errors } = e;
|
|
116
120
|
function formatIssue(issue) {
|
|
117
|
-
const { path: _path, message } = issue;
|
|
121
|
+
const { code, path: _path, message } = issue;
|
|
118
122
|
let path = _path;
|
|
119
123
|
if (typeof prefix === "string") {
|
|
120
124
|
path = [prefix, ...path];
|
|
@@ -122,6 +126,16 @@ function interpretZodError(e, { prefix, separator = "." }) {
|
|
|
122
126
|
else if (Array.isArray(prefix)) {
|
|
123
127
|
path = [...prefix, ...path];
|
|
124
128
|
}
|
|
129
|
+
if (code === "invalid_return_type") {
|
|
130
|
+
const returnTypeIssue = issue.returnTypeError.errors[0];
|
|
131
|
+
path = [...path, "returnType", ...returnTypeIssue.path];
|
|
132
|
+
return `${path.join(separator)}: ${returnTypeIssue.message}`;
|
|
133
|
+
}
|
|
134
|
+
else if (code === "invalid_arguments") {
|
|
135
|
+
const argumentsIssue = issue.argumentsError.errors[0];
|
|
136
|
+
path = [...path, "arguments", ...argumentsIssue.path];
|
|
137
|
+
return `${path.join(separator)}: ${argumentsIssue.message}`;
|
|
138
|
+
}
|
|
125
139
|
if (path.length === 0)
|
|
126
140
|
return message;
|
|
127
141
|
return `${path.join(separator)}: ${message}`;
|
|
@@ -140,7 +154,7 @@ function zodValidate(p) {
|
|
|
140
154
|
}
|
|
141
155
|
class ZodInterpretedError extends Error {
|
|
142
156
|
constructor(message = null, { prefix, joinSeparator = "\n", pathSeparator = "." } = {}) {
|
|
143
|
-
if (message
|
|
157
|
+
if (isZodError(message))
|
|
144
158
|
message = interpretZodError(message, { prefix, separator: pathSeparator });
|
|
145
159
|
super(Array.isArray(message) ? message.join(joinSeparator) : message ?? undefined);
|
|
146
160
|
this.zodMessage = message;
|