使用 json 模式强制对象非空

Enforce object non emptyness using json schema

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

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

解释为 here

现在我想验证属性 which

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

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

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

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

听起来 minProperties 就是你想要的。

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

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