Skip to content

Commit 33c23a5

Browse files
committed
add string to boolean conversion in error-handler
1 parent 8b34d64 commit 33c23a5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omer-x/next-openapi-route-handler",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
55
"keywords": [
66
"next.js",

src/core/zod-error-handler.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@ function convertStringToNumber(input: Record<string, unknown>, keys: string[]) {
66
}, input);
77
}
88

9+
function getTrueBoolean(input: string) {
10+
if (input === "true") return true;
11+
if (input === "false") return false;
12+
return null;
13+
}
14+
15+
function convertStringToBoolean(input: Record<string, unknown>, keys: string[]) {
16+
return keys.reduce((mutation, key) => {
17+
return { ...mutation, [key]: getTrueBoolean(mutation[key] as string) } as Record<string, unknown>;
18+
}, input);
19+
}
20+
921
export function safeParse<T>(schema: ZodType<T>, input: Record<string, unknown>) {
1022
const result = schema.safeParse(input);
1123
if (!result.success) {
1224
for (const issue of result.error.issues) {
1325
if (issue.code === "invalid_type" && issue.expected === "number" && issue.received === "string") {
1426
return safeParse(schema, convertStringToNumber(input, issue.path as string[]));
1527
}
28+
if (issue.code === "invalid_type" && issue.expected === "boolean" && issue.received === "string") {
29+
return safeParse(schema, convertStringToBoolean(input, issue.path as string[]));
30+
}
1631
}
1732
throw result.error;
1833
}

0 commit comments

Comments
 (0)