Multiple Joi validation types

Mr.Orange picture Mr.Orange · Jan 4, 2017 · Viewed 10.3k times · Source

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(),
    ])
};

Answer

rsp picture rsp · Jan 4, 2017

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