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

Schema with 'default: "0000-00-00"' became Zod validator 'default(0)' #187

Open
robogeek opened this issue Dec 4, 2023 · 4 comments
Open
Labels
question Further information is requested

Comments

@robogeek
Copy link

robogeek commented Dec 4, 2023

Bug description

An OpenAPI schema with type: string, format: date-time and default: 0000-00-00 becomes default(0) in code generated by ts-to-zod

Full into: fabien0102/openapi-codegen#211

Input

/**
 * datetime in ISO 8601 format
 *
 * @format date-time
 * @example "2023-06-15T09:30:00.000Z"
 * @default 0000-00-00
 */
export type DateTime = string;

Expected output

export const dateTimeSchema = z.string().datetime().default('0000-00-00');

Actual output

export const dateTimeSchema = z.string().datetime().default(0);

Versions

  • Typescript: v4.5.3
  • Zod: v3.22.4
@tvillaren
Copy link
Collaborator

Hello @robogeek

I believe it works if you add 0000-00-00 between quotes, as it is a string.

/**
 * datetime in ISO 8601 format
 *
 * @format date-time
 * @example "2023-06-15T09:30:00.000Z"
 * @default "0000-00-00"
 */
export type DateTime = string;

@robogeek
Copy link
Author

robogeek commented Dec 4, 2023

Yes that's accurate, but with a caveat.

Using @default "0000-00-00" generates default("0000-00-00")

However, using @default '0000-00-00' generates default("'0000-00-00'")

BUT -- The code I showed above as the Input - that was autogenerated by openapi-codegen. This appears to be a sister project to ts-to-zod?

AND, because this is @format date-time shouldn't the code look for the value of @default to be looking for a date-time string??

@schiller-manuel
Copy link
Collaborator

schiller-manuel commented Dec 4, 2023

The parsing of the @default tag is decoupled from the parsing of the @format tag (in fact all tags are parsed separately from each other).
In your case the the value after the @default tag is parsed as a number here:

https://github.com/fabien0102/ts-to-zod/blob/main/src/core/jsDocTags.ts#L181

A possible solution could be to store the original value that follows value JSDoc tag and later, when both @default and @format are parsed try to interpret it.

@tvillaren
Copy link
Collaborator

However, using @default '0000-00-00' generates default("'0000-00-00'")

We could add handling of single quotes as well in the tag handling if that'd make sense

This appears to be a sister project to ts-to-zod?

It seems that it has been created by the same person, indeed. If the code is wrongly generated there, maybe the fix should be made there?

AND, because this is @Format date-time shouldn't the code look for the value of @default to be looking for a date-time string??

I think that would add complexity to the ts-to-zod codebase (because it would mean handling other @format tags as well IMO), for something that can be simplfy handled by adding quotes.

@tvillaren tvillaren added the question Further information is requested label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants