admin管理员组

文章数量:1400007

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?

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?

Share Improve this question asked Nov 6, 2019 at 11:44 AlokAlok 10.7k11 gold badges70 silver badges130 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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

本文标签: javascriptHow to use maths operation with Joiref() for validating object using JoiStack Overflow