aboutsummaryrefslogtreecommitdiffstats
path: root/tests/usecases/5G-bulkpm/resources/JsonValidatorLibrary.py
blob: 12d5d8566f12ca19ae60fc93b4c304138ab020ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-

import sys
import logging
from simplejson import load
from jsonschema import validate, ValidationError, SchemaError


class JsonValidatorLibrary(object):

    def __init__(self):
        pass

    def validate(self, schemaPath, jsonPath):
        logging.info("Schema path: " + schemaPath)
        logging.info("JSON path: " + jsonPath)
        schema = None
        data = None
        try:
            schema = load(open(schemaPath, 'r'))
            data = load(open(jsonPath, 'r'))
        except (IOError, ValueError, OSError) as e:
            logging.error(e.message)
            return 1

        try:
            validate(data, schema)
        except (ValidationError, SchemaError) as e:
            logging.error(e.message)
            return 1

        # logger.log("JSON validation successful")
        print("JSON validation successful")
        return 0

if __name__ == '__main__':
    lib = JsonValidatorLibrary()
    # sys.exit(JsonValidatorLibrary().validate(sys.argv[1], sys.argv[2]))