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

bigint with max and min produces incorrect schema #286

Open
adrienlo opened this issue Nov 22, 2024 · 3 comments
Open

bigint with max and min produces incorrect schema #286

adrienlo opened this issue Nov 22, 2024 · 3 comments
Labels
bug Something isn't working workaround available

Comments

@adrienlo
Copy link

adrienlo commented Nov 22, 2024

Bug description

We have a field that has a type of bigint with min/max. But the output doesn't produce a correct schema.

Input

  /**
   * @minimum -999999999 scale must be greater than or equal to -999999999
   * @maximum 999999999 scale must be less than or equal to 999999999
   */
type Foo = {
  scale: bigint
}

Expected output

const fooSchema = z.object({
  scale:  z.bigint()
    .min(BigInt(-999999999), 'scale must be greater than or equal to -999999999')
    .max(BigInt(999999999), 'scale must be less than or equal to 999999999'),
})

Actual output

const fooSchema = z.object({
  scale: z.bigint()
    .min(-999999999, 'scale must be greater than or equal to -999999999')
    .max(999999999, 'scale must be less than or equal to 999999999'),
})

This is also a Typescript error
Screenshot 2024-11-22 at 12 28 36 PM

Versions

  • Typescript: ^5.5.4
  • Zod: ^3.23.8
  • ts-to-zod: '^3.13.0'
@adrienlo
Copy link
Author

I did try this but it did not work

  /**
   * @minimum BigInt(-999999999) scale must be greater than or equal to -999999999
   * @maximum BigInt999999999) scale must be less than or equal to 999999999
   */

@tvillaren
Copy link
Collaborator

Hello,

Indeed, the bigInt type it not detected.
Not an easy fix given the way JSDoc tags are handled (they're unaware of the underlying property)

A workaround can be implemented using the @schema annotation:

type Foo = {
  /**
   * @schema .min(BigInt(-999999999), "scale must be greater than or equal to -999999999").max(BigInt(999999999), "scale must be less than or equal to 999999999")
   */
  scale: bigint;
};

This will append whatever is behind the @schema at generation time:

// Generated by ts-to-zod
import { z } from "zod";

const fooSchema = z.object({
  scale: z
    .bigint()
    .min(
      BigInt(-999999999),
      "scale must be greater than or equal to -999999999"
    )
    .max(BigInt(999999999), "scale must be less than or equal to 999999999"),
});

@tvillaren tvillaren added the bug Something isn't working label Nov 23, 2024
@adrienlo
Copy link
Author

This worked great. Thank you. Looks like @schema is where we'll be spending a lot of our time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working workaround available
Projects
None yet
Development

No branches or pull requests

2 participants