fabien0102 / ts-to-zod

Generate zod schemas from typescript types/interfaces
MIT License
1.21k stars 66 forks source link

Integer JSDoc tag #272

Open FlorianCassayre opened 3 weeks ago

FlorianCassayre commented 3 weeks ago

Feature description

It would be useful if we could annotate a number as an integer (whole number).

Input

For example:

/**
 * @integer
 */
export type Integer = number;

Output

export const integerSchema = z.number().int();
tvillaren commented 3 days ago

Hello @FlorianCassayre,

Thanks for your request. Feel free to open a PR as this is quite a straightforward jump into the lib codebase I think 😉

In the meantime, you can also use the @schema annotation to append the .int() validator:

// Input
export type MyType = {
  /**
   * @schema .int()
   */
  name: number;
};

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

export const myTypeSchema = z.object({
  name: z.number().int(),
});