【问题标题】:Enforce object non emptyness using json schema使用 json 模式强制对象非空
【发布时间】:2016-02-26 12:20:35
【问题描述】:

我们可以强制类型对象的空属性如下:

{
   "description": "voice mail record",
   "type": "object",
   "additionalProperties": false,
   "properties": {}
}

正如here 解释的那样。

现在我想验证哪个属性

  1. 是对象类型,
  2. 没有任何预定义的属性
  3. 可以具有字符串或数字类型的属性
  4. 不应为空

强制非空(第 4 点)是我无法猜测的。这与上面示例中的强制清空有些相反。我当前的 json 架构摘录如下所示:

"attribute": 
{
    "type": "object",
    "additionalProperties": { "type": ["string","number","integer"] }
}

但上面并没有强制非空。我怎样才能做到这一点?

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    听起来像minProperties 就是你想要的。

    {
        "type": "object",
        "additionalProperties": {"type": ["string", "number", "integer"]},
        "minProperties": 1
    }
    

    还有maxProperties,可以用作您链接到的相反问题的替代解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多