Skip to content

Commit 3600946

Browse files
committed
Mv name from test_json_schema.py to jsonschema_test.py
Full Json schema structure test - can be integrated
1 parent 8f4a9a2 commit 3600946

2 files changed

Lines changed: 70 additions & 60 deletions

File tree

‎jsonschema_test.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import json
2+
import jsonschema
3+
from jsonschema.validators import validate
4+
5+
6+
#Great documentations at https://json-schema.org/understanding-json-schema/reference/array.html#additional-items
7+
8+
# "prefixItems" verifies that the type of the current position is the same as defined,
9+
# "items":False controls that nothing more is added (= "additional-items" previously draft/2020-12)
10+
11+
schema = {
12+
"$schema": "http://json-schema.org/draft/2020-12/schema",
13+
"type": "object",
14+
"required": [ "categorie", "titre", "questions", "difficulte"],
15+
"properties": {
16+
"categorie": {"type": "string"},
17+
"titre": {"type": "string"},
18+
"questions": {
19+
"type": "array",
20+
"default": [],
21+
"items": {
22+
"type": "object",
23+
"properties": {
24+
"titre": {"type": "string"},
25+
"choix": {
26+
"type": "array",
27+
"minItems": 2,
28+
"items": {
29+
"type": "array",
30+
"minItems": 2,
31+
"prefixItems": [{"type": "string"}, {"type": "boolean"}],
32+
"items": False
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
jsondata_test = '{' \
42+
'"categorie":"TEST",' \
43+
'"titre":"Question Test",' \
44+
'"questions":[' \
45+
' {"titre":"This is a question ?",' \
46+
' "choix":[["Answer 1",true],["Answer 2",false],["Answer 3",false],["Answer 4",false]]},' \
47+
' {"titre":"This is another question ?",' \
48+
' "choix":[["Answer 5",true],["Answer 6",false],["Answer 7",false],["Answer 8",false]]}' \
49+
'],' \
50+
'"difficulte":"intermediaire"' \
51+
'}'
52+
53+
54+
def validate_json_syntax(d):
55+
try:
56+
return json.loads(d)
57+
except ValueError:
58+
print('DEBUG: JSON data contains an error')
59+
return None
60+
61+
62+
json_data = validate_json_syntax(jsondata_test)
63+
64+
if json_data:
65+
#output will be None
66+
try:
67+
validate(json_data, schema)
68+
print('ok')
69+
except jsonschema.exceptions.ValidationError:
70+
print("Schema JSON incorrect")

‎test_json_schema.py‎

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)