【发布时间】:2022-08-16 13:45:52
【问题描述】:
我需要验证 json
[
{
\"relatedParty\": \"tr13\",
\"action\": \"b\"
},
{
\"relatedParty\": \"er127\"
}
]
我想验证狭窄和属性值。所以我编写了以下 json 模式:
{
\"type\": \"array\",
\"maxItems\": 4,
\"items\": {
\"type\": \"object\",
\"oneOf\": [
{
\"properties\": {
\"relatedParty\": {
\"type\": \"string\",
\"pattern\": \"tr\\\\d{2}\"
},
\"action\": {
\"type\": \"string\",
\"pattern\": \"a\"
}
}
},
{
\"properties\": {
\"relatedParty\": {
\"type\": \"string\",
\"pattern\": \"er\\\\d{3}\"
}
}
}
]
}
}
但如果我有错误的价值行动我收到两者的验证消息相关方和行动特性。
谁能解释为什么我收到 2 条验证消息而不是 1 条,以及如何调整我的验证模式以仅获得 1 条消息?
更新:
尝试使用以下 json-schema 验证相同的 json-object:
{
\"type\": \"array\",
\"maxItems\": 4,
\"items\": {
\"type\": \"object\",
\"properties\": {
\"relatedParty\": {
\"type\": \"string\",
\"oneOf\": [
{
\"pattern\": \"tr\\\\d{2}\"
},
{
\"pattern\": \"er\\\\d{3}\"
}
]
}
},
\"$defs\": {
\"tr-requires-action\": {
\"if\": {
\"properties\": {
\"relatedParty\": {
\"pattern\": \"tr\\\\d{2}\"
}
},
\"required\": [
\"relatedParty\"
]
},
\"then\": {
\"properties\": {
\"action\": {
\"pattern\": \"a\"
}
},
\"required\": [
\"action\"
]
}
}
}
}
}
然后我\'没有发现错误。 JSON 对 schema\' 消息进行验证。但是\'action\'属性的差异仍然存在。
标签: json jsonschema