Created
July 28, 2020 15:09
-
-
Save anchitarnav/e9927bf91c7f283db34be33f97de72a2 to your computer and use it in GitHub Desktop.
JSON Schema Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from jsonschema import Draft7Validator | |
| schema_file = '/path/to/schema_file/schema.json' | |
| json_data = dict() # as ingested | |
| # Loading schema file from data file folder | |
| with open(schema_file) as fp: | |
| json_schema = json.load(fp) | |
| # Strict Schema Check. If there are any extra keys in the JSON, the validation shall fail. | |
| if 'additionalProperties' not in json_schema: | |
| json_schema['additionalProperties'] = False | |
| validator = Draft7Validator(schema=json_schema) | |
| for error in validator.iter_errors(instance=json_data): | |
| print("######################################################") | |
| print(error) | |
| print("######################################################") | |
| if not validator.is_valid(json_data): | |
| assert False, "Schema of ingested JSON failed to adhere to the saved schema format" | |
| else: | |
| print("JSON Schema successfully verified. Passing testcase .. ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment