使用JSON模式强制对象非空性 [英] Enforce object non emptyness using json schema

查看:100
本文介绍了使用JSON模式强制对象非空性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以如下强制实施类型为object的空属性:

We can enforce empty attribute of type object as follows:

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

此处所述.

现在我要验证哪个属性

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

强制执行非空(第4点)是我无法猜测的.如上面的示例,这与强制执行空性有些相反.我当前的json模式摘录如下:

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?

推荐答案

类似

Sounds like minProperties is what you want.

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

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

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

这篇关于使用JSON模式强制对象非空性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆