blob: ed376ff2624e42a405602c37db5f165432180c26 [file] [log] [blame]
LiamBurke4d438ce2019-02-14 13:24:52 +00001# -*- coding: utf-8 -*-
2
LiamBurke4d438ce2019-02-14 13:24:52 +00003import logging
efiacor125390b2020-05-06 01:14:00 +01004
LiamBurke4d438ce2019-02-14 13:24:52 +00005from jsonschema import validate, ValidationError, SchemaError
efiacor125390b2020-05-06 01:14:00 +01006from simplejson import load
LiamBurke4d438ce2019-02-14 13:24:52 +00007
8
9class JsonValidatorLibrary(object):
10
11 def __init__(self):
12 pass
13
efiacor125390b2020-05-06 01:14:00 +010014 @staticmethod
15 def validate(schema_path, json_path):
16 logging.info("Schema path: " + schema_path)
17 logging.info("JSON path: " + json_path)
LiamBurke4d438ce2019-02-14 13:24:52 +000018 schema = None
19 data = None
20 try:
efiacor125390b2020-05-06 01:14:00 +010021 schema = load(open(schema_path, 'r'))
22 data = load(open(json_path, 'r'))
LiamBurke4d438ce2019-02-14 13:24:52 +000023 except (IOError, ValueError, OSError) as e:
24 logging.error(e.message)
25 return 1
26
27 try:
28 validate(data, schema)
29 except (ValidationError, SchemaError) as e:
30 logging.error(e.message)
31 return 1
32
33 # logger.log("JSON validation successful")
34 print("JSON validation successful")
35 return 0
36
efiacor125390b2020-05-06 01:14:00 +010037
LiamBurke4d438ce2019-02-14 13:24:52 +000038if __name__ == '__main__':
39 lib = JsonValidatorLibrary()
efiacor125390b2020-05-06 01:14:00 +010040# sys.exit(JsonValidatorLibrary().validate(sys.argv[1], sys.argv[2]))