Joi:验证可变数量的键的对象

时间:2017-08-05 02:04:05

标签: json node.js joi dynogels

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

{
  "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]

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

1 个答案:

答案 0 :(得分:0)

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

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