Skip to content

fix(isISO8601): accept years below 1000 in strict mode - #2965

Open
Ultroncn wants to merge 1 commit into
validatorjs:masterfrom
Ultroncn:fix/isISO8601-strict-year-below-1000
Open

Ultroncn wants to merge 1 commit into
validatorjs:masterfrom
Ultroncn:fix/isISO8601-strict-year-below-1000

Conversation

@Ultroncn

Copy link
Copy Markdown

Hi! Thanks for maintaining validator.js. I found a small bug in strict isISO8601 and would love your review.

Problem (fixes #2517)

isValidDate builds new Date('${year}-${month}-${day}') with the year as captured, e.g. new Date('1-01-13'). V8 parses that as 2013-01-01, so the getUTCFullYear() === year check always fails for years below 1000:

validator.isISO8601('0001-01-13', {strict: true}) // false, should be true
validator.isISO8601('0004-02-29', {strict: true}) // false, should be true
validator.isISO8601('0099-12-31', {strict: true}) // false, should be true

Fix

Pad the year to 4 digits before building the date string (same slice(-4) style already used for month/day).

Tests

Added strict-mode cases for 0001-01-13, 0001-01-13T00:00:00.000Z, 0004-02-29, 0099-12-31 (valid) and 0001-02-29, 0099-02-29 (invalid). New tests fail on the old code and pass with the fix. npm test (build + lint + full suite) passes: 323 passing.

isValidDate built the comparison Date from an unpadded year, so
new Date('1-01-13') parsed as 2013-01-01 and every valid date with a
year below 1000 failed the strict check. Pad the year to four digits
before constructing the Date.

Fixes validatorjs#2517.
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9ff3424) to head (5b0b57e).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2965   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2599      2600    +1     
  Branches       658       658           
=========================================
+ Hits          2599      2600    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

isISO8601 does not support dates with year inferior to 1000 in strict mode

1 participant