question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why are request headers defined as an array instead of a ZodObject?

See original GitHub issue

First 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:

  1. Registers this as a route in the OpenAPI documentation.
  2. Provides validation on the request object (body/params/query/headers), and rejects the response if an input is malformed.
  3. 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:open
  • Created 9 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
adamgarcia4commented, Dec 19, 2022

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.

1reaction
samchungycommented, Dec 19, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · asteasolutions/zod-to-openapi - GitHub
Why are request headers defined as an array instead of a ZodObject? #80 opened 2 days ago by adamgarcia4 · 6 · Unknown...
Read more >
Why aren't http headers just a simple array? - Google Groups
Hi,. I'm curious why the approach for headers was to use a key/value pair for headers, instead of doing an array of header...
Read more >
HTTP headers - MDN Web Docs - Mozilla
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its ......
Read more >
Defining and mapping headers, query parameters, or path ...
Only single-dimension arrays can be mapped to an HTTP header or parameter. For more information, see Request and response mapping rules. Request array...
Read more >
HTTP headers - GeeksforGeeks
The HTTP headers are used to pass additional information between the clients and the server through the request and response header.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found