Skip to content
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

Merged
merged 5 commits into from
Jul 18, 2022

Conversation

Balastrong
Copy link
Contributor

This PR fixes #1089.

ZodDate now can natively validate for min and max allowed dates.

@netlify
Copy link

netlify bot commented Jun 24, 2022

Deploy Preview for guileless-rolypoly-866f8a ready!

Name Link
🔨 Latest commit 5d7d3bd
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/62d4f57cc9285800083471e0
😎 Deploy Preview https://deploy-preview-1222--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link

@Svish Svish left a 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 and max should be inclusive (to match other min and max checks)
  • issue generated should include inclusive: true
  • inclusive should be used in error map to check whether or equal to should be used or not. Even though the current min and max 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 for number does.

deno/lib/ZodError.ts Outdated Show resolved Hide resolved
deno/lib/ZodError.ts Outdated Show resolved Hide resolved
deno/lib/types.ts Show resolved Hide resolved
deno/lib/types.ts Show resolved Hide resolved
src/ZodError.ts Outdated Show resolved Hide resolved
src/ZodError.ts Outdated Show resolved Hide resolved
src/types.ts Show resolved Hide resolved
src/types.ts Show resolved Hide resolved
@Balastrong
Copy link
Contributor Author

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.
if (input.data.getTime() > check.value) { throw error } already means that if the submitted value equals the max, it is considered valid.

That's also why the hardcoded message - it's inclusive by default.

I get the feedback of adding the inclusive field to show the error message as it might bet used by something else in the future, but the min/max check looks correct to me.

I'll push asap the new changes :)

@Svish
Copy link

Svish commented Jun 24, 2022

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. if (input.data.getTime() > check.value) { throw error } already means that if the submitted value equals the max, it is considered valid.

That's also why the hardcoded message - it's inclusive by default.

I get the feedback of adding the inclusive field to show the error message as it might bet used by something else in the future, but the min/max check looks correct to me.

I'll push asap the new changes :)

Oh! Sorry, I must have read the checks backwards. I think I just expected to see an <= when you're message read "or equal". My bad! But yeah, more inclusive error messages at least is great 😁

src/types.ts Outdated Show resolved Hide resolved
src/__tests__/date.test.ts Outdated Show resolved Hide resolved
@Svish
Copy link

Svish commented Jul 11, 2022

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 ZodString has minLength and maxLength, so that you for example can do z.string().min(1).minLength and get 1. Could be called get minDate() and get maxDate() for instance.

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 ZodString?

  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;
  }

@Balastrong
Copy link
Contributor Author

That's indeed a valid suggestion, I'll add them :)

@colinhacks
Copy link
Owner

An immaculate PR — thanks!! 🙌

@colinhacks colinhacks merged commit ac2fb4a into colinhacks:master Jul 18, 2022
@Balastrong Balastrong deleted the feature/1089-min-max-date branch July 18, 2022 06:45
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.

Request: ZodDate min and max
4 participants