-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate.py
More file actions
67 lines (57 loc) · 1.79 KB
/
Copy pathvalidate.py
File metadata and controls
67 lines (57 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import argparse
from jsonschema import validate
# Describe what kind of json you expect.
schema1 = {
"type": "object",
"properties": {
"unit_name": {"type" : "string"},
"devices" : {
"type" : "array",
"minItems" : 1,
"items" : {
"type" : "object",
"properties" : {
"capacity" : {"type" : "string"},
"storage_type" : {"type" : "string"}
},
"required" : ["capacity", "storage_type"]
}
}
},
"required": ["unit_name", "devices"]
}
""" # this JSON instance is not a valid object because there is no key for "2017-c.2"
instance = {
"2003-xyz.2": [
{
"capacity" : "239849820954321",
"storage_type" : "block"
}
]
} """
# this is a valid JSON object because there is now a key for both 2017-c.2
# and for the devices contained within that unit
correct_instance = {
"unit_name" : "2003-xyz.2",
"devices" : [
{
"capacity" : "239849820954321",
"storage_type" : "block"
},
{
"capacity" : "459835489",
"storage_type" : "block"
}
]
}
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="read in a JSON file with the specified filename")
args = parser.parse_args()
with open(args.filename) as f:
instance = f.read()
correct_instance = json.loads(instance)
# if the schema is valid and the instance matches the schema
# there will be no exceptions raised
validate(instance=correct_instance, schema=schema1)
print("JSON object present in ", args.filename, " is valid according to specified JSON schema")