fix(isISO8601): validate years before 1000 in strict mode#2779
Open
DruckerE wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2779 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2587 2588 +1
Branches 656 656
=========================================
+ Hits 2587 2588 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
When `strict: true`, `isISO8601` rejected otherwise-valid dates whose
year is below 1000 (e.g. `0001-01-01`, `0099-12-31`). The `isValidDate`
helper casts the matched year to a Number, dropping the leading zeros the
ISO 8601 regex guarantees, so `new Date(`${year}-...`)` was built from a
string like `1-01-01` and misparsed, failing the round-trip check.
Pad the year back to 4 digits before constructing the Date, mirroring the
existing month/day padding. Adds regression cases to the strict suite.
Closes validatorjs#2517
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2517.
When
strict: true,isISO8601rejected otherwise-valid dates whose year is below 1000 (for example0001-01-01,0001-01-13T00:00:00.000Z,0099-12-31).Root cause
In
isValidDate(src/lib/isISO8601.js) the matched year is cast to aNumber:The ISO 8601 regex guarantees a 4-digit year, but the
Numbercast drops the leading zeros. The date string was then rebuilt as`${year}-${monthString}-${dayString}`→1-01-01, whichnew Date()misparses, so thed.getUTCFullYear() === yearround-trip check failed and the date was reported invalid.Fix
Pad the year back to 4 digits before building the
Date, mirroring the existing month/day padding:Tests
Added valid (
0001-01-01,0001-01-13T00:00:00.000Z,0099-12-31,0500-06-15) and invalid (0001-13-01,0001-01-32) cases to the existingstrict = truesuite. Full suite passes (318 passing).