必需属性上的json模式问题

时间:2015-03-09 07:06:14

标签: json-schema-validator

我需要根据http://json-schema.org/定义的规范编写JSON Schema。但我正在努力争取必要/强制性的财产验证。下面是我写的JSON模式,其中所有3个属性都是必需的,但在我的情况下,任何一个属性都必须是。怎么做?。

{
    "id": "http://example.com/searchShops-schema#",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "searchShops Service",
    "description": "",
    "type": "object",
    "properties": {     
            "city":{
                "type": "string"                
            },  
            "address":{
                "type": "string"                
            },      
            "zipCode":{
                "type": "integer"
            }                   
    },
    "required": ["city", "address", "zipCode"]
}

2 个答案:

答案 0 :(得分:0)

如果您的目标是告诉“我希望至少有一名成员存在”,请使用minProperties

{
    "type": "object",
    "etc": "etc",
    "minProperties": 1
}

另请注意,如果您还希望在此成员或该成员出现时存在其他约束,则可以使用"dependencies"

答案 1 :(得分:0)

{
  ...
  "anyOf": [
    { "required": ["city"] },
    { "required": ["address"] },
    { "required": ["zipcode"] },
  ]
}

或者使用" oneOf"如果只有一个属性应该存在

相关问题