Multiple Joi validation types

23.6k views Asked by At

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

There are 2 answers

0
rsp On

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

0
DLoneDeveloper On
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