Skip to content

Commit

Permalink
Validate schema program (#41)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
thomas-robinson authored May 23, 2024
1 parent 4daee9c commit 25b6c84
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
65 changes: 65 additions & 0 deletions fms_yaml_tools/schema/validate_schema.py
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ 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
fms_yaml_tools

[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
Expand Down

0 comments on commit 25b6c84

Please sign in to comment.