无法验证 JSON 数组中的 'required' 属性
Unable to validate the 'required' properties in an array of a JSON
我在这样的文件中有一个 draft-7
JSON 架构:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"tenantid",
"owningObjectId",
"owningObject",
"cudAction",
"message"
],
"properties": {
"tenantid": {
"type": "string",
"default": ""
},
"owningObjectId": {
"type": "string",
"default": ""
},
"owningObject": {
"type": "string",
"default": "",
"pattern": "^TeamUser$"
},
"cudAction": {
"type": "string",
"default": "",
"pattern": "^c$"
},
"messageDateTime": {
"type": "string",
"default": ""
},
"encrypted": {
"type": "boolean",
"default": false
},
"message": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"teamcode",
"enabled"
],
"properties": {
"name": {
"type": "string",
"default": ""
},
"teamcode": {
"type": "string",
"default": ""
},
"org": {
"type": "string",
"default": ""
},
"enabled": {
"type": "boolean",
"default": false,
},
"version": {
"type": "string",
"default": ""
},
"orgDisplay": {
"type": "string",
"default": ""
}
}
}
}
}
}
我正在使用以下架构验证 JSON/response:
# pip install jsonschema
from jsonschema import Draft7validator
def validate_response(response, schema_file_path: str) -> bool:
"""
Validate the input message based on the input schema provided.
:reference http://json-schema.org/understanding-json-schema/
:param response: response received as JSON
:param schema_file_path: The schema file path
:return validated: returns True if valid response else False
"""
validated = True
with open(schema_file_path, "r") as schema_reader:
schema = json.loads(schema_reader.read())
errors = Draft7Validator(schema).iter_errors(response)
for error in errors:
print(error.message)
validated = False
if validated:
print(f"Valid response")
return validated
但是,对于如下所示的 JSON faulty_json_response
,其中 message
字段值数组为空并且 required
属性的 none 存在于message
字段,验证器不会抛出任何错误。可能是什么原因?
faulty_json_response = {
"tenantid": "5e3bb57222b49800016b666f",
"owningObjectId": "5e680018ceb7d600012e4375",
"owningObject": "TeamUser",
"cudAction": "c",
"messageDateTime": "1584460716.01416",
"encrypted": false,
"message": [],
}
如果需要更多详细信息,请告诉我。谢谢。
items
关键字将子模式值(包括我们所需的)应用于适用数组中的每个项目(在您的情况下为 message
)。
鉴于您在 message
数组中没有任何项目,因此未应用子模式,因此您不会收到任何验证错误。
如果要指定数组的项目数最少,可以使用 minItems
关键字...
The value of this keyword MUST be a non-negative integer.
An array instance is valid against "minItems" if its size is
greater than, or equal to, the value of this keyword.
Omitting this keyword has the same behavior as a value of 0.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.4
您可以在此现场演示中看到实际效果 https://jsonschema.dev/s/yMM0c
我在这样的文件中有一个 draft-7
JSON 架构:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"tenantid",
"owningObjectId",
"owningObject",
"cudAction",
"message"
],
"properties": {
"tenantid": {
"type": "string",
"default": ""
},
"owningObjectId": {
"type": "string",
"default": ""
},
"owningObject": {
"type": "string",
"default": "",
"pattern": "^TeamUser$"
},
"cudAction": {
"type": "string",
"default": "",
"pattern": "^c$"
},
"messageDateTime": {
"type": "string",
"default": ""
},
"encrypted": {
"type": "boolean",
"default": false
},
"message": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"teamcode",
"enabled"
],
"properties": {
"name": {
"type": "string",
"default": ""
},
"teamcode": {
"type": "string",
"default": ""
},
"org": {
"type": "string",
"default": ""
},
"enabled": {
"type": "boolean",
"default": false,
},
"version": {
"type": "string",
"default": ""
},
"orgDisplay": {
"type": "string",
"default": ""
}
}
}
}
}
}
我正在使用以下架构验证 JSON/response:
# pip install jsonschema
from jsonschema import Draft7validator
def validate_response(response, schema_file_path: str) -> bool:
"""
Validate the input message based on the input schema provided.
:reference http://json-schema.org/understanding-json-schema/
:param response: response received as JSON
:param schema_file_path: The schema file path
:return validated: returns True if valid response else False
"""
validated = True
with open(schema_file_path, "r") as schema_reader:
schema = json.loads(schema_reader.read())
errors = Draft7Validator(schema).iter_errors(response)
for error in errors:
print(error.message)
validated = False
if validated:
print(f"Valid response")
return validated
但是,对于如下所示的 JSON faulty_json_response
,其中 message
字段值数组为空并且 required
属性的 none 存在于message
字段,验证器不会抛出任何错误。可能是什么原因?
faulty_json_response = {
"tenantid": "5e3bb57222b49800016b666f",
"owningObjectId": "5e680018ceb7d600012e4375",
"owningObject": "TeamUser",
"cudAction": "c",
"messageDateTime": "1584460716.01416",
"encrypted": false,
"message": [],
}
如果需要更多详细信息,请告诉我。谢谢。
items
关键字将子模式值(包括我们所需的)应用于适用数组中的每个项目(在您的情况下为 message
)。
鉴于您在 message
数组中没有任何项目,因此未应用子模式,因此您不会收到任何验证错误。
如果要指定数组的项目数最少,可以使用 minItems
关键字...
The value of this keyword MUST be a non-negative integer.
An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword.
Omitting this keyword has the same behavior as a value of 0.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.4
您可以在此现场演示中看到实际效果 https://jsonschema.dev/s/yMM0c