diff --git a/validate.py b/validate.py index 26f931d..9461159 100644 --- a/validate.py +++ b/validate.py @@ -16,9 +16,17 @@ try: except ImportError: 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") -def command(): +@click.option("--verbose", "-v", default=False, is_flag=True) +def command(verbose): for profile in list_profiles(): print(f"Checking {profile}:") db_path = build_database_filename(profile) @@ -32,31 +40,52 @@ def command(): if not data: print(cf.green(" Ruleset is empty")) else: + errors = 0 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 if rule.get("name"): try: re.compile(rule["name"]) 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 if rule.get("description"): try: re.compile(rule["description"]) 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 if rule.get("iban"): try: IBAN(rule.get("iban")) 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: print(cf.red(f" The rule file doesn't exists")) sys.exit(1)