Why are request headers defined as an array instead of a ZodObject?
See original GitHub issueFirst of all, great job with this project. I don’t see why it isn’t more adopted in the larger community.
I’m struggling to understand why the request.headers
configuration is defined as a Zodtype[]
, while params and query are defined as AnyZodObject
.
My understanding behind this decision is because there are often non-user defined headers that are passed through with a request directly from the browser (cache-control, content-security-policy, etc…).
However, playing around with request.body
revealed that you are actually able to pass in un-specified keys unless you add z.object({...}).strict()
to the request.body
specification.
If that is the case, could request.headers
also accept an AnyZodObject
?
I’m building an Express middleware which accepts a RouteConfig object and:
- Registers this as a route in the OpenAPI documentation.
- Provides validation on the request object (body/params/query/headers), and rejects the response if an input is malformed.
- Once the route returns a response, the response is also validated against the schema.
I’m trying to build a strongly typed end-to-end system, and the usage of zod-to-openapi as an input to the validation middleware allows stronger consistency. I’ve attached a gist of the middleware here. (It is ofc a work in progress). https://gist.github.com/adamgarcia4/7b5644bb6bb5d3daabf5f8377c5b66b4
I first wanted to ask the above question, as well as showcase how I’m using your package.
export interface RouteConfig extends OperationObject {
method: Method;
path: string;
request?: {
body?: ZodRequestBody;
params?: AnyZodObject;
query?: AnyZodObject;
headers?: ZodType<unknown>[];
};
responses: {
[statusCode: string]: ResponseConfig;
};
}
Issue Analytics
- State:
- Created 9 months ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Understood that would be a breaking change, unless you can simply do an array check and allow for either interfaces.
Side question: Could you provide insight how you guys are using this project beyond the “hello world” use case?
I’m not sure if you guys have any future development planned, but contributing to this project is something that I’d be pretty interested in helping with.
I think for consistency given how these are declared in openapi it think it would make sense if it was also made an
AnyZodObject
. Would be a breaking change though.