Multiple Joi validation types

泄露秘密 提交于 2020-06-25 09:33:07

问题


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

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/41468779/multiple-joi-validation-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!