Python jsonschema不验证ref文件中的必需属性



我正在尝试使用"$ref"它指向一个外部文件。我的问题是"要求"。外部文件中的属性在请求中不存在时不会验证失败。根据下面的信息,我本以为我的请求会失败,因为'ofld2'丢失了。如果我从父模式中省略任何必需的字段,它就会正常失败。如有任何帮助/指导,将不胜感激。

父Json文件:test_ref . Json:

{
"test_refs":{
"schema": {
"allOf": [
{
"type": "object",
"properties": {
"fld1": {
"type": "string"
},
"fld2": {
"type": "string"
},
"fld3": {
"type": "string"
},
"ref1": { 
"$ref":"myref.json"
}
},
"required": ["fld1", "fld2", "fld3", "ref1"]
}
]
}
}
}

参考Json文件:

{
"myrefs": {
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"id":"myref",
"type": "object",
"properties": {
"ofld1": {"type": "string"},
"ofld2": {"type": "string"},
"ofld3": {"type": "string"}
},
"required": ["ofld1", "ofld2"]
}
}
}

验证逻辑:

req_schema = resource_config.get("schema", {})
schema = jsonschema.Draft4Validator(req_schema)
try:
json_content = jsonref.loads(content)
except Exception as error:
is_valid = False
log.error(error)
myerrors.append("EXCEPTION Error: invalid message payload.")

请求数据:

{'fld1': '10.0', 'fld2': '10.0', 'fld3': 'test', 'ref1': {'ofld1': 'test 1', 'ofld3': 'test  3'}}

模式:

{
'schema': {
'allOf': [
{'type': 'object', 
'properties': 
{'fld1': {'type': 'string'}, 
'fld2': {'type': 'string'},
'fld3': {'type': 'string'}, 
'ref1': {
'myrefs': {
'schema': {
'$schema': 'http://json-schema.org/draft-04/schema#', 
'id': 'myref', 
'type': 'object', 
'properties': {
'ofld1': {'type': 'string'}, 
'ofld2': {'type': 'string'}, 
'ofld3': {'type': 'string'}
}, 
'required': ['ofld1', 'ofld2']}
}
}
}, 
'required': ['fld1', 'fld2', 'fld3', 'ref1']}
]
}, 
'resolver': <jsonschema.validators.    RefResolver object at 0xb0e4526c>, 'format_checker': None}

更新:下面是一个测试脚本,它使用以下文件产生以下输出:

import json
import jsonref
import jsonschema
import logging
from os.path import dirname
from jsonschema import validate

def read_json_file(file_path):
"""Reads the file at the file_path and returns the contents.
Returns:
json_content: Contents of the json file
"""
try:
json_content_file = open(file_path, 'r')
except IOError as error:
print(error)
else:
try:
base_path = dirname(file_path)
base_uri = 'file://{}/'.format(base_path)
json_schema = jsonref.load(json_content_file, base_uri=base_uri, jsonschema=True)
except (ValueError, KeyError) as error:
print(file_path)
print(error)
json_content_file.close()
return json_schema
def validate_json_file(json_data):
req_schema = read_json_file('/path/to/json/files/test_refs.json')
print("SCHEMA --> "+str(req_schema))
try:
validate(instance=json_data, schema=req_schema)
except jsonschema.exceptions.ValidationError as err:
print(err)
err = "Given JSON data is InValid"
return False, err
message = "Given JSON data is Valid"
return True, message

jsonData = jsonref.loads('{"fld1": "10.0", "fld2": "10.0", "fld3": "test", "ref1": {"ofld1": "test 1", "ofld3": "test  3"}}')
is_valid, msg = validate_json_file(jsonData)
print(msg)

模式不正确。/schema/allOf/0/properties/ref1下的所有内容都不是有效的模式,因此将全部忽略。您应该将嵌入的模式提升多个级别,并将其放在ref1下面。

您包含的前两个模式在任何地方都没有引用;它们只是你所创建的嵌入所有内容的模式的副本吗?

最新更新