Enforce object non emptyness using json schema

依然范特西╮ 提交于 2019-12-07 01:43:59

问题


We can enforce empty attribute of type object as follows:

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

as explained here.

Now I want to validate attribute which

  1. is of object type,
  2. dont have any predefined properties
  3. can have properties of type string or numeric
  4. should not be empty

Enforcing non-emptyness (point 4) is what I am unable to guess. This is somewhat opposite of enforcing emptyness as in above example. My current json schema excerpt looks like this:

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

But above does not enforce non-emptyness. How can I accomplish this?


回答1:


Sounds like minProperties is what you want.

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

There is also maxProperties, which can be used as an alternative solution to the opposite question that you linked to.



来源:https://stackoverflow.com/questions/35651589/enforce-object-non-emptyness-using-json-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!