joi icon indicating copy to clipboard operation
joi copied to clipboard

isoDuration considers fractional seconds to be invalid

Open erin-doyle opened this issue 3 years ago • 1 comments

Support plan

  • is this issue currently blocking your project? (yes/no): Yes
  • is this issue affecting a production system? (yes/no): No

Context

  • node version: v12.19.0
  • module version with issue: 17.3.0
  • last module version without issue: unknown
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi
  • any other relevant information:

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.

erin-doyle avatar May 21 '21 17:05 erin-doyle

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:

  • is a PR welcome to add support for fractions?
  • if so, is partial support (for seconds) acceptable or is full support (additionally for minutes, hours, etc.) required?

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

devinivy avatar Oct 13 '21 02:10 devinivy