YSH
YSH

Reputation: 65

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()
})

Upvotes: 1

Views: 613

Answers (1)

Bhumit 070
Bhumit 070

Reputation: 426

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()
})

Upvotes: 2

Related Questions