Skip to content

fix(isISO8601): validate years before 1000 in strict mode#2779

Open
DruckerE wants to merge 1 commit into
validatorjs:masterfrom
DruckerE:fix/isISO8601-years-before-1000
Open

fix(isISO8601): validate years before 1000 in strict mode#2779
DruckerE wants to merge 1 commit into
validatorjs:masterfrom
DruckerE:fix/isISO8601-years-before-1000

Conversation

@DruckerE

Copy link
Copy Markdown

Description

Fixes #2517.

When strict: true, isISO8601 rejected otherwise-valid dates whose year is below 1000 (for example 0001-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 a Number:

const match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
const year = match[1]; // "0001" -> 1

The ISO 8601 regex guarantees a 4-digit year, but the Number cast drops the leading zeros. The date string was then rebuilt as `${year}-${monthString}-${dayString}`1-01-01, which new Date() misparses, so the d.getUTCFullYear() === year round-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:

const yearString = `000${year}`.slice(-4);
const d = new Date(`${yearString}-${monthString || '01'}-${dayString || '01'}`);

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 existing strict = true suite. Full suite passes (318 passing).

@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3d2f4b3) to head (f76d789).

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.
📢 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.

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