@ Relequestual помог мне решить эту проблему вне SO.Мне вообще не нужно было использовать select
, я мог просто использовать if/then/else
примерно так:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"contactByPost": {
"type": "boolean"
},
"streetAddress1": {
"type": ["string", "null"]
},
"streetAddress2": {
"type": ["string", "null"]
},
"streetAddress3": {
"type": ["string", "null"]
},
"locality": {
"type": ["string", "null"]
},
"region": {
"type": ["string", "null"]
},
"postCode": {
"type": ["string", "number", "null"]
},
"country": {
"type": ["string", "null"]
}
},
"if": {
"type": "object",
"properties": {
"contactByPost": {
"type": "boolean",
"const": true
}
}
},
"then": {
"if": {
"type": "object",
"properties": {
"country": {
"type": "string",
"enum": ["JP", "US"]
}
}
},
"then": {
"type": "object",
"properties": {
"contactByPost": { "type": "boolean" },
"streetAddress1": { "type": "string" },
"locality": { "type": "string" },
"region": { "type": "string" },
"postCode": { "type": ["string", "number"] },
"country": { "type": "string" }
},
"required": ["streetAddress1", "locality", "region", "postCode", "country"]
},
"else": {
"type": "object",
"properties": {
"contactByPost": { "type": "boolean" },
"streetAddress1": { "type": "string" },
"locality": { "type": "string" },
"region": { "type": ["string", "null"] },
"country": { "type": "string" }
},
"required": ["streetAddress1", "locality", "country"]
}
}
}
Даст следующие результаты:
// Valid
{
"contactByPost": false
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": "State",
"postCode": "12345",
"country": "JP"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": "State",
"postCode": "12345",
"country": "US"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": "State",
"postCode": "12345",
"country": "NZ"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": null,
"postCode": null,
"country": "NZ"
}
// Invalid
{
"contactByPost": true
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": null,
"postCode": null,
"country": "JP"
}
{
"contactByPost": true,
"streetAddress1": "123 Alphabet St",
"streetAddress2": null,
"streetAddress3": null,
"locality": "City",
"region": null,
"postCode": null,
"country": "US"
}