Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use maths operation with Joi.ref() for validating object using Joi?

I want to validate object using Joi which inovle use of Joi.ref() with multiplication operation.

var object = {
    a: 5,
    b: 6
}

// this is wrong as Joi.ref('a')*2 is now allowed in max()
var schema = Joi.object({
    a: Joi.number().integer(),
    b: Joi.number().integer().min(1).max(Joi.ref('a')*2)
})

Joi.ref('a')*2 is not allowed. So how can I validate object such that b<=2*a?

like image 573
Alok Avatar asked Nov 02 '25 03:11

Alok


1 Answers

Using adjust option

var schema = Joi.object({
    a: Joi.number().integer(),
    b: Joi.number().integer().min(1).max(Joi.ref('a', {
      adjust: (value) => value * 2
    }))
})

stackblitz

like image 139
User863 Avatar answered Nov 03 '25 17:11

User863



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!