Skip to content
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

TODO - handle OpenAPI format #8

Closed
Tracked by #26
astahmer opened this issue Aug 27, 2022 · 1 comment
Closed
Tracked by #26

TODO - handle OpenAPI format #8

astahmer opened this issue Aug 27, 2022 · 1 comment

Comments

@astahmer
Copy link
Owner

astahmer commented Aug 27, 2022

zod implementation: https://github.com/colinhacks/zod#dates

To write a schema that accepts either a Date or a date string, use z.preprocess.

const dateSchema = z.preprocess((arg) => {
  if (typeof arg == "string" || arg instanceof Date) return new Date(arg);
}, z.date());
type DateSchema = z.infer<typeof dateSchema>;
// type DateSchema = Date

dateSchema.safeParse(new Date("1/12/22")); // success: true
dateSchema.safeParse("2022-01-12T00:00:00.000Z"); // success: true

open api spec description: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types

@astahmer astahmer changed the title TODO - OpenAPI format TODO - handle OpenAPI format Aug 27, 2022
@astahmer astahmer mentioned this issue Oct 14, 2022
8 tasks
@astahmer
Copy link
Owner Author

those format have first-class support in zod and most importantly do not change the inferred type: email | hostname | uri | uuid, so we will be implement them

whereas format: date requires a z.preprocess and change the inferred type from string to Date, idk if that's such a good idea to implement that
if there is demand for this date format then maybe we can implement it with an option flag, but I doubt most people want it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant