Json 使用 AND 进行模式验证
Json schema validation with AND
我正在尝试验证此示例 json
{
"wrapper": {
"contact_number": "1662626872123",
"dataset": {
"param": [
{
"id": "customer_profile_type",
"value": "0"
},
{
"id": "sssssssssssssssssss",
"value": "1"
}
]
}
}
}
我试图使 contact_number 仅在数组包含 id==customer_profile_type AND value==0
时才需要
我试过了
{"wrapper": {
"if": {
"properties": {
"dataset": {
"properties": {
"param": {
"items": {
"contains": {
"enum": [{ "id": "customer_profile_type","value":"0"}]
}
}
}
}
}
}
},
"then": {
"properties": {
"contact_number": {
"maxLength": 10
}
}
}
}
请帮我解决这个问题
提前致谢
你必须使用 required
:
{
"properties": {
"wrapper": {
"if": {
"properties": {
"dataset": {
"properties": {
"param": {
"contains": {
"properties": {
"id": {
"enum": [
"customer_profile_type"
]
},
"value": {
"enum": [
"0"
]
}
},
"required": [
"id",
"value"
]
}
}
}
}
}
},
"then": {
"properties": {
"contact_number": {
"maxLength": 10
}
},
"required": [
"contact_number"
]
}
}
}
}
您可以在 https://www.jsonschemavalidator.net/s/Co3pwxfs
试试这个
为了更好地理解 JSON 模式阅读 https://json-schema.org/understanding-json-schema/index.html
我正在尝试验证此示例 json
{
"wrapper": {
"contact_number": "1662626872123",
"dataset": {
"param": [
{
"id": "customer_profile_type",
"value": "0"
},
{
"id": "sssssssssssssssssss",
"value": "1"
}
]
}
}
}
我试图使 contact_number 仅在数组包含 id==customer_profile_type AND value==0
时才需要我试过了
{"wrapper": {
"if": {
"properties": {
"dataset": {
"properties": {
"param": {
"items": {
"contains": {
"enum": [{ "id": "customer_profile_type","value":"0"}]
}
}
}
}
}
}
},
"then": {
"properties": {
"contact_number": {
"maxLength": 10
}
}
}
}
请帮我解决这个问题
提前致谢
你必须使用 required
:
{
"properties": {
"wrapper": {
"if": {
"properties": {
"dataset": {
"properties": {
"param": {
"contains": {
"properties": {
"id": {
"enum": [
"customer_profile_type"
]
},
"value": {
"enum": [
"0"
]
}
},
"required": [
"id",
"value"
]
}
}
}
}
}
},
"then": {
"properties": {
"contact_number": {
"maxLength": 10
}
},
"required": [
"contact_number"
]
}
}
}
}
您可以在 https://www.jsonschemavalidator.net/s/Co3pwxfs
试试这个为了更好地理解 JSON 模式阅读 https://json-schema.org/understanding-json-schema/index.html