astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)

Home Page:openapi-zod-client.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow `type: string` with `format: date-time` to coerce `Date` to string when parsing

Jackman3005 opened this issue · comments

The schema generated is correct by OpenAPI standards, but the reality is that Date is converted to string when JSON stringifying from either the frontend or backend. I want to use the schema provided to validate the data before this happens and cannot because the Date objects in the type are not strings yet.

I believe if .coerce() was added to the z.string().datetime() than it would convert the date before validating it and all should be good. Maybe this could be an option when generating the schemas?

It's difficult to do this myself since I have a few different dates in different areas of a large and deeply nested object, so it is not ideal to manually manipulate the schema to coerce for those dates, plus not very maintainable as this is generated and could change.

hey, sorry for the delay I totally forgot this one

I'm not sure this change would please everyone but I feel like this could definitely be nice to have as an opt-in config option or CLI arg
feel free to open a PR for that 🙏