fabien0102 / ts-to-zod

Generate zod schemas from typescript types/interfaces

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jsDoc validations not working

THEO-184 opened this issue · comments

Bug description

jsDoc validations are not been applied in the generated zod schemas

Input

export type VerifyCode = {
  id?: number;
  /**
   * @format email
   * @maxLength 254
   * @minLength 1
   */
  email?: string | null;
  /**
   * @maxLength 128
   * @minLength 1
   */
  phone_number?: string | null;
  account?: number | null;
  verification_action?:
    | 'change_email'
    | 'change_phone_number'
    | 'forgot_password'
    | 'login'
    | 'create_account'
    | null;
  verification_type?: 'email' | 'phone_number' | null;
  /**
   * @maxLength 255
   * @minLength 1
   */
  code: string;
};

Expected output

//export const verifyCodeSchema = z.object({
  id: z.number().optional(),
  email: z
    .string()
    .min(1)
    .max(254)
    .email()
    .optional()
    .nullable(),
  phone_number: z
    .string()
    .min(1)
    .max(128)
    .optional()
    .nullable(),
  account: z
    .number()
    .optional()
    .nullable(),
  verification_action: z
    .union([
      z.literal("change_email"),
      z.literal("change_phone_number"),
      z.literal("forgot_password"),
      z.literal("login"),
      z.literal("create_account")
    ])
    .optional()
    .nullable(),
  verification_type: z
    .union([z.literal("email"), z.literal("phone_number")])
    .optional()
    .nullable(),
  code: z
    .string()
    .min(1)
    .max(255)
})

Actual output

export const verifyCodeSchema = z.object({
  id: z.number().optional(),
  email: z.string().optional().nullable(),
  phone_number: z.string().optional().nullable(),
  account: z.number().optional().nullable(),
  verification_action: z
    .union([
      z.literal('change_email'),
      z.literal('change_phone_number'),
      z.literal('forgot_password'),
      z.literal('login'),
      z.literal('create_account'),
    ])
    .optional()
    .nullable(),
  verification_type: z
    .union([z.literal('email'), z.literal('phone_number')])
    .optional()
    .nullable(),
  code: z.string(),
});

Versions

  • Typescript: ^5
  • Zod: ^3.22.4
  • ts-to-zod ^3.7.3

looks like #50

please check the different solutions, especially the last 3-4 comments.
if this does not help, please comment in the linked issue