Skip to content

fix(zod): enforce integer validation - #3737

Merged
melloware merged 2 commits into
orval-labs:masterfrom
federicobartoli:fix/zod-integer-validation
Jul 18, 2026
Merged

fix(zod): enforce integer validation#3737
melloware merged 2 commits into
orval-labs:masterfrom
federicobartoli:fix/zod-integer-validation

Conversation

@federicobartoli

@federicobartoli federicobartoli commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • generate native integer validators for OpenAPI integer schemas
  • use z.int() for Zod 4 and Zod Mini while preserving z.number().int() for Zod 3
  • preserve integer validation in OpenAPI 3.1 multi-type schemas
  • support integer coercion and forward custom Zod params to both the numeric input validator and the integer validator
  • update generated samples and snapshots

Behavior

Fractional values are now rejected for OpenAPI integer schemas. Zod 4 and Zod Mini use the native z.int() safe-integer semantics; Zod 3 retains its native z.number().int() behavior.

Breaking behavior

  • On Zod 4 and Zod Mini, z.int() enforces JavaScript safe integer bounds. Integer fields with format: int64 that carry values above Number.MAX_SAFE_INTEGER (for example 64-bit snowflake ids) were accepted by the previous z.number() output and are now rejected. The Zod 3 output (z.number().int()) still accepts them. Note that JSON.parse already loses precision for such values, so rejecting them surfaces a real problem; APIs that need full 64-bit range should serialize those fields as strings or use an override.
  • For zodParams mutator authors: integer fields now surface the new validator name int. On Zod 4 and Zod Mini without coercion the mutator is called with int instead of number; on Zod 3 and on coerced output it is called with both number and int.

Test plan

  • vp lint packages/zod/src/index.ts packages/zod/src/zod.test.ts packages/orval/src/reusable-schemas.test.ts
  • vitest run packages/zod/src packages/orval/src/reusable-schemas.test.ts
  • tsc --noEmit -p packages/zod/tsconfig.json
  • tsc --noEmit -p packages/orval/tsconfig.json
  • vp run -w test:snapshots

Fixes #3736

Summary by CodeRabbit

  • New Features
    • Generated Zod schemas now emit integer-specific validation (int) for OpenAPI type: "integer", including correct min/max constraint handling and support across different Zod emission modes (including coercion and lightweight variants).
    • Parameter injection into generated schemas now applies correctly for integer validators.
  • Bug Fixes
    • Numeric fields used for identifiers, limits, quantities, and status now reject fractional values and consistently require integers.
  • Tests
    • Expanded assertions to cover integer rendering, constraint output, and injected parameter behavior.

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Zod generator now preserves OpenAPI integer semantics and emits integer validators across classic, coerced, and mini targets. Tests cover rendering and parameter injection, while generated Angular, Hono, and MCP schemas update numeric fields to use integer validation.

Changes

Integer-aware Zod generation

Layer / File(s) Summary
Integer detection and rendering
packages/zod/src/index.ts
Integer schemas remain distinct and render with int validators, including bounds, coercion, and Zod Mini handling.
Integer output and injection coverage
packages/zod/src/zod.test.ts, packages/orval/src/reusable-schemas.test.ts
Tests cover integer bounds, defaults, unions, target-specific output, and zodParams injection.
Angular generated schemas
samples/angular-app/src/api/...
Generated schemas replace applicable number() validators with int() while retaining existing constraints.
Angular Query generated schemas
samples/angular-query/src/api/model-zod/*
Pet-related numeric fields now use integer validators across nested and union schemas.
Hono and MCP generated schemas
samples/hono/..., samples/mcp/...
Identifiers, quantities, parameters, inventory values, and related numeric fields now require integers.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested labels: bug

Suggested reviewers: melloware, soartec-lab, ire4ever1190, aqeelat, the-ult

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #3736 by making OpenAPI integer schemas reject fractional values across Zod classic, Zod Mini, and Zod 3/4 paths.
Out of Scope Changes check ✅ Passed The added tests and sample updates are directly tied to integer-validation support and do not appear unrelated.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: enforcing integer validation in Zod schemas.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@melloware melloware added the zod Zod schema client related issue label Jul 17, 2026

@melloware melloware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like build failed?

@federicobartoli

Copy link
Copy Markdown
Contributor Author

The CI failure was unrelated to this PR: during sample generation, the external-ref project failed to resolve https://geojson.org/schema/Point.json because geojson.org returned a 503 at the time (EXTERNAL_REFERENCE_NOT_FOUND in the run logs, and the Windows job was simply canceled by the matrix fail fast). The URL is reachable again, so I pushed an empty commit to retrigger the checks. The workflows are now awaiting maintainer approval. Could you approve the runs when you get a chance? Thanks! @melloware

@federicobartoli
federicobartoli force-pushed the fix/zod-integer-validation branch from b7abaf4 to 49d019d Compare July 17, 2026 23:42
@melloware

Copy link
Copy Markdown
Collaborator

Running now

@pkg-pr-new

pkg-pr-new Bot commented Jul 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

@orval/angular

bun add https://pkg.pr.new/@orval/angular@49d019d

@orval/axios

bun add https://pkg.pr.new/@orval/axios@49d019d

@orval/core

bun add https://pkg.pr.new/@orval/core@49d019d

@orval/effect

bun add https://pkg.pr.new/@orval/effect@49d019d

@orval/fetch

bun add https://pkg.pr.new/@orval/fetch@49d019d

@orval/hono

bun add https://pkg.pr.new/@orval/hono@49d019d

@orval/mcp

bun add https://pkg.pr.new/@orval/mcp@49d019d

@orval/mock

bun add https://pkg.pr.new/@orval/mock@49d019d

orval

bun add https://pkg.pr.new/orval@49d019d

@orval/query

bun add https://pkg.pr.new/@orval/query@49d019d

@orval/solid-start

bun add https://pkg.pr.new/@orval/solid-start@49d019d

@orval/swr

bun add https://pkg.pr.new/@orval/swr@49d019d

@orval/zod

bun add https://pkg.pr.new/@orval/zod@49d019d

commit: 49d019d

@melloware
melloware merged commit 2d90b05 into orval-labs:master Jul 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

zod Zod schema client related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zod generator does not enforce OpenAPI integer semantics

2 participants