Better validation of rule file
This commit is contained in:
parent
65c0aac1e2
commit
725759a421
1 changed files with 35 additions and 6 deletions
41
validate.py
41
validate.py
|
@ -16,9 +16,17 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from yaml import Loader, Dumper
|
from yaml import Loader, Dumper
|
||||||
|
|
||||||
|
ALLOWED_KEYS = ["name", "tags", "iban", "description"]
|
||||||
|
TAGS_REGEXP = re.compile(r'[\w]+(?:,\s{0,1}[\w]+)*')
|
||||||
|
|
||||||
|
def verbose_print(text, verbose):
|
||||||
|
if verbose:
|
||||||
|
print(text)
|
||||||
|
|
||||||
|
|
||||||
@click.command(name="validate")
|
@click.command(name="validate")
|
||||||
def command():
|
@click.option("--verbose", "-v", default=False, is_flag=True)
|
||||||
|
def command(verbose):
|
||||||
for profile in list_profiles():
|
for profile in list_profiles():
|
||||||
print(f"Checking {profile}:")
|
print(f"Checking {profile}:")
|
||||||
db_path = build_database_filename(profile)
|
db_path = build_database_filename(profile)
|
||||||
|
@ -32,31 +40,52 @@ def command():
|
||||||
if not data:
|
if not data:
|
||||||
print(cf.green(" Ruleset is empty"))
|
print(cf.green(" Ruleset is empty"))
|
||||||
else:
|
else:
|
||||||
|
errors = 0
|
||||||
for rule in data:
|
for rule in data:
|
||||||
# TODO: validate tag list
|
# validate yaml keys:
|
||||||
|
for key in rule.keys():
|
||||||
|
if key not in ALLOWED_KEYS:
|
||||||
|
verbose_print(cf.red(f"Rule uses the invalid key '{key}': {rule}"),verbose)
|
||||||
|
errors+=1
|
||||||
|
|
||||||
|
if not rule.get("tags"):
|
||||||
|
verbose_print(cf.red(f"Rule has no tags: {rule}"),verbose)
|
||||||
|
errors+=1
|
||||||
|
|
||||||
|
if not TAGS_REGEXP.fullmatch(rule.get("tags")):
|
||||||
|
verbose_print(cf.red(f"Rule has an invalid list of tags: {rule}"),verbose)
|
||||||
|
errors+=1
|
||||||
|
|
||||||
|
|
||||||
# validate name regex
|
# validate name regex
|
||||||
if rule.get("name"):
|
if rule.get("name"):
|
||||||
try:
|
try:
|
||||||
re.compile(rule["name"])
|
re.compile(rule["name"])
|
||||||
except:
|
except:
|
||||||
print(cf.red(f" Invalid name regex: '{rule.get('name')}'"))
|
verbose_print(cf.red(f" Invalid name regex: '{rule.get('name')}'"),verbose)
|
||||||
|
errors+=1
|
||||||
|
|
||||||
# validate description regex
|
# validate description regex
|
||||||
if rule.get("description"):
|
if rule.get("description"):
|
||||||
try:
|
try:
|
||||||
re.compile(rule["description"])
|
re.compile(rule["description"])
|
||||||
except:
|
except:
|
||||||
print(cf.red(f" Invalid description regex: '{rule.get('description')}'"))
|
verbose_print(cf.red(f" Invalid description regex: '{rule.get('description')}'"),verbose)
|
||||||
|
errors+=1
|
||||||
|
|
||||||
# validate IBAN
|
# validate IBAN
|
||||||
if rule.get("iban"):
|
if rule.get("iban"):
|
||||||
try:
|
try:
|
||||||
IBAN(rule.get("iban"))
|
IBAN(rule.get("iban"))
|
||||||
except:
|
except:
|
||||||
print(cf.red(f" Invalid IBAN: '{rule.get('iban')}'"))
|
verbose_print(cf.red(f" Invalid IBAN: '{rule.get('iban')}'"), verbose)
|
||||||
|
errors+=1
|
||||||
|
|
||||||
|
if errors == 0:
|
||||||
|
print(cf.green(f" All rules are valid"))
|
||||||
|
else:
|
||||||
|
print(cf.red(f" Found {errors} invalid rules"))
|
||||||
|
|
||||||
print(cf.green(f" All rules are valid"))
|
|
||||||
else:
|
else:
|
||||||
print(cf.red(f" The rule file doesn't exists"))
|
print(cf.red(f" The rule file doesn't exists"))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
Loading…
Add table
Reference in a new issue