Skip to content

Commit

Permalink
Add yml validation schema generation to rules2yml and schema checker …
Browse files Browse the repository at this point in the history
…in osi_rules

Signed-off-by: ClemensLinnhoff <clemens.linnhoff@partner.bmw.de>
  • Loading branch information
ClemensLinnhoff committed Mar 13, 2024
1 parent f2a0b8b commit fede1af
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 122 deletions.
21 changes: 21 additions & 0 deletions osivalidator/osi_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from ruamel.yaml import YAML
from pathlib import Path
import yamale

import osi_rules_implementations

Expand All @@ -31,6 +32,25 @@ def __init__(self):
"orientation_acceleration",
}

def validate_rules_yml(self, file=None):
"""Validate rule yml files against schema."""

# Read schema file
directory = os.path.dirname(file)
filename, file_extension = os.path.splitext(os.path.basename(file))
schema_file = directory + os.sep + "schema" + os.sep + filename + "_schema.yml"
if os.path.exists(schema_file):
schema = yamale.make_schema(schema_file)
else:
print(f"WARNING: No schema file found for {file}.\n")
return

# Create a Data object
data = yamale.make_data(file)

# Validate data against the schema. Throws a ValueError if data is invalid.
yamale.validate(schema, data)

def from_yaml_directory(self, path=None):
"""Collect validation rules found in the directory."""

Expand All @@ -42,6 +62,7 @@ def from_yaml_directory(self, path=None):
try:
for filename in os.listdir(path):
if filename.startswith("osi_") and filename.endswith(exts):
self.validate_rules_yml(os.path.join(path, filename))
self.from_yaml_file(os.path.join(path, filename))

except FileNotFoundError:
Expand Down
Loading

0 comments on commit fede1af

Please sign in to comment.