Json 如果模式和 json 文件的属性不同,模式不会失败
Json schema does not fail if properties of schema and json file are different
正在使用https://www.jsonschemavalidator.net/
我有一个架构:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acmes catalog",
"type": "object",
"additionalProperties": false,
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"birthday": {
"type": "string",
"format": "date-time"
},
"category": {
"type": "string",
"enum": [
"Driver Qual",
"Medical Info",
"Personel",
"Personnel",
"Prev Emp Inquiry",
"Drug-Alcohol"
]
},
"address": {
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
},
"country": {
"type": "string"
}
}
}
},
"required": [
"first_name",
"last_name"
]
}
和一个 json 文件是:
{
"first_name": "George",
"last_name": "Washington",
"birthday": "2018-11-13T20:20:39+00:00",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States",
"category": "Personel2"
}
}
这通过了验证,但是如果我将 json 文件更改为:
(即将类别位置移至生日下方)
它失败了(正如我所期望的,Personel2 不在枚举列表中)
{
"first_name": "George",
"last_name": "Washington",
"birthday": "2018-11-13T20:20:39+00:00",
"category": "Personel2",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}
为什么类别在文件末尾时不会失败?
我认为属性的顺序无关紧要?
当您将类别移动到地址时,它会通过验证,因为对于编译器而言,此类别 属性 没有任何验证,它只是地址 [=] 的额外字符串 属性 18=]。如果您也将类别添加到地址 属性,则验证将失败,或者您可以将 "additionalProperties": false 添加到地址
验证
"address": {
"type": "object",
"additionalProperties": false,
在这种情况下,向地址添加额外的类别 属性 会出现错误消息“属性 'category' 尚未定义,架构不允许其他属性”。
但是,当您将类别移动到根目录时,验证失败,因为根验证模式包含类别 属性,它应该是一个枚举。
正在使用https://www.jsonschemavalidator.net/
我有一个架构:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/product.schema.json",
"title": "Product",
"description": "A product from Acmes catalog",
"type": "object",
"additionalProperties": false,
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"birthday": {
"type": "string",
"format": "date-time"
},
"category": {
"type": "string",
"enum": [
"Driver Qual",
"Medical Info",
"Personel",
"Personnel",
"Prev Emp Inquiry",
"Drug-Alcohol"
]
},
"address": {
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
},
"country": {
"type": "string"
}
}
}
},
"required": [
"first_name",
"last_name"
]
}
和一个 json 文件是:
{
"first_name": "George",
"last_name": "Washington",
"birthday": "2018-11-13T20:20:39+00:00",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States",
"category": "Personel2"
}
}
这通过了验证,但是如果我将 json 文件更改为: (即将类别位置移至生日下方) 它失败了(正如我所期望的,Personel2 不在枚举列表中)
{
"first_name": "George",
"last_name": "Washington",
"birthday": "2018-11-13T20:20:39+00:00",
"category": "Personel2",
"address": {
"street_address": "3200 Mount Vernon Memorial Highway",
"city": "Mount Vernon",
"state": "Virginia",
"country": "United States"
}
}
为什么类别在文件末尾时不会失败? 我认为属性的顺序无关紧要?
当您将类别移动到地址时,它会通过验证,因为对于编译器而言,此类别 属性 没有任何验证,它只是地址 [=] 的额外字符串 属性 18=]。如果您也将类别添加到地址 属性,则验证将失败,或者您可以将 "additionalProperties": false 添加到地址 验证
"address": {
"type": "object",
"additionalProperties": false,
在这种情况下,向地址添加额外的类别 属性 会出现错误消息“属性 'category' 尚未定义,架构不允许其他属性”。
但是,当您将类别移动到根目录时,验证失败,因为根验证模式包含类别 属性,它应该是一个枚举。