Skip to content

Conversation

boneskull
Copy link
Contributor

This adds the missing methods min, max, and nonempty to ZodMap, as well as the missing size property.

Fixes #5313.

expect(result.error!.issues[0].code).toEqual("too_small");
});

test("failing when map is bigger than max() ", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know if the trailing spaces in the test titles were intentional. I just copied them from set.test.ts.

file: { unit: "bytes", verb: "to have" },
array: { unit: "items", verb: "to have" },
set: { unit: "items", verb: "to have" },
map: { unit: "entries", verb: "to have" },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured "entries" was appropriate. I'm not sure if I need to apply this to the rest of the locale files

This adds the missing methods `min`, `max`, and `nonempty` to `ZodMap`, as well as the missing `size` property.

Fixes colinhacks#5313.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 18, 2025

Walkthrough

This pull request adds size-related validation constraints to ZodMap. Four new methods are introduced—min(), max(), size(), and nonempty()—which enable fluent chaining for restricting map entry counts. The methods connect to core validation functions (_minSize, _maxSize, _size) and are initialized on ZodMap instances in the constructor. Comprehensive test coverage is added for these constraints across various map scenarios. Error messaging is updated to handle "map" as an origin type with "entries" as the sizing unit.

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title "feat(v4): add min, max, nonempty, & size to ZodMap" is specific and clearly describes the main change: adding four size-related methods to ZodMap. It uses conventional commit formatting and is concise while being descriptive enough for someone reviewing history to immediately understand what's being added. The title directly aligns with the changes made across all three files in the changeset.
Linked Issues Check ✅ Passed The PR fully satisfies the requirements from issue #5313, which requested adding min() and max() methods to ZodMap (noting they already exist on ZodSet). The implementation adds these methods along with helpful bonus methods (nonempty and size), includes comprehensive tests covering various scenarios, and adds error message support for the "map" origin type. All core coding requirements from the linked issue are met.
Out of Scope Changes Check ✅ Passed All changes are appropriately scoped to the linked issue. The core additions (min/max methods) directly address issue #5313, while the bonus additions (nonempty/size methods) are natural extensions that follow the pattern from ZodSet. The test additions and error message support in the locale file are necessary enablers for these features to function correctly. No unrelated changes or scope creep is evident.

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/zod/src/v4/locales/en.ts (1)

34-34: LGTM! "Entries" is the right choice for maps.

The unit "entries" makes sense for maps since they contain key-value pairs. The pattern matches the existing sizing types perfectly. As you noted in your comment, you might want to apply this to other locale files too for consistency, but that's optional cleanup.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8e4739f and c1954b4.

📒 Files selected for processing (3)
  • packages/zod/src/v4/classic/schemas.ts (1 hunks)
  • packages/zod/src/v4/classic/tests/map.test.ts (3 hunks)
  • packages/zod/src/v4/locales/en.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/zod/src/v4/classic/schemas.ts (2)
packages/zod/src/v4/core/api.ts (3)
  • $ZodCheckMinSizeParams (870-870)
  • $ZodCheckMaxSizeParams (858-858)
  • $ZodCheckSizeEqualsParams (882-882)
packages/zod/src/v4/core/core.ts (2)
  • $constructor (7-10)
  • $constructor (17-77)
🔇 Additional comments (7)
packages/zod/src/v4/classic/tests/map.test.ts (5)

7-11: Nice setup for testing the new methods.

These precomputed validators cover all the new size-related methods and even test method chaining with nonempty().max(2). Clean approach.


33-80: Solid coverage of the happy paths.

The test checks all the new methods with valid inputs and even verifies that the parsed results have the correct size. The boundary cases are well-covered here.


82-87: Good error case coverage.

This correctly tests that an empty map fails the nonempty constraint and produces the expected error code.


89-100: Max constraint test looks good.

Testing that a 3-entry map fails the max(2) constraint with the right error code. Straightforward and correct.


273-330: Excellent boundary testing with snapshots.

This test hits all the important cases: valid at minimum, below minimum, and above maximum. The snapshots verify that the error messages properly reference "map" and "entries", which confirms the locale changes are working correctly.

packages/zod/src/v4/classic/schemas.ts (2)

1437-1440: Interface looks good, matches ZodSet pattern.

The method signatures are consistent with ZodSet (lines 1470-1473) and use the right parameter types. The return type of this enables fluent chaining as expected.


1447-1450: Implementation matches the established patterns perfectly.

The constructor implementation is identical to ZodSet (lines 1479-1482) and follows the same pattern as ZodArray. The nonempty method correctly delegates to _minSize(1, params), which is clean.

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.

enhancement: min() / max() for ZodMap

1 participant