Skip to content

Commit

Permalink
Avoid propagating invalid date
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Jun 12, 2024
1 parent a5a46ae commit cf2f6f5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/util/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export function utilNormalizeDateString(raw) {
date.setUTCFullYear(parseInt((match[1] || '') + match[2], 10));
if (match[3]) date.setUTCMonth(parseInt(match[3], 10) - 1); // 0-based
if (match[4]) date.setUTCDate(parseInt(match[4], 10));
if (isNaN(date.getDate())) {
return null;
}
} else {
// Fall back on whatever the browser can parse into a date.
date = new Date(raw);
Expand Down
2 changes: 2 additions & 0 deletions test/spec/util/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ describe('iD.date', function() {
it('rejects malformed dates', function() {
expect(iD.utilNormalizeDateString('1970-01--1')).to.eql(null);
expect(iD.utilNormalizeDateString('197X')).to.eql(null); // no EDTF for now
// https://github.com/OpenHistoricalMap/issues/issues/826
expect(iD.utilNormalizeDateString('1912091095')).to.eql(null);
});
it('respects the original precision', function() {
expect(iD.utilNormalizeDateString('123').value).to.eql('0123');
Expand Down

0 comments on commit cf2f6f5

Please sign in to comment.