【问题标题】:Joi : validate object of variable number of keysJoi:验证可变数量键的对象
【发布时间】:2018-01-13 00:38:12
【问题描述】:

我正在尝试为以下对象(关联数组)编写验证方法:

{
  "10:00": {
    discount: 10,
    time: "10:00",
  },
  "11:00": {
    discount: 11,
    time: "11:00",
  },
  ...
  ....
}

使用 Joi (https://github.com/hapijs/joi) 到目前为止我得到的是:

Joi.object().keys(
            {time:{
                discount: Joi.number(),
                time: Joi.string(),
            }}
        ),

这显然是错误的并且失败了:ValidationError: child "discounts" fails because ["10:00" is not allowed, "11:00" is not allowed]

任何人都可以建议如何为具有可变数量的键(关联数组)的对象编写验证

【问题讨论】:

    标签: json node.js joi dynogels


    【解决方案1】:

    阅读后整理:Is there a way to validate dynamic key names?

    Joi.object().pattern(/^/, [
                Joi.object({
                    discount: Joi.number(),
                    time: Joi.string()
                })
            ])
    

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 2021-10-10
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2019-12-13
      • 2016-10-11
      • 2019-07-31
      相关资源
      最近更新 更多