Open snebjorn opened 8 months ago
ISO 8601 is indeed a large spec with various features, that might not fit in a single validator. Can you elaborate a bit on why you would want to validate duration input and why it would be a good addition to this project?
I can try :)
Currently this library doesn't support duration inputs - I was unable to find a suitable validator
Users have to fallback on regex and matches
which does work. But writing the regex isn't straight forward. I found this regex on stack overflow and it's quite a mouthful
^P(?!$)(\d+(?:\.\d+)?Y)?(\d+(?:\.\d+)?M)?(\d+(?:\.\d+)?W)?(\d+(?:\.\d+)?D)?(T(?=\d)(\d+(?:\.\d+)?H)?(\d+(?:\.\d+)?M)?(\d+(?:\.\d+)?S)?)?$
And that only handles the P[n]Y[n]M[n]DT[n]H[n]M[n]S
format. There are alternative formats. PnW
and P<date>T<time>
.
All in all it's not trivial to validate an ISO 8601 duration input.
As to why someone would want to use ISO 8601 duration is because it's a simple why to describe a duration 😄 and it's an ISO standard. It's not concerned with time zones. It's a simple way to describe; 5 min, 1 hour or 1 day, etc
I'm having this issue too. In my use case, I need to describe media objects like audio/video which have a length defined as a duration.
Recommend adding an isISO8601Duration
function for duration.
isISO8601
appears to only check dates. But the ISO 8601 spec includes a bunch of different formats.https://github.com/validatorjs/validator.js/blob/4197b8632522818164cc05d8d6cb44c98eb3decd/src/lib/isISO8601.js#L39
The
isISO8601
validator name is a bit misleading as it suggests that it support the entire ISO 8601 spec. I discovered this wanting to use it to validate an ISO 8601 duration input, example:P3Y6M4DT12H30M5S
.So I'd like to request support for ISO 8601 durations. I'd humbly suggest a name change for this validator. It can be split into
isISO8601Date
,isISO8601Duration
, etc