Support ISO 8601 duration as a string validator#1613
Support ISO 8601 duration as a string validator#1613pettyalex wants to merge 2 commits intohapijs:masterfrom
Conversation
|
Nice, exactly what I need :) Wanted to propose that myself. Thanks for writing it up. Lets hope it gets merged soon. |
|
What do we need to do to get this PR approved? Is there further work necessary? Tests are updated. |
|
Is there a way to extend joi via plugins maybe? So things like this could be released as Works quite well with other open source libs (eslint, webpack, parcel etc) A little hacky working approach would be: // joi-string-duration/index.js
const Joi = require("joi")
const JoiErrors = require("joi/lib/language")
JoiErrors.errors.string.isoDuration = "must be a valid ISO 8601 duration"
Joi.string().__proto__.isoDuration = function () {
return this._test('isoDuration', undefined, function (value, state, options) {
if (/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/.test(value)) {
return value
}
return this.createError('string.isoDuration', { value }, state, options)
})
} |
Yes! :) extensions
I will try to review later today if nobody beats me to it. |
|
Ah cool, @WesTyler |
Marsup
left a comment
There was a problem hiding this comment.
Only one comment, LGTM otherwise.
| return internals.isoDate.test(value); | ||
| } | ||
|
|
||
| _isIsoDuration(value) { |
There was a problem hiding this comment.
What is it doing in this file if it's never going to be used here?
There was a problem hiding this comment.
I didn't want to put the regex for ISO duration into the string index.js, but rather follow the existing pattern. I also don't want to reach into date's internals from the string validator.
I could just move this regex into the string validator, but I feel like it belongs next to the date regex.
There was a problem hiding this comment.
Well, since it's never going to be involved in the creation of a date, I'm not so sure. It could even be in number for all I know. I don't usually use those, how are you going to consume that data?
|
Actually the documentation for the new method and the new error are missing as well, would be great if you could add that. |
4a2de06 to
a9aa3e7
Compare
|
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
This addresses #1612 by adding iso duration as a type of string validator. I'm not sure the best design to add it on the date validator (because it's not a date), so I didn't.
This also adds a reasonable amount of unit tests to cover it. Tests and linter pass