数组中的 Enum在json-schema中无效



我正在用json_schema验证json。

ghrBillingCode允许的值应该只有"I9NOT">

预期结果应该是错误的,因为第二个和第三个节点不是I9NOT,但它正在验证json是正确的。

我正在使用

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"invoiceLineInfo": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"ghrBillingCode": {
"type": "string",
"enum": [
"I9NOT"
]
},
"quantity": {
"type": "integer"
}
}
}
]
},
"invoiceNumber": {
"type": "string"
}
}
}
]
}

json:

[
{
"invoiceLineInfo":[
{
"ghrBillingCode":"I9NOT",
"quantity":1
},
{
"ghrBillingCode":"I9NOTRU",
"quantity":2
},
{
"ghrBillingCode":"I9PSUP",
"quantity":1
}
],
"invoiceNumber":"202203010100301"
}
]

在模式中,在items数组类型周围有额外的括号[]。这意味着枚举只检查第一个数组元素,并且您的示例验证,因为第一项恰好是"I9NOT"

从您的示例文档中,似乎您希望枚举适用于所有数组元素。要实现这一点,只需从items值中删除[]。

关于数组/项的语法,请看这里:https://json-schema.org/understanding-json-schema/reference/array.html项目

最新更新