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)' #211

Open
robogeek opened this issue Dec 4, 2023 · 0 comments
Open

Comments

@robogeek
Copy link

robogeek commented Dec 4, 2023

Description

I have an OpenAPI spec where one schema object is declared as so:

    dateTime:
      type: string
      format: date-time
      description: datetime in ISO 8601 format
      example: 2023-06-15T09:30:00Z
      default: "0000-00-00"

The idea of this default value is that it's a string parseable as a Date, but produces a value we can readily detect as invalid.

The generated code from openapi-typescript is:

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

That's a good correspondence to the schema.

The output from ts-to-zod is:

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

The correspondence fell apart. It got string and datetime correct, but the default value went sideways. It's as if it evaluated @default 0000-00-00 as an arithmetic expression, producing a Number.

Name Version
@openapi-codegen/typescript 8.0.0
ts-to-zoc 3.4.1
Node.js 20.10.0
OS + version Ubuntu 20.04

Reproduction

See above

Expected result

export const dateTimeSchema = z.string().datetime().default('0000-00-00');
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