validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

isISO8601 doesn't support year with more than 4 digits

Open This-is-a-Book opened this issue 1 year ago • 3 comments

https://github.com/validatorjs/validator.js/blob/f074abdd851d56c8425bc14aec41049eb26b041e/src/lib/isISO8601.js#L5

We have a code that would use maximum date possible in JS

let maxDate = new Date(8640000000000000);
let minDate = new Date(-8640000000000000);

Those would be converted to the following from toISOString() function

// maxDate
+275760-09-13T00:00:00.000Z
// minDate
-275760-09-13T00:00:00.000Z

However, when using isISO8601 validator, they're invalid because the year has more than 4 digits.

Would it be possible to fix the validator to allow year with more than 4 digits?

This-is-a-Book avatar Aug 11 '23 03:08 This-is-a-Book

const iso8601 = /^([+-]?\d{4, }(?!\d will puting comma after 4 work , ??

Siddhant-Kashyap avatar Aug 14 '23 13:08 Siddhant-Kashyap

const iso8601 = /^([+-]?\d{4, }(?!\d will puting comma after 4 work , ??

@Siddhant-Kashyap For my case, yes, this would work.

This-is-a-Book avatar Aug 15 '23 02:08 This-is-a-Book

I think this is intended "default" behaviour as per the standard:

but your use case also is valid, but must be handled a bit more special, when it comes to IS8601:

https://en.wikipedia.org/wiki/ISO_8601#Years

To represent years before [0000] or after [9999], the standard also permits the expansion of the year representation but only by prior agreement between the sender and the receiver. An expanded year representation [±YYYYY] must have an agreed-upon number of extra year digits beyond the four-digit minimum, and it must be prefixed with a + or − sign instead of the more common AD/BC (or CE/BCE) notation; by convention 1 BC is labelled +0000, 2 BC is labeled −0001, and so on.

And this, IMHO, should rather be something that should be done as an option, because the more common use case will be to deal with those 4 digit years - and if the RegEx is changed to also - by default - allow 4 and more digits - this will break a few validations, that depend on this.

pano9000 avatar Sep 29 '23 10:09 pano9000