others - javascript JSON架构:"additionalProperties"的"allof"


{


"$schema":"http://json-schema.org/draft-04/schema#",



"definitions": {


"address": {


"type":"object",


"properties": {


"street_address": {"type":"string" },


"city": {"type":"string" },


"state": {"type":"string" }


 },


"required": ["street_address","city","state"]


 }


 },



"type":"object",



"properties": {


"billing_address": {"$ref":"#/definitions/address" },


"shipping_address": {


"allOf": [


 {"$ref":"#/definitions/address" },


 {"properties":


 {"type": {"enum": ["residential","business" ] } },


"required": ["type"]


 }


 ]


 } 



 }


}



下面是有效的实例:


{


"shipping_address": {


"street_address":"1600 Pennsylvania Avenue NW",


"city":"Washington",


"state":"DC",


"type":"business"


 }


}



需要确保shipping_address的附加字段都无效,但是,当我按以下的方式设置"additionalProprties":false时:


"shipping_address": {


"allOf": [


 {"$ref":"#/definitions/address" },


 {"properties":


 {"type": {"enum": ["residential","business" ] } },


"required": ["type"]


 }


 ],


"additionalProperties":false


 } 



我得到验证错误(已检查这里):


[ {


"level" :"error",


"schema" : {


"loadingURI" :"#",


"pointer" :"/properties/shipping_address"


 },


"instance" : {


"pointer" :"/shipping_address"


 },


"domain" :"validation",


"keyword" :"additionalProperties",


"message" :"additional properties are not allowed",


"unwanted" : ["city","state","street_address","type" ]


} ] 



问题是:如何仅限制shipping_address部分的字段?

时间:

你已经发现了JSON Schema中最常见的问题,即它无法像用户期望那样进行继承;但这是它核心特性之一。

当你执行以下操作时:


"allOf": [ {"schema1":"here" }, {"schema2":"here" } ]



然后,shipping_address的架构将为:


{


"merge": {


"source": {"$ref":"#/definitions/address" },


"with": {


"properties": {


"type": {"enum": ["residential","business" ] }


 }


 }


 }


}



address中定义strictProperties到。


{


"extends": {"type":"null" },


"type":"string"


}



现在是这样:


{


"allOf": [ {"type":"string" }, {"type":"null" } ]


}



它们是一样的或者?


{


"anyOf": [ {"type":"string" }, {"type":"null" } ]


}



或者?


{


"oneOf": [ {"type":"string" }, {"type":"null" } ]


}



你所遇到的问题是最常见的问题。

这意味着当你拥有:


 {


"allOf": [


 {"$ref":"#/definitions/address" },


 {"properties":


 {"type": {"enum": ["residential","business" ] } },


"required": ["type"]


 }


 ],


"additionalProperties":false


 }



你可以做的一件事是将properties定义移动一级,并为要导入的属性提供存根条目:


 {


"allOf": [{"$ref":"#/definitions/address"}],


"properties": {


"type": {"enum": ["residential","business"]},


"addressProp1": {},


"addressProp2": {},


 ...


 },


"required": ["type"],


"additionalProperties":false


 }




{


"$schema":"http://json-schema.org/draft-04/schema#",


"definitions": {


"address": {


"type":"object",


"properties": {


"street_address": {


"type":"string"


 },


"city": {


"type":"string"


 },


"state": {


"type":"string"


 }


 },


"required": [


"street_address",


"city",


"state"


 ]


 }


 },


"type":"object",


"properties": {


"billing_address": {


"$ref":"#/definitions/address"


 },


"shipping_address": {


"allOf": [


 {


"$ref":"#/definitions/address"


 }


 ],


"properties": {


"type": {


"enum": [


"residential",


"business"


 ]


 },


"street_address": {},


"city": {},


"state": {}


 },


"required": [


"type"


 ],


"additionalProperties": false


 }


 }


}



这将保留基address架构中必需属性的验证,并在shipping_address中添加必需的type属性。


{ 


"definitions": {


"address": {


"type":"object",


"properties": {


"street_address": {"type":"string" },


"city": {"type":"string" },


"state": {"type":"string" }


 }


 }


 },



"type":"object",


"properties": {



"billing_address": {


"allOf": [


 {"$ref":"#/definitions/address" }


 ],


"properties": {


"street_address": {},


"city": {},


"state": {} 


 }, 


"additionalProperties": false


"required": ["street_address","city","state"] 


 },



"shipping_address": {


"allOf": [


 {"$ref":"#/definitions/address" },


 {


"properties": {


"type": {


"enum": ["residential","business"]


 }


 }


 }


 ],


"properties": {


"street_address": {},


"city": {},


"state": {},


"type": {} 


 }, 


"additionalProperties": false


"required": ["street_address","city","state","type"] 


 }



 }


}



每个billing_addressshipping_address都应指定它们自己的必需属性。

...