Skip to content

broken schema when using a string property with @format int64 #355

@clicktodev

Description

@clicktodev

Bug description

it seems the generated code doesn't compile.
i believe the code should check for int64 validity but preserve the type as string.

Input

interface Body {
  /**
   * The id of the order item to update.
   * @format int64
   */
  id: string;
}

Expected output

not sure but i found this thread colinhacks/zod#330

also it seems zod supports int64 in v4 https://zod.dev/v4?id=number-formats

// Expected Zod schemas

Actual output

Property 'int64' does not exist on type 'ZodString'

// Actual Zod schemas
export const bodySchema = z.object({
    id: z.string().int64(),
});

Versions

  • Typescript: v5.9.2
  • Zod: v4.1.12

i think this is the solution:

import { z } from "zod";

export const int64String = z.string().refine((val) => {
  try {
    // Let zod do the int64 check
    z.int64().parse(BigInt(val));
    return true;
  } catch {
    return false;
  }
  }, {
    message: "Must be a valid int64 string",
  });

int64String.parse("123");                      // OK
int64String.parse("9223372036854775807");     // OK
int64String.parse("9223372036854775808");     // ❌ outside int64
int64String.parse("abc");                     // ❌ invalid

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions