LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | import sys |
| 4 | import logging |
| 5 | from simplejson import load |
| 6 | from jsonschema import validate, ValidationError, SchemaError |
| 7 | |
| 8 | |
| 9 | class JsonValidatorLibrary(object): |
| 10 | |
| 11 | def __init__(self): |
| 12 | pass |
| 13 | |
| 14 | def validate(self, schemaPath, jsonPath): |
| 15 | logging.info("Schema path: " + schemaPath) |
| 16 | logging.info("JSON path: " + jsonPath) |
| 17 | schema = None |
| 18 | data = None |
| 19 | try: |
| 20 | schema = load(open(schemaPath, 'r')) |
| 21 | data = load(open(jsonPath, 'r')) |
| 22 | except (IOError, ValueError, OSError) as e: |
| 23 | logging.error(e.message) |
| 24 | return 1 |
| 25 | |
| 26 | try: |
| 27 | validate(data, schema) |
| 28 | except (ValidationError, SchemaError) as e: |
| 29 | logging.error(e.message) |
| 30 | return 1 |
| 31 | |
| 32 | # logger.log("JSON validation successful") |
| 33 | print("JSON validation successful") |
| 34 | return 0 |
| 35 | |
| 36 | if __name__ == '__main__': |
| 37 | lib = JsonValidatorLibrary() |
| 38 | # sys.exit(JsonValidatorLibrary().validate(sys.argv[1], sys.argv[2])) |