使用Python和jsonschema
我試圖驗證的ObjA
或ObjB
等來beta
(test.json
)如何指定JSON對象應該採用哪一個項目?
{
"alpha": {
"beta": "ObjA"
}
}
分配在我的架構(testschema.json
)beta
是oneOf
多個項目,每個項目被定義爲下面(與a
,b
不同的值,並且c
)
"ObjA": {
"type": "object",
"properties": {
"items": {
"a": [90, 95],
"b": [4, 8],
"c": [0.2, 0.6]
}
},
"additionalProperties": false
}
也就是說,beta
可以採用oneOf
值爲ObjA
,ObjB
,ObjC
和ObjD
。我只是想說明它應該在test.json
"alpha": {
"type": "object",
"properties": {
"beta": {
"oneOf": [
{
"type": "object",
"properties": {
"ObjA": {
"type": "object",
"properties": {
"items": {
"a": [90, 95],
"b": [4, 8],
"c": [0.2, 0.6]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"ObjB": {
"type": "object",
"properties": {
"items": {
"a": [100],
"b": [0],
"c": [0]
}
},
"additionalProperties": false
}
}
},
...
ObjC and ObjD defined
...
}
}
}
},
但是使用哪一個,試圖驗證針對該架構在使用jsonschema.validate()
### Test the whole JSON is valid against the Schema
def test_valid__JSON_against_schema(self):
with open(schema_filename) as schema_file:
test_schema = json.load('testschema.json')
schema_file.close()
with open(json_filename) as json_file:
test_json = json.load('test.json')
json_file.close()
validate(test_json, test_schema)
我收到以下錯誤
Failed validating 'oneOf' in schema['properties']['alpha']['properties']['beta']:
這裏是整個消息
E
======================================================================
ERROR: test_valid__JSON_against_schema (__main__.SchemaTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_test-variables.py", line 35, in test_valid__JSON_against_schema
validate(test_json, test_schema)
File "/local/tools/PACKAGES/python3/lib/python3.6/site-packages/jsonschema/validators.py", line 541, in validate
cls(schema, *args, **kwargs).validate(instance)
File "/local/tools/PACKAGES/python3/lib/python3.6/site-packages/jsonschema/validators.py", line 130, in validate
raise error
jsonschema.exceptions.ValidationError: 'ObJA' is not valid under any of the given schemas
Failed validating 'oneOf' in schema['properties']['alpha']['properties']['beta']:
{'oneOf': [{'additionalProperties': False,
'properties': {'ObjA': {'additionalProperties': False,
'properties': {'items': {'a': [0.2, 0.6],
'b': [90, 95],
'c': [4, 8]}},
'type': 'object'}},
'type': 'object'},
{'additionalProperties': False,
'properties': {'ObjB': {'additionalProperties': False,
'properties': {'items': {'a': [0],
'b': [100],
'c': [0]}},
'type': 'object'}},
'type': 'object'},
{'additionalProperties': False,
'properties': {'ObjC': {'additionalProperties': False,
'properties': {'items': {'a': [0],
'b': [50],
'c': [50]}},
'type': 'object'}},
'type': 'object'},
{'additionalProperties': False,
'properties': {'ObjD': {'additionalProperties': False,
'properties': {'items': {'a': [100],
'b': [0],
'c': [0]}},
'type': 'object'}},
'type': 'object'}]}
On instance['alpha']['beta']:
'ObjA'
----------------------------------------------------------------------
Ran 1 test in 0.007s
FAILED (errors=1)
使用在線jsonschema
驗證器(http://json-schema-validator.herokuapp.com/)test.json
未驗證,所以我從文件中刪除了任何提及的alpha
(即,這個{ }
)和驗證報告以下
[ {
"level" : "warning",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta/oneOf/0/properties/ObjA/properties/items"
},
"domain" : "syntax",
"message" : "the following keywords are unknown and will be ignored: [a, b, c]",
"ignored" : [ "a", "b", "c" ]
} ]
恢復test.json
回,確認給
[ {
"level" : "warning",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta/oneOf/0/properties/ObjA/properties/items"
},
"domain" : "syntax",
"message" : "the following keywords are unknown and will be ignored: [a, b, c]",
"ignored" : [ "a", "b", "c" ]
}, {
"level" : "warning",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta/oneOf/0/properties/ObjA/properties/items"
},
"domain" : "syntax",
"message" : "the following keywords are unknown and will be ignored: [a, b, c]",
"ignored" : [ "a", "b", "c" ]
}, {
"level" : "warning",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta/oneOf/0/properties/ObjA/properties/items"
},
"domain" : "syntax",
"message" : "the following keywords are unknown and will be ignored: [a, b, c]",
"ignored" : [ "a", "b", "c" ]
}, {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta"
},
"instance" : {
"pointer" : "/alpha/beta"
},
"domain" : "validation",
"keyword" : "oneOf",
"message" : "instance failed to match exactly one schema (matched 0 out of 1)",
"matched" : 0,
"nrSchemas" : 1,
"reports" : {
"/properties/alpha/properties/beta/oneOf/0" : [ {
"level" : "warning",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta/oneOf/0/properties/ObjA/properties/items"
},
"domain" : "syntax",
"message" : "the following keywords are unknown and will be ignored: [a, b, c]",
"ignored" : [ "a", "b", "c" ]
}, {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/alpha/properties/beta/oneOf/0"
},
"instance" : {
"pointer" : "/alpha/beta"
},
"domain" : "validation",
"keyword" : "type",
"message" : "instance type (string) does not match any allowed primitive type (allowed: [\"object\"])",
"found" : "string",
"expected" : [ "object" ]
} ]
}
} ]
有誰知道這樣做的正確方法是什麼?
謝謝。
幾乎不可能不知道你想驗證的JSON實例是什麼樣的。因爲你正在動態生成它可能是任何東西。 –
文本開始 - 「測試版」:「ObjA」 - 我編輯了句子和JSON,使其更清晰。 – Dodomac
是的,這正是問題所在。嘗試根據模式驗證「某些內容」時,您會收到驗證錯誤。你試圖驗證的「東西」是失敗的驗證。不幸的是,我們不可能看到*爲什麼*它失敗了(因此能夠提供一些幫助),因爲我們不知道它是什麼樣子。 –