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

Fix several errors relating to date validation #8345

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

ryanb-gds
Copy link
Contributor

Includes fixes for the following issues:

  • When the year was nil, a NoMethodError was being thrown because we were attempting to call "zero?" on nil. We're now rescuing from the NoMethodError and marking the date invalid
  • Entering a negative month value would trigger a MultiparameterAssigmentError, because Ruby Date constructor allows months to be set using a negative offset but Rails date assignment does not

Includes fixes for the following issues:
- When the year was nil, a NoMethodError was being thrown because we were attempting to call "zero?" on nil. We're now rescuing from the NoMethodError and marking the date invalid
- Entering a negative month value would trigger a MultiparameterAssigmentError, because Ruby Date constructor allows months to be set using a negative offset but Rails date assignment does not
@ryanb-gds ryanb-gds force-pushed the fix-invalid-date-errors branch from c7f62f9 to 51afa2f Compare October 10, 2023 10:15
raise TypeError if date[1].zero?

# Rails does not accept negative month values, but the Date constructor does
raise TypeError if (date[2]).negative?
Copy link
Contributor

@davidgisbey davidgisbey Oct 10, 2023

Choose a reason for hiding this comment

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

does the date constructor handle negative values for years and days then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, a negative year is legitimate (BC years), and yes it does prevent negative days: https://docs.ruby-lang.org/en/master/Date.html#method-c-new

Copy link
Contributor

Choose a reason for hiding this comment

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

cool thanks. I suspect the person putting in negative values was purposefully doing to try and break it 😂

LGTM!

@ryanb-gds ryanb-gds merged commit bdaa707 into main Oct 10, 2023
9 checks passed
@ryanb-gds ryanb-gds deleted the fix-invalid-date-errors branch October 10, 2023 10:39
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.

2 participants