我有以下JSON:
{
"inputs": [
{
"term": {
"units": "liters",
"termType": "material"
}
},
{
"term": {
"units": "kilogram",
"termType": "crop"
}
}
]
}
我想验证:
- 如果
inputs
中任何元素具有termType=material
- 那么
units
应该是kilogram
所以我把验证写成这样:
{
"allOf": [
{
"if": {
"required": [
"inputs"
],
"properties": {
"inputs": {
"items": {
"properties": {
"term": {
"properties": {
"termType": {
"const": "material"
}
}
}
}
}
}
}
},
"then": {
"properties": {
"inputs": {
"items": {
"properties": {
"term": {
"properties": {
"units": {
"const": "kilogram"
}
}
}
}
}
}
}
}
}
]
}
我正在使用ajv版本6来验证(启用了draft 07),并且它仅在inputs
数组中只有一个元素时有效,因此如果我在
{
"inputs": [
{
"term": {
"units": "liters",
"termType": "material"
}
}
]
}
它会给出一个错误,但是不使用数组中有两个元素的顶部的样本。为什么?
1条答案
按热度按时间rlcwz9us1#
一般来说,在JSON Schema中,像这样的约束是这样定义的: