コード例 #1
0
def last_element(self, field, rule):
    """Check rule for last message of a repeated field.

    :param field: Field to which the rule needs to comply
    :param rule: dictionary of rules to be checked for the last message
                   (mapping)
    """
    statement_true = True
    nested_fields_rules = rule.params

    # Convert parsed yaml file to dictonary rules
    nested_fields_rules_list = []
    for key_field, nested_rule in nested_fields_rules.items():
        nested_rule[0].update({"target": "this." + key_field})
        nested_fields_rules_list.append(nested_rule[0])

    rules_checker_list = []
    for nested_fields_rule in nested_fields_rules_list:
        statement_rule = Rule(
            dictionary=nested_fields_rule,
            field_name=rule.field_name,
            severity=Severity.ERROR,
        )
        statement_rule.path = rule.path.child_path(statement_rule.verb)
        statement_true = self.check_rule(field[-1],
                                         statement_rule) and statement_true
        rules_checker_list.append(statement_true)

    return all(rules_checker_list)
コード例 #2
0
def check_if(self, field, rule):
    """
    Evaluate rules if some statements are verified:

    :param params: statements
    :param extra_params: `do_check`: rules to validate if statements are true

    Structure:

    a_field:
    - check_if:
    {params: statements}
    do_check:
    {extra_params: rules to validate if statements are true}


    Example:

    a_field:
    - check_if:
    - is_set: # Statements
    target: parent.environment.temperature
    - another_statement: statement parameter
    do_check: # Check that will be performed only if the statements are True
    - is_less_than_or_equal_to: 0.5
    - is_greater_than_or_equal_to: 0

    """
    statements = rule.params
    do_checks = rule.extra_params["do_check"]
    statement_true = True

    # Check if all the statements are true
    for statement in statements:
        statement_rule = Rule(dictionary=statement,
                              field_name=rule.field_name,
                              severity=Severity.INFO)
        statement_rule.path = rule.path.child_path(statement_rule.verb)
        statement_true = self.check_rule(field,
                                         statement_rule) and statement_true

    # If the statements are true, check the do_check rules
    if not statement_true:
        return True

    return all((self.check_rule(
        field,
        Rule(
            path=rule.path.child_path(next(iter(check.keys()))),
            dictionary=check,
            field_name=rule.field_name,
        ),
    ) for check in do_checks))