17

I search a lot but nothing found to allow multiple type validation in Joi

Link: https://github.com/hapijs/joi

I would like to use something like this:

validate: {
    type: joi.or([
        joi.string(),
        joi.array(),
    ])
};
Mr.Orange
  • 426
  • 1
  • 4
  • 8
  • Did [my post below](https://stackoverflow.com/questions/41468779/multiple-joi-validation-types/41468932#41468932) answer your question? If so then you can consider [accepting the answer](http://meta.stackexchange.com/a/5235/157646) so that other people see that it's answered when they search for this topic. – rsp Jan 09 '17 at 22:00

2 Answers2

30

Try:

validate: {
    type: joi.alternatives().try(joi.string(), joi.array())
}

or:

validate: {
    type: [joi.string(), joi.array()]
}

See: https://github.com/hapijs/joi/blob/v10.1.0/API.md#alternatives

rsp
  • 107,747
  • 29
  • 201
  • 177
1
export const saveDeviceCommandsSchema = {
  devices: [
    Joi.array().items(Joi.string().required()).required(),
    Joi.string().valid('all').required().lowercase()
  ],
  info: Joi.array()
};

example specifying more than a validation rule to an object

Dykotomee
  • 728
  • 6
  • 20