kubb-labs / kubb

The ultimate toolkit for working with APIs.
https://kubb.dev
MIT License
670 stars 54 forks source link

[swagger-zod] Add a way to coerce date #1050

Closed Wykks closed 3 months ago

Wykks commented 3 months ago

What is the problem this feature would solve?

If have a setup where I generate mocks and use msw (without kubb here), and zod to validate the data.

I would like to generate every date with the native Date, but on runtime msw send a date as a string (because it's serialized), and it break, because zod expect a Date object.

External documents/projects?

No response

What is the feature you are proposing to solve the problem?

Instead of doing z.date(), do z.coerce.date() to parse date even if they come in string

What alternatives have you considered?

No response

stijnvanhulle commented 3 months ago

There will be a new option called coercion that you can enable that will use z.coerce.date() instead of .date(). See https://1051.docs.kubb.dev/plugins/swagger-zod/#coercion.

Wykks commented 3 months ago

Awesome thanks!

mchataigner commented 2 months ago

Thank you for this feature! It has a slight side effect to coerce everything where it’s might not be wanted: z.union([z.coerce.string(), z.null()]), null in input will be coerced as "null" string

Would there be a way to coerce only Date and not the rest?