Last active
December 17, 2018 16:09
-
-
Save dosumis/2fc95a4d4b7aa2f0d5a9856a94d75696 to your computer and use it in GitHub Desktop.
A quick demo of how to use JPATH to test whether JSON schema required fields are all valid property keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jsonpath_rw import parse | |
import json | |
vfbsf = open("./json_schema/vfb_termInfo.json", "r") | |
vfbs = json.loads(vfbsf.read()) | |
R = parse('*..required') | |
P = parse('*..properties') | |
Rs = [match for match in R.find(vfbs)] | |
Ps = [match for match in P.find(vfbs)] | |
rp_map = {} | |
for p in Ps: | |
p2r = {} | |
p2r['props'] = set(p.value.keys()) | |
p2r['required'] = set() | |
path = p.full_path | |
index = path.left | |
rp_map[str(index)] = p2r | |
for r in Rs: | |
path = r.full_path | |
rp_map[str(path.left)]['required'].update(set(r.value)) | |
for k,v in rp_map.items(): | |
if v['required'].issubset(v['props']): | |
print(k + " is valid") | |
else: | |
print(k + ' is invalid: ' + str(v['required']) + str(v['props'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment