LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 3 | import logging |
efiacor | 125390b | 2020-05-06 01:14:00 +0100 | [diff] [blame] | 4 | |
LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 5 | from jsonschema import validate, ValidationError, SchemaError |
efiacor | 125390b | 2020-05-06 01:14:00 +0100 | [diff] [blame] | 6 | from simplejson import load |
LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | class JsonValidatorLibrary(object): |
| 10 | |
| 11 | def __init__(self): |
| 12 | pass |
| 13 | |
efiacor | 125390b | 2020-05-06 01:14:00 +0100 | [diff] [blame] | 14 | @staticmethod |
| 15 | def validate(schema_path, json_path): |
| 16 | logging.info("Schema path: " + schema_path) |
| 17 | logging.info("JSON path: " + json_path) |
LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 18 | schema = None |
| 19 | data = None |
| 20 | try: |
efiacor | 125390b | 2020-05-06 01:14:00 +0100 | [diff] [blame] | 21 | schema = load(open(schema_path, 'r')) |
| 22 | data = load(open(json_path, 'r')) |
LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 23 | 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 | |
efiacor | 125390b | 2020-05-06 01:14:00 +0100 | [diff] [blame] | 37 | |
LiamBurke | 4d438ce | 2019-02-14 13:24:52 +0000 | [diff] [blame] | 38 | if __name__ == '__main__': |
| 39 | lib = JsonValidatorLibrary() |
efiacor | 125390b | 2020-05-06 01:14:00 +0100 | [diff] [blame] | 40 | # sys.exit(JsonValidatorLibrary().validate(sys.argv[1], sys.argv[2])) |