hapijs / joi

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

Multi flag on some rules #2457

Open zuozp8 opened 4 years ago

zuozp8 commented 4 years ago

Context

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

In joi v15 i could apply greater contraint twice and both were evaluated during validation:

const schema = Joi.object({
  from: Joi.date().required(),
  to: Joi.date().greater(Joi.ref('from')).greater('now').required()
})
schema.validate({from: '2099-05-02', to: '2099-05-01'})
// { error: Error [ValidationError]: child "to" fails because ["to" must be greater than "Sat May 02 2099 00:00:00 GMT+0000 (Coordinated Universal Time)"]…

currently in v17.2 this validation passes, as greater('now') overwrites greater(Joi.ref('from'))

I looked into the code and I see flag multi: true in some rules like any.custom or 'string.pattern', it should be documented.

For example those rules could get badge multi [![multi](https://img.shields.io/badge/multi-blue.svg)](#multi) pointing to new seciton in docs about overwriting/appending rules

brianle1301 commented 4 years ago

@zuozp8 Great idea, although I'm not sure about the badges.

hueniverse commented 4 years ago

Just need to add a line to each method indicating if it is a multi rule or override rule. There is also the question of whether greater() and similar should support multi.

brianle1301 commented 4 years ago

@hueniverse I would love to work on this.

hueniverse commented 4 years ago

@brianle1301 go ahead and make the docs changes. Actually changing the multi flag requires a breaking change so not going to change for now.

brianle1301 commented 4 years ago

@hueniverse Yeah I meant the doc changes.