From 25b6c84df695ed760e07b6dfd5d5e1ddaa981500 Mon Sep 17 00:00:00 2001 From: Tom Robinson <33458882+thomas-robinson@users.noreply.github.com> Date: Thu, 23 May 2024 09:35:48 -0400 Subject: [PATCH] Validate schema program (#41) * Adds a schema validaion program * Updates setup.cfg for schema validation install * Fixes typo Improves documentation for validate_schema.py * Adds better error handling for invalid yamls to be validated Updates setup to include schema folder --- fms_yaml_tools/schema/validate_schema.py | 65 ++++++++++++++++++++++++ setup.cfg | 3 ++ 2 files changed, 68 insertions(+) create mode 100755 fms_yaml_tools/schema/validate_schema.py diff --git a/fms_yaml_tools/schema/validate_schema.py b/fms_yaml_tools/schema/validate_schema.py new file mode 100755 index 0000000..be7dabb --- /dev/null +++ b/fms_yaml_tools/schema/validate_schema.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 +import click +import yaml +import json +#import jsonschema +from jsonschema import validate, ValidationError, SchemaError, Draft7Validator +""" This program is used for validating a file against a schema. The +schema is written in JSON format, so the file it is validating must be +in a JSON-like format. That means that this can be used to validate JSON +or YAML files, but the YAML files must adhere to some of the JSON rules. +The arguments of this program are the path to the file to be validated +which is ypath and the path to the schema file which is spath. +This program uses click for accepting command like arguments and options +and jsonschema to run the validation. +""" +@click.command() +# Debug is used to print more information to the screen. +@click.option('--debug/--no-debug', type=click.BOOL, show_default=True, default=False, + help="Print steps in the validation") +# This option controls whether or not the program prints a success message +@click.option('--success/--no-show-success', type=click.BOOL, show_default=True, default=False, + help="Print success message") +@click.argument("ypath") # This is the path to the file to be validated +@click.argument("spath") # This is the path to the schema to use for validation + +def valyaml (ypath,spath,debug,success): #main program + """ Validates a file (YAML or JSON) based on a schema. \n + YPATH - Path to the YAML file to be validated against the schema \n + SPATH - Path to the schema file to use for validation \n + + """ +# The debug messages are basically comments showing what the code is doing + if debug: + print ("Open "+ypath) + with open(ypath, 'r') as file: + if debug: + print ("Load "+ypath) + y = yaml.safe_load(file) + if debug: + print ("Open "+spath) + with open(spath, 'r') as f: + if debug: + print ("Read "+spath) + s = f.read() + if debug: + print ("Load "+spath) + schema = json.loads(s) + if debug: + print ("Validate "+ypath+" against "+spath) + try: + v = validate(instance=y, schema=schema) + except ValidationError as e: + print("The following errors have occurred:\n") + vr = Draft7Validator(schema) + errors = vr.iter_errors(y) + i = 1 + for err in errors: + print("("+str(i)+") "+err.message+"---"+str(err.path)) + i=i+1 + return -1 + if success or debug: + print (ypath+" was successfully validated against the schema "+spath) + +if __name__ == '__main__': + valyaml(prog_name="valyaml") diff --git a/setup.cfg b/setup.cfg index 41c307b..b40f6d5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,7 +23,9 @@ project_urls = install_requires = pyyaml click + jsonschema packages = + fms_yaml_tools/schema fms_yaml_tools/data_table fms_yaml_tools/diag_table fms_yaml_tools/field_table @@ -31,6 +33,7 @@ packages = [options.entry_points] console_scripts = + validate-schema = fms_yaml_tools.schema.validate_schema:valyaml data-table-to-yaml = fms_yaml_tools.data_table.data_table_to_yaml:main is-valid-data-table-yaml = fms_yaml_tools.data_table.is_valid_data_table_yaml:main combine-data-table-yamls = fms_yaml_tools.data_table.combine_data_table_yamls:main