简体   繁体   English

JSonSchema中允许1个patternProperty的单个实例

[英]1 single instance of a patternProperty allowed in JSonSchema

I have a JsonSchema looking like this 我有一个看起来像这样的JsonSchema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "additionalProperties": false,
    "definitions": {},
    "id": "http://example.com/example.json",
    "patternProperties": {
        "^[0-9]$": {
....
....
....
        }
     }
}

What I want this Schema to validate is, if there is a JSON string with 0 or 1 of this patternProperty 我希望此模式验证的是,是否存在带有此模式的0或1的JSON字符串。

So for example 所以举个例子

{
   "0":{}
}

is a right output 是正确的输出

{
}

is also a right output 也是正确的输出

{
   "0":{},
   "1":{}
}

would be a wrong output even though the patternProperty matches of both, but there is more than 1 node 即使两者的patternProperty都匹配,也会输出错误,但是有多个节点

Is this somehow possible with JSonSchema? JSonSchema可以通过某种方式实现吗?

You can use maxProperties and minProperties . 您可以使用maxPropertiesminProperties If you want exactly one property you can do this: 如果您只需要一个属性,则可以执行以下操作:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "additionalProperties": true,
  "patternProperties": {
    "[a-z]": {}
  },
  "maxProperties": 1,
  "minProperties": 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM