-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zod-OpenAPI not performing strict validation on response schemas #913
Comments
Thank you for the issue. It is a known issue that the Zod OpenAPI can't validate the value of the response: #181 |
function strictJSONResponse<
C extends Context,
S extends ZodSchema,
U extends StatusCode
>(c: C, schema: S, data: Parameters<Context['json']>[0], statusCode: U) {
const validatedResponse = schema.safeParse(data);
if (!validatedResponse.success) {
return c.json(
{
message: 'Strict response validation failed',
},
500
);
}
return c.json(validatedResponse.data as z.infer<S>, statusCode);
} worked for me. maybe we could make this a helper? |
Would that work with parseAsync too? |
@Rick-Phoenix yes just make your function async |
Ah, I'm also considering the same approach: using a helper to validate the data before |
Which middleware has the bug?
@hono/zod-openapi
What version of the middleware?
0.18.3
What version of Hono are you using?
4.6.14
What runtime/platform is your app running on? (with version if possible)
Node 20.17.0
What steps can reproduce the bug?
What is the expected behavior?
What do you see instead?
Additional information
I have tested the schema and I can clearly see from the openAPI specs that "additionalProperties" is set to false.
Also, I can see from the autocomplete on c.json() that the schema does indeed exclude the extra properties that I am passing.
However, I can still pass extra fields and I get no error.
I only get an error if I am manually parsing the response with zod.
The text was updated successfully, but these errors were encountered: