Skip to content

Commit

Permalink
Add separate function for validating YAML
Browse files Browse the repository at this point in the history
Signed-off-by: John Pennycook <[email protected]>
  • Loading branch information
Pennycook committed Jan 26, 2024
1 parent 1842cb9 commit 7256a84
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 1 addition & 2 deletions codebasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,7 @@ def load(config_file, rootdir):
raise RuntimeError(f"Could not open {config_file!s}.")

# Validate config against a schema
# We don't use any advanced features of YAML, so can use JSON here
util._validate_json(config, schema_name="config")
util._validate_yaml(config, schema_name="config")

# Read codebase definition
if "codebase" in config:
Expand Down
32 changes: 32 additions & 0 deletions codebasin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ def _validate_json(json_object: object, schema_name: str) -> bool:
return True


def _validate_yaml(yaml_object: object, schema_name: str) -> bool:
"""
Validate YAML against a schema.
Parameters
----------
yaml_object : Object
The YAML to validate.
schema_name : {'config'}
The schema to validate against.
Returns
-------
bool
True if the YAML is valid.
Raises
------
ValueError
If the YAML fails to validate, or the schema name is unrecognized.
RuntimeError
If the schema file cannot be located.
"""
if schema_name != "config":
raise ValueError("Unrecognized schema name.")

# We don't use any advanced features of YAML, so can use JSON here
return _validate_json(yaml_object, schema_name)


def _load_json(file_object: typing.TextIO, schema_name: str) -> object:
"""
Load JSON from file and validate it against a schema.
Expand Down

0 comments on commit 7256a84

Please sign in to comment.