【问题标题】:Multiple Joi validation types多种 Joi 验证类型
【发布时间】:2017-05-19 00:53:01
【问题描述】:

我搜索了很多,但没有发现在Joi 中允许进行多种类型验证

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

我想用这样的东西:

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

【问题讨论】:

  • my post below 回答您的问题了吗?如果是这样,那么您可以考虑accepting the answer,以便其他人在搜索此主题时看到它已得到回答。

标签: javascript node.js hapijs joi


【解决方案1】:

试试:

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

【讨论】:

    【解决方案2】:
    export const saveDeviceCommandsSchema = {
      devices: [
        Joi.array().items(Joi.string().required()).required(),
        Joi.string().valid('all').required().lowercase()
      ],
      info: Joi.array()
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2018-09-24
      • 2018-02-03
      相关资源
      最近更新 更多