为什么 JSON-validation 总是成功使用这个包含 'allOf' 的模式?
Why is JSON-validation always successful using this schema containing 'allOf'?
我有一个 JSON 模式,我想使用 python 和 jsonschema 模块验证一些数据。然而,这并不像预期的那样有效,因为一些接受的数据似乎根本无效(对我和我的应用程序的目的)。遗憾的是,提供了架构,所以我无法更改架构本身 - 至少不能手动更改。
这是架构的简化版本(以下代码中的'schema.json'):
{
"type": "object",
"allOf": [
{
"type": "object",
"allOf": [
{
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"language": {
"type": "integer"
}
}
}
]
},
{
"type": "object",
"properties": {
"addressArray": {
"type": "array",
"items": {
"type": "object",
"properties": {
"streetNumber": {
"type": "string"
},
"street": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
}
}
}
]
}
这是一个有效实例的示例(以下代码中的'person.json'):
{
"firstName": "Sherlock",
"lastName": "Holmes",
"language": 1,
"addresses": [
{
"streetNumber": "221B",
"street": "Baker Street",
"city": "London"
}
]
}
这是应视为无效内容的示例(以下代码中的'no_person.json'):
{
"name": "eggs",
"colour": "white"
}
这是我用来验证的代码:
from json import load
from jsonschema import Draft7Validator, exceptions
with open('schema.json') as f:
schema = load(f)
with open('person.json') as f:
person = load(f)
with open('no_person.json') as f:
no_person = load(f)
validator = Draft7Validator(schema)
try:
validator.validate(person)
print("person.json is valid")
except exceptions.ValidationError:
print("person.json is invalid")
try:
validator.validate(no_person)
print("no_person.json is valid")
except exceptions.ValidationError:
print("no_person.json is invalid")
结果:
person.json is valid
no_person.json is valid
我预计 no_person.json 无效。怎么才能只有person.json这样的数据验证成功呢?非常感谢您的帮助,我对此很陌生(花了很长时间寻找答案)。
这是工作模式并注意 "required"(当没有这样的键时 - 如果字段没有得到它只是跳过):
{
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"language": {
"type": "integer"
},
"addresses": {
"type": "array",
"items": {
"type": "object",
"properties": {
"streetNumber": {
"type": "string"
},
"street": {
"type": "string"
},
"city": {
"type": "string"
}
},
"required": [
"streetNumber",
"street",
"city"
]
}
}
},
"required": [
"firstName",
"lastName",
"language",
"addresses"
]
}
我有:
person.json is valid
no_person.json is invalid
如果您有最难的响应结构(对象数组,其中包含对象等)让我知道
我有一个 JSON 模式,我想使用 python 和 jsonschema 模块验证一些数据。然而,这并不像预期的那样有效,因为一些接受的数据似乎根本无效(对我和我的应用程序的目的)。遗憾的是,提供了架构,所以我无法更改架构本身 - 至少不能手动更改。
这是架构的简化版本(以下代码中的'schema.json'):
{
"type": "object",
"allOf": [
{
"type": "object",
"allOf": [
{
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
},
{
"type": "object",
"properties": {
"language": {
"type": "integer"
}
}
}
]
},
{
"type": "object",
"properties": {
"addressArray": {
"type": "array",
"items": {
"type": "object",
"properties": {
"streetNumber": {
"type": "string"
},
"street": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
}
}
}
]
}
这是一个有效实例的示例(以下代码中的'person.json'):
{
"firstName": "Sherlock",
"lastName": "Holmes",
"language": 1,
"addresses": [
{
"streetNumber": "221B",
"street": "Baker Street",
"city": "London"
}
]
}
这是应视为无效内容的示例(以下代码中的'no_person.json'):
{
"name": "eggs",
"colour": "white"
}
这是我用来验证的代码:
from json import load
from jsonschema import Draft7Validator, exceptions
with open('schema.json') as f:
schema = load(f)
with open('person.json') as f:
person = load(f)
with open('no_person.json') as f:
no_person = load(f)
validator = Draft7Validator(schema)
try:
validator.validate(person)
print("person.json is valid")
except exceptions.ValidationError:
print("person.json is invalid")
try:
validator.validate(no_person)
print("no_person.json is valid")
except exceptions.ValidationError:
print("no_person.json is invalid")
结果:
person.json is valid
no_person.json is valid
我预计 no_person.json 无效。怎么才能只有person.json这样的数据验证成功呢?非常感谢您的帮助,我对此很陌生(花了很长时间寻找答案)。
这是工作模式并注意 "required"(当没有这样的键时 - 如果字段没有得到它只是跳过):
{
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"language": {
"type": "integer"
},
"addresses": {
"type": "array",
"items": {
"type": "object",
"properties": {
"streetNumber": {
"type": "string"
},
"street": {
"type": "string"
},
"city": {
"type": "string"
}
},
"required": [
"streetNumber",
"street",
"city"
]
}
}
},
"required": [
"firstName",
"lastName",
"language",
"addresses"
]
}
我有:
person.json is valid
no_person.json is invalid
如果您有最难的响应结构(对象数组,其中包含对象等)让我知道