Why is the data validation successful?
from jsonschema import validate
schema = {
"$schema": "https://json-schema.org/draft-04/schema#",
"type": "object",
"retMsg": {
"type": "str",
"const": "OK"
},
"result": {
"type": "object",
"required": ["orderId", "orderLinkId"],
"properties": {
"orderId": {"type": "str"},
"orderLinkId": {"type": "str"}
},
},
"required": [
"retMsg",
"result"
]
}
raw_data = {
"retMsg": "OK",
"result": {
"orderId": [],
"orderLinkId": "abcde"
}
}
validate(instance=raw_data, schema=schema)
In theory, validation should be carried out - since the OrderID is a list and not a string as required by the scheme, however, everything is OK, there are no errors. It may be necessary to change the link to the schema template, but I don't understand which one I need.
Your schema is not correct, only the "required": ["retMsg","result"]
part is used. You can confirm this by checking against data with missing or misspelled keys.
properties
key.string
, not str
.After fixing your schema I got:
{
"$schema": "https://json-schema.org/draft-04/schema#",
"type": "object",
"required": [
"retMsg",
"result"
],
"properties": {
"retMsg": {
"type": "string",
"const": "OK"
},
"result": {
"type": "object",
"required": [
"orderId",
"orderLinkId"
],
"properties": {
"orderId": {
"type": "string"
},
"orderLinkId": {
"type": "string"
}
}
}
}
}
Validation:
>>> validate(instance=raw_data, schema=schema)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python39\lib\site-packages\jsonschema\validators.py", line 1312, in validate
raise error
jsonschema.exceptions.ValidationError: [] is not of type 'string'
Failed validating 'type' in schema['properties']['result']['properties']['orderId']:
{'type': 'string'}
On instance['result']['orderId']:
[]