Skip to content

Commit f0dbd17

Browse files
committed
add zod safe parser
1 parent ffb642a commit f0dbd17

File tree

7 files changed

+31
-8
lines changed

7 files changed

+31
-8
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git-tag-version=false

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.0",
3+
"version": "0.2.1",
44
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
55
"keywords": [
66
"next.js",

src/core/path-params.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { safeParse } from "./zod-error-handler";
12
import type { ZodType } from "zod";
23

3-
export default function parsePathParams<T>(source: unknown, schema?: ZodType<T>) {
4-
if (!schema) return null;
5-
return schema.parse(source);
4+
export default function parsePathParams<T>(source?: T, schema?: ZodType<T>) {
5+
if (!schema || !source) return null;
6+
return safeParse(schema, source as Record<string, unknown>);
67
}

src/core/search-params.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { safeParse } from "./zod-error-handler";
12
import type { ZodType } from "zod";
23

34
export default function parseSearchParams<T>(source: URLSearchParams, schema?: ZodType<T>) {
@@ -6,5 +7,5 @@ export default function parseSearchParams<T>(source: URLSearchParams, schema?: Z
67
...collection,
78
[key]: value,
89
}), {});
9-
return schema.parse(params);
10+
return safeParse(schema, params);
1011
}

src/core/zod-error-handler.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { ZodType } from "zod";
2+
3+
function convertStringToNumber(input: Record<string, unknown>, keys: string[]) {
4+
return keys.reduce((mutation, key) => {
5+
return { ...mutation, [key]: parseInt(mutation[key] as string) } as Record<string, unknown>;
6+
}, input);
7+
}
8+
9+
export function safeParse<T>(schema: ZodType<T>, input: Record<string, unknown>) {
10+
const result = schema.safeParse(input);
11+
if (!result.success) {
12+
for (const issue of result.error.issues) {
13+
if (issue.code === "invalid_type" && issue.expected === "number" && issue.received === "string") {
14+
return safeParse(schema, convertStringToNumber(input, issue.path as string[]));
15+
}
16+
}
17+
throw new Error("There are some errors left unhandled. (next-openapi-route-handler)");
18+
}
19+
return result.data;
20+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function createRoute<M extends HttpMethod, PP, QP, RB>(input: Rou
4242
const handler: RouteMethodHandler<PP> = async (request, props) => {
4343
try {
4444
const { searchParams } = new URL(request.url);
45-
const pathParams = parsePathParams(props.params, input.pathParams) as PP;
45+
const pathParams = parsePathParams<PP>(props.params, input.pathParams) as PP;
4646
const queryParams = parseSearchParams(searchParams, input.queryParams) as QP;
4747
const body = await parseRequestBody(request, input.method, input.requestBody ?? undefined, input.hasFormData) as RB;
4848
return await input.action({ pathParams, queryParams, body });

0 commit comments

Comments
 (0)