-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(v4): add min, max, nonempty, & size to ZodMap #5316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
expect(result.error!.issues[0].code).toEqual("too_small"); | ||
}); | ||
|
||
test("failing when map is bigger than max() ", () => { |
There was a problem hiding this comment.
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" }, |
There was a problem hiding this comment.
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.
a37c4cb
to
c1954b4
Compare
WalkthroughThis pull request adds size-related validation constraints to Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
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. Comment |
There was a problem hiding this 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
📒 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 ofthis
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 asZodArray
. Thenonempty
method correctly delegates to_minSize(1, params)
, which is clean.
This adds the missing methods
min
,max
, andnonempty
toZodMap
, as well as the missingsize
property.Fixes #5313.