hapijs / joi

The most powerful data validation library for JS
Other
20.8k stars 1.51k forks source link

isoDuration considers fractional seconds to be invalid #2612

Open erin-doyle opened 3 years ago

erin-doyle commented 3 years ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

When using the isoDuration type a ValidationError is thrown with must be a valid ISO 8601 duration when using a decimal value for the Seconds (for example: PT4.4S). Per the standard this should be a valid value:

"The smallest value used may also have a decimal fraction[citation needed], as in "P0.5Y" to indicate half a year. This decimal fraction may be specified with either a comma or a full stop, as in "P0,5Y" or "P0.5Y". "

But it looks like the regex does not allow for it:

internals.isoDuration = /^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/;

const videoDuration = `PT${parseFloat(videoMetaData.duration).toFixed(1)}S`;

await AsyncCheckInDetails.query(trx)
            .where({ checkInId: id })
            .patch({ videoDuration });

What was the result you got?

ValidationError: videoDuration: "videoDuration" must be a valid ISO 8601 duration

What result did you expect?

The Update to be successful.

devinivy commented 2 years ago

I agree that isoDuration() doesn't support the full standard 👍 I think comprehensive support for fractions could be a little bit tricky to achieve just by tweaking the regex, since the fractional part can only appear in the smallest value. Partial support for fractions of seconds seems pretty simple to add since seconds are always the smallest value if they're present.

I think the open questions here are:

In the meantime it could be useful to experiment writing a joi exension to get this working!