|
| 1 | +import json |
| 2 | +import jsonschema |
| 3 | +import sys |
| 4 | +from .libcommons import libcommons |
| 5 | + |
| 6 | +class JSONSchemaValidator: |
| 7 | + ''' |
| 8 | + JSONSchemaValidator validates json schema against the json data file provided as parameter. |
| 9 | + ''' |
| 10 | + |
| 11 | + schema_file_path = '' |
| 12 | + data_file_path = '' |
| 13 | + |
| 14 | + def get_node_path(path): |
| 15 | + ''' |
| 16 | + get_node_path is function used to extract the json node path. |
| 17 | + ''' |
| 18 | + str_path = '' |
| 19 | + for i in path: |
| 20 | + str_path = str_path + '/' + str(i) |
| 21 | + return str_path |
| 22 | + |
| 23 | + def ValidateSchema(schema, data): |
| 24 | + ''' |
| 25 | + ValidateSchema is a function which validates schema file against the json data file provided as parameter to this function. |
| 26 | + It looks for the schema file in the same directory. |
| 27 | + ''' |
| 28 | + __schema_str = None |
| 29 | + __data_str = None |
| 30 | + __flag = False |
| 31 | + __schema_flag = True |
| 32 | + __json_flag = True |
| 33 | + __err_arr = [] |
| 34 | + try: |
| 35 | + if libcommons.path_exists(schema): |
| 36 | + with open(schema) as schema_file: |
| 37 | + __schema_str = json.load(schema_file) |
| 38 | + elif type(schema) is str: |
| 39 | + __schema_str = json.loads(schema) |
| 40 | + elif schema.__class__.__name__ in ('dict', 'dotdict'): |
| 41 | + __schema_str = schema |
| 42 | + except Exception as e: |
| 43 | + __json_flag = False |
| 44 | + #print("JSON Data file not found", '-->', e.filename) |
| 45 | + print(str(e)) |
| 46 | + try: |
| 47 | + if libcommons.path_exists(data): |
| 48 | + with open(data) as data_file: |
| 49 | + __data_str = json.load(data_file) |
| 50 | + elif type(data) is str: |
| 51 | + __data_str = json.loads(data) |
| 52 | + elif data.__class__.__name__ in ('dict', 'dotdict'): |
| 53 | + __data_str = data |
| 54 | + except Exception as e: |
| 55 | + __schema_flag = False |
| 56 | + #print("JSON Schema file not found", '-->', se.filename) |
| 57 | + print(str(e)) |
| 58 | + |
| 59 | + flag = __schema_flag and __json_flag |
| 60 | + if flag: |
| 61 | + try: |
| 62 | + error_count = 1 |
| 63 | + validation = jsonschema.Draft7Validator(__schema_str) |
| 64 | + for error in sorted(validation.iter_errors(__data_str), key=str): |
| 65 | + msg_str = '' |
| 66 | + if error_count == 1: |
| 67 | + #print('Schema Verification failed, following are the details:') |
| 68 | + error_count = error_count + 1 |
| 69 | + if 'required' in error.message: |
| 70 | + msg_str = 'Msg: Following node has a property missing from Schema.' |
| 71 | + if msg_str == '': |
| 72 | + __err_arr.append( |
| 73 | + {'type': 'Schema Mismatch', 'path': JSONSchemaValidator.get_node_path(error.absolute_path), |
| 74 | + 'error': ''.join(error.message)}) |
| 75 | + else: |
| 76 | + __err_arr.append( |
| 77 | + {'type': 'Node missing', 'path': JSONSchemaValidator.get_node_path(error.absolute_path), |
| 78 | + 'error': ''.join(error.message)}) |
| 79 | + |
| 80 | + #__err_arr = json.dumps(__err_arr, indent=4) |
| 81 | + return __err_arr |
| 82 | + except jsonschema.SchemaError as sExp: |
| 83 | + print('Bad Definition - Schema Definition Exception', sExp) |
0 commit comments