1 回答

TA貢獻2080條經驗 獲得超4個贊
正如評論中指出的那樣,這可能已被多次詢問。
下面的技巧、模式和 json 示例使用邏輯運算符。我不能說它是否更好,它只是將“anyOf”中的任何內容分組(以及您可以在需要時添加的郵政編碼的要求,并且您可以根據需要構建對完整“屬性”內部的引用等) .). 或者您可以將其作為嚴格的異或“oneOf”(也如評論中所述)并確保每個案例的地址->屬性定義都按照您的需要進行調整。
請閱讀(鏈接到我自己過去的回答并不優雅,但它可能會引導您進一步閱讀):JSON Schema conditional: require and not require
您的問題的簡化架構和示例(據我了解):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties" : {
"address": {
"type" : "object",
"anyOf" : [
{
"properties" : {
"house_number" : {
"type":"string",
"maxLength": 4
},
},
"required":["house_number"]
},
{
properties : {
"house_name" : {
"type" : "string",
"maxLength" : 50
}
},
"required":["house_name"]
}
]
}
},
required: ["address"],
examples : [
{
baddress: {
}
},
{
address: {
"house_number":"1234",
"house_name" : null
}
},
{
address: {
"house_number":null,
"house_name" : null
}
},
{
address: {
"house_number":null,
"house_name" : "some name"
}
},
{
address: {
"house_number": "12345",
"house_name" : "some afafafasagagagffgfsagragsgasgasssssssfdgsdfgsdfgdsgsdfgsdgsdfgdfsgsdfgs name"
}
},
]
}
添加回答
舉報