Skip to content

Commit

Permalink
Make it possible to define a different HTTP Status Code without using…
Browse files Browse the repository at this point in the history
… a hook

I usually return 422 on invalid data, would be nice to set this without writing out a hook on every request 👍
  • Loading branch information
Matzu89 authored Jan 28, 2024
1 parent 9b9cebe commit c4f7cd7
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/zod-validator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const zValidator = <
>(
target: Target,
schema: T,
hook?: Hook<z.infer<T>, E, P>
hook?: Hook<z.infer<T>, E, P>,
statusCode?: Number
): MiddlewareHandler<E, P, V> =>
validator(target, async (value, c) => {
const result = await schema.safeParseAsync(value)
Expand All @@ -44,7 +45,7 @@ export const zValidator = <
}

if (!result.success) {
return c.json(result, 400)
return c.json(result, statusCode ?? 400);
}

const data = result.data as z.infer<T>
Expand Down

0 comments on commit c4f7cd7

Please sign in to comment.