多个Joi验证类型

时间:2017-01-04 16:30:20

标签: javascript node.js hapijs joi

我搜索了很多,但没有找到允许在Joi

中进行多种类型验证的内容

链接:https://github.com/hapijs/joi

我想使用这样的东西:

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

2 个答案:

答案 0 :(得分:6)

尝试:

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

或:

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

请参阅:https://github.com/hapijs/joi/blob/v10.1.0/API.md#alternatives

答案 1 :(得分:0)

export const saveDeviceCommandsSchema = {
  devices: [
    Joi.array().items(Joi.string().required()).required(),
    Joi.string().valid('all').required().lowercase()
  ],
  info: Joi.array()

}; 为对象指定多个验证规则的示例

相关问题