Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isISO8601 doesn't support duration #2379

Open
snebjorn opened this issue Mar 14, 2024 · 3 comments
Open

isISO8601 doesn't support duration #2379

snebjorn opened this issue Mar 14, 2024 · 3 comments

Comments

@snebjorn
Copy link

isISO8601 appears to only check dates. But the ISO 8601 spec includes a bunch of different formats.

export default function isISO8601(str, options = {}) {

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

@WikiRik
Copy link
Member

WikiRik commented Mar 15, 2024

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?

@snebjorn
Copy link
Author

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

@TigerC10
Copy link

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.

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

No branches or pull requests

3 participants