-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Added native support for min and max date validations #1222
Added native support for min and max date validations #1222
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
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.
Added some comments that I hope can be changed, to make it work a bit more consistently with other checks and errors.
min
andmax
should be inclusive (to match othermin
andmax
checks)- issue generated should include
inclusive: true
inclusive
should be used in error map to check whetheror equal to
should be used or not. Even though the currentmin
andmax
check (should) be hard-coded to be inclusive, I can easily create my own non-inclusive issue from a refinement or transform, so it's good if the default error map takes that into account, like e.g. the one fornumber
does.
Thank you @Svish for the review! I'll be really happy to conform to the library standards :) I'm a bit confused, the checks are already inclusive by default. That's also why the hardcoded message - it's inclusive by default. I get the feedback of adding the I'll push asap the new changes :) |
Oh! Sorry, I must have read the checks backwards. I think I just expected to see an |
Unless it already is possible and I've just missed it in the PR, could you please you add getters for the minimum and maximum date, so that it's possible to get those values "back out" of the schema? Equivalent to how These getters makes it possible to set the correct max and min values of an input component from a schema directly, rather than having to pass those in manually. Might be possible to just copy them from get minDate() {
let min: Date | null = null;
this._def.checks.map((ch) => {
if (ch.kind === "min") {
if (min === null || ch.value > min) {
min = ch.value;
}
}
});
return min;
}
get maxDate() {
let max: Date | null = null;
this._def.checks.map((ch) => {
if (ch.kind === "max") {
if (max === null || ch.value < max) {
max = ch.value;
}
}
});
return max;
} |
That's indeed a valid suggestion, I'll add them :) |
An immaculate PR — thanks!! 🙌 |
This PR fixes #1089.
ZodDate
now can natively validate formin
andmax
allowed dates.