Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Tools/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#! /usr/bin/env python3
""" Script to validate YAML file(s) against a YAML schema file """

import argparse
import logging
import pprint
import sys
import os

try:
import yaml
except ImportError as e:
print("Failed to import yaml: " + str(e))
print("")
print("You may need to install it using:")
print(" pip3 install --user pyyaml")
print("")
sys.exit(1)

try:
import cerberus
except ImportError as e:
print("Failed to import cerberus: " + str(e))
print("")
print("You may need to install it using:")
print(" pip3 install --user cerberus")
print("")
sys.exit(1)


def load_data_file(file_name: str):
with open(file_name) as stream:
try:
return yaml.safe_load(stream)
except:
raise Exception("Unable to parse schema file: syntax error or unsupported file format")


def main():
parser = argparse.ArgumentParser(description='Validate YAML or JSON file(s) against a schema')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is json being handled?


parser.add_argument('data_file', nargs='+', help='data file(s)')
parser.add_argument('--schema-file', type=str, action='store',
help='schema file', required=True)
parser.add_argument('--skip-if-no-schema', dest='skip_if_no_schema',
action='store_true',
help='Skip test if schema file does not exist')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
help='Verbose Output')

args = parser.parse_args()

data_files = args.data_file
schema_file = args.schema_file

if args.verbose:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.ERROR)

if args.skip_if_no_schema and not os.path.isfile(schema_file):
logging.info(f"Schema file {schema_file} not found, skipping")
sys.exit(0)

# load the schema
schema = load_data_file(schema_file)
validator = cerberus.Validator(schema, allow_unknown=False)

# validate the specified data files
for data_file in data_files:
logging.info(f"Validating {data_file}")

document = load_data_file(data_file)

# ignore top-level entries prefixed with __ for yaml module definitions
for key in list(document.keys()):
if key.startswith('__'):
del document[key]

if not validator.validate(document):
logging.error(f"Found validation errors with {data_file}:")
logging.error(pprint.pformat(validator.errors))

raise Exception("Validation of {:} failed".format(data_file))


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions Tools/validate_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@
print("JSON validation for {:} failed (schema: {:})".format(json_file, schema_file))
Comment thread
coderkalyan marked this conversation as resolved.
Outdated
raise


76 changes: 0 additions & 76 deletions Tools/validate_yaml.py

This file was deleted.