Search code examples
joi

Pass an array of valid values to Joi.valid()


I'm tyring to valid req.body, and the valid values is one item from the array.

Is there a way to do that?

Code:

updatePersonSchema = Joi.object({
    personName: Joi.string().min(2).required(),
    updateFiled: Joi.string().valid(personParams).required(), //oneOf the personParams
    value: Joi.required()
})

Solution

  • Yes you can pass an array to a valid function like this

    const personParams = ['name', 'email', 'password']
    
    updatePersonSchema = Joi.object({
        personName: Joi.string().min(2).required(),
        updateFiled: Joi.string().valid(...personParams).required(), //oneOf the personParams
        value: Joi.required()
    })