zod icon indicating copy to clipboard operation
zod copied to clipboard

feat: Add support for ISO-8601 Durations

Open mastermatt opened this issue 2 years ago • 5 comments

https://en.wikipedia.org/wiki/ISO_8601#Durations As an extension of the ISO standard, the format is also used in RFC 3339, XML Schema Part 2, TC39's Temporal proposal, and a format for JSON Schema strings since draft 2019-09.

~~Does not allow for negative values.~~

I believe this would close https://github.com/colinhacks/zod/issues/2725, however, it also referenced "Periods" which is how RFC 3339 (confusingly) refers to ISO 8601 Time Intervals and are not covered by this change. So I'll let @andoks decide if this covers their request fully.

mastermatt avatar Feb 23 '24 20:02 mastermatt

Deploy Preview for guileless-rolypoly-866f8a ready!

Name Link
Latest commit 4d81e8c2e82a6498eb12903d5a5c2c33fbd9a101
Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/6620600c88fa4300084fa3e0
Deploy Preview https://deploy-preview-3265--guileless-rolypoly-866f8a.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Feb 23 '24 20:02 netlify[bot]

Thanks for the feedback @andoks. I just pulled the regex used by Joi, and that was short-sighted. With ISO 8601-2 on the books, it makes sense to add the full functionality. The SO link you provided was a great resource. I've updated the regex and added more test cases. It now supports positive/negative prefixes for the whole input or individual components, and decimal values on the least significant component (allows full stop and commas per the spec).

mastermatt avatar Feb 25 '24 01:02 mastermatt

Thanks for the feedback @andoks. I just pulled the regex used by Joi, and that was short-sighted. With ISO 8601-2 on the books, it makes sense to add the full functionality. The SO link you provided was a great resource. I've updated the regex and added more test cases. It now supports positive/negative prefixes for the whole input or individual components, and decimal values on the least significant component (allows full stop and commas per the spec).

Thank you for implementing it!

Looking at the test cases, everything I would need + more is now handled :smile:

andoks avatar Feb 25 '24 10:02 andoks

As an additional thought: can this end up conflicting with a possible future feature of supporting js temporal in the future in some way do you think?

andoks avatar Feb 25 '24 10:02 andoks

@andoks my understanding of the Temporal proposal for durations is that its string representations are based on ISO 8601 itself, not a variation e.i. the XML schema spec. Therefore if there is ever any conflict it would point to a bug in this regex.

mastermatt avatar Feb 26 '24 15:02 mastermatt