Skip to content

Conversation

mohankumarelec
Copy link

This pull request enhances ZodTuple by adding .min(), .max(), .length(), and .nonempty() methods, similar to those available on ZodArray.

Currently, when a z.tuple() schema is converted to a JSON Schema, it lacks minItems and maxItems properties. This results in an inaccurate schema that could, for example, incorrectly validate an empty array.

By adding these length-based validation methods, the JSON Schema conversion now correctly includes the tuple's length constraints, ensuring more precise and reliable validation.

@vercel
Copy link

vercel bot commented Oct 22, 2025

@mohankumarelec is attempting to deploy a commit to the colinhacks Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 22, 2025

Walkthrough

The ZodTuple interface and constructor received four new validation methods for constraining tuple lengths. The min(), nonempty(), max(), and length() methods were added as public APIs, with corresponding implementation bindings that delegate to existing length check utilities. These methods follow the established pattern of chainable validators, accepting optional parameters for customization.

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title "feat: Add length constraints to ZodTuple for JSON Schema" directly aligns with the changeset, which adds new public methods (min, max, length, nonempty) to the ZodTuple interface for enforcing tuple length constraints. The title is specific, concise, and clearly communicates the primary change from the developer's perspective without unnecessary noise or vagueness.
Description Check ✅ Passed The description is clearly related to the changeset and provides meaningful context about the changes. It explains that the PR adds length-validation methods to ZodTuple (mirroring ZodArray), identifies the problem that JSON Schema conversion was missing minItems/maxItems properties, and describes the solution of implementing these methods for more precise validation. The description adequately conveys the intent and rationale behind the changes.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5e6c0fd and 84bc532.

📒 Files selected for processing (1)
  • packages/zod/src/v4/classic/schemas.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (8)
**/*.{js,jsx,ts,tsx,mjs,cjs,json}

📄 CodeRabbit inference engine (CLAUDE.md)

Enforce line width of 120 characters via Biome formatting

Files:

  • packages/zod/src/v4/classic/schemas.ts
**/*.{js,jsx,ts,tsx,mjs,cjs}

📄 CodeRabbit inference engine (CLAUDE.md)

Use ES5-style trailing commas in JavaScript/TypeScript code

Files:

  • packages/zod/src/v4/classic/schemas.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Allow the any type in TypeScript (noExplicitAny off)
Allow non-null assertions in TypeScript (noNonNullAssertion off)
Write TypeScript to pass strict mode with exactOptionalPropertyTypes enabled
Use NodeNext module resolution semantics for imports in TypeScript
Target ES2020 language features in TypeScript source

Files:

  • packages/zod/src/v4/classic/schemas.ts
**/*.{ts,tsx,js,jsx,mjs,cjs}

📄 CodeRabbit inference engine (CLAUDE.md)

Allow parameter reassignment for performance-sensitive code (noParameterAssign off)

Files:

  • packages/zod/src/v4/classic/schemas.ts
**/*.{js,mjs,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development-setup.mdc)

**/*.{js,mjs,ts,tsx}: Use .js extensions in import specifiers (e.g., import { z } from "./index.js")
Don’t use require(); use ESM import statements

Files:

  • packages/zod/src/v4/classic/schemas.ts
**/*.{js,mjs,cjs,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/guidelines.mdc)

Do not leave log statements (e.g., console.log, debugger) in tests or production code

Files:

  • packages/zod/src/v4/classic/schemas.ts
packages/**/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/zod-project-guide.mdc)

Write source code in TypeScript (TypeScript-first codebase)

Files:

  • packages/zod/src/v4/classic/schemas.ts
packages/zod/**

📄 CodeRabbit inference engine (.cursor/rules/zod-project-guide.mdc)

Make core Zod library changes in the main package at packages/zod/

Files:

  • packages/zod/src/v4/classic/schemas.ts
🧬 Code graph analysis (1)
packages/zod/src/v4/classic/schemas.ts (3)
packages/zod/src/v4/core/api.ts (3)
  • $ZodCheckMinLengthParams (907-907)
  • $ZodCheckMaxLengthParams (894-894)
  • $ZodCheckLengthEqualsParams (919-919)
packages/zod/src/v4/core/schemas.ts (2)
  • $ZodType (167-174)
  • $ZodType (178-316)
packages/zod/src/v4/core/core.ts (2)
  • $constructor (7-10)
  • $constructor (17-77)
🔇 Additional comments (2)
packages/zod/src/v4/classic/schemas.ts (2)

1348-1351: LGTM! Clean interface additions.

The method signatures mirror ZodArray perfectly and maintain the chainable API. Parameter types are correctly specified.


1357-1360: Nice consistency with ZodArray!

The implementation follows the exact same pattern as ZodArray (lines 1085-1088), delegating to the established checks.* helpers. This will work correctly since tuples have the same .length property as arrays.


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 and usage tips.

@mohankumarelec
Copy link
Author

@colinhacks , Can you please take a look into this PR and let me know if anything further required. Also please feel free to edit the PR if required. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant