Как я могу сделать несколько полей «обязательными» в зависимости от условия?
Например; в приведенном ниже случае, если status==Failed, мне нужны только четыре обязательных поля ("orgId", "идентификатор подразделения", "фондАутТип", "статус") но любое другое значение status изменит обязательный список полей: ("transactionId", "Идентификатор организации", "идентификатор подразделения", "фондАутТип", "сумма вывода", "статус")
Мое решение ниже не работает. Пожалуйста помоги.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"versionId": "1.0",
"javaInterfaces": [
"java.io.Serializable"
],
"type": "object",
"properties": {
"transactionId": {
"type": "string"
},
"orgId": {
"type": "string"
},
"subunitId": {
"type": "string"
},
"fundOutType": {
"type": "string"
},
"fundOutAmount": {
"type": "string"
},
"status": {
"type": "string"
},
"lang": {
"type": "string"
},
"transactionCreatedDateTime": {
"type": "integer",
"format": "date-time"
},
"userId": {
"type": "string"
}
},
"if": {
"properties": {
"status": {
"const": "Failed"
}
},
"required": [ "status" ]
},
"then": {
"required": [
"orgId",
"subunitId",
"fundOutType",
"status"
]
},
"else": {
"required": [
"transactionId",
"orgId",
"subunitId",
"fundOutType",
"fundOutAmount",
"status"
]
}
}
1 ответ
Вы можете использовать предложение oneOf
вместо if/else:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"versionId": "1.0",
"javaInterfaces": [
"java.io.Serializable"
],
"type": "object",
"properties": {
"transactionId": {
"type": "string"
},
"orgId": {
"type": "string"
},
"subunitId": {
"type": "string"
},
"fundOutType": {
"type": "string"
},
"fundOutAmount": {
"type": "string"
},
"status": {
"type": "string"
},
"lang": {
"type": "string"
},
"transactionCreatedDateTime": {
"type": "integer",
"format": "date-time"
},
"userId": {
"type": "string"
}
},
"oneOf": [
{
"properties": {
"status": {
"const": "Failed"
}
},
"required": [
"orgId",
"subunitId",
"fundOutType",
"status"
]
},
{
"required": [
"transactionId",
"orgId",
"subunitId",
"fundOutType",
"fundOutAmount",
"status"
]
}
]
}
См. документацию https:// json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.1
Похожие вопросы
Новые вопросы
json
JSON (нотация объектов JavaScript) — это сериализуемый формат обмена данными, который может быть прочитан машиной и человеком. Не используйте этот тег для собственных объектов JavaScript или литералов объектов JavaScript. Прежде чем задать вопрос, подтвердите свой JSON с помощью валидатора JSON, такого как JSONLint (https://jsonlint.com).
if
/then
/else
.