我需要根据http://json-schema.org/定义的规范编写JSON。但我正在为所需的/强制的属性验证而奋斗。下面是我编写的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"]
}发布于 2015-03-10 06:02:57
如果您的目标是告诉您“我希望至少有一个成员存在”,那么使用minProperties
{
    "type": "object",
    "etc": "etc",
    "minProperties": 1
}还请注意,如果您还希望在存在此成员或该成员时存在其他约束,则可以使用"dependencies"产生很大的效果。
发布于 2015-06-21 00:22:21
{
  ...
  "anyOf": [
    { "required": ["city"] },
    { "required": ["address"] },
    { "required": ["zipcode"] },
  ]
}如果恰好存在一个属性,则使用"oneOf“
https://stackoverflow.com/questions/28936710
复制相似问题