|
| 1 | +from esdlvalidator.validation.functions import utils |
| 2 | +from esdlvalidator.validation.functions.function import ( |
| 3 | + FunctionFactory, |
| 4 | + FunctionCheck, |
| 5 | + FunctionDefinition, |
| 6 | + ArgDefinition, |
| 7 | + FunctionType, |
| 8 | + CheckResult, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +@FunctionFactory.register(FunctionType.CHECK, "attributes_are_valid") |
| 13 | +class AttributesNotSet(FunctionCheck): |
| 14 | + |
| 15 | + def get_function_definition(self): |
| 16 | + return FunctionDefinition( |
| 17 | + "attributes_are_valid", |
| 18 | + "Check if attribute(s) or nested reference attribute(s) value matches the value(s) in count_as_valid." |
| 19 | + "Currently, only string type is supported, but can be extended to support other types.", |
| 20 | + [ |
| 21 | + ArgDefinition( |
| 22 | + "reference_path", |
| 23 | + "A dot-separated string representing the reference path. For instance, costInformation.investmentCosts" |
| 24 | + "If an empty string is given, it refers to the parent entity.", |
| 25 | + True, |
| 26 | + ), |
| 27 | + ArgDefinition( |
| 28 | + "checks", |
| 29 | + "A list of dictionaries with structure as {attribute: str, count_as_valid: str | list[str]} to be checked." |
| 30 | + "Use 'Unset' as count_as_valid value to indicate the attribute should stay unset (null).", |
| 31 | + True, |
| 32 | + ), |
| 33 | + ArgDefinition("resultMsgJSON", "Display output in JSON format", False), |
| 34 | + ], |
| 35 | + ) |
| 36 | + |
| 37 | + def execute(self): |
| 38 | + value = self.value |
| 39 | + msg = {"offending_asset": self.value.id} |
| 40 | + |
| 41 | + reference_path = utils.get_attribute(self.args, "reference_path") |
| 42 | + checks = utils.get_attribute(self.args, "checks") |
| 43 | + |
| 44 | + results = [] |
| 45 | + unset_keyword = "Unset" |
| 46 | + |
| 47 | + if not isinstance(reference_path, str): |
| 48 | + raise TypeError( |
| 49 | + f"Invalid function argument. Argument 'reference_path' should be a string, got {type(checks)}." |
| 50 | + ) |
| 51 | + |
| 52 | + if not isinstance(checks, list): |
| 53 | + raise TypeError(f"Invalid function argument. Argument 'checks' should be a list, got {type(checks)}.") |
| 54 | + |
| 55 | + for dict in checks: |
| 56 | + if "attribute" not in dict or "count_as_valid" not in dict: |
| 57 | + raise ValueError("Missing required keys: 'attribute' and/or 'count_as_valid'.") |
| 58 | + |
| 59 | + attr = dict["attribute"] |
| 60 | + count_as_valid = dict["count_as_valid"] |
| 61 | + |
| 62 | + if not isinstance(attr, str): |
| 63 | + raise TypeError(f"'attribute' must be a string, got {type(attr)}.") |
| 64 | + |
| 65 | + if isinstance(count_as_valid, str): |
| 66 | + count_as_valid = [count_as_valid] |
| 67 | + |
| 68 | + if not all(isinstance(item, str) for item in count_as_valid): |
| 69 | + raise TypeError("'count_as_valid' must be a list of strings") |
| 70 | + |
| 71 | + attr_path = f"{reference_path}.{attr}" if reference_path else attr |
| 72 | + attr_value = utils.get_attr_or_ref_attr(value, attr_path) |
| 73 | + |
| 74 | + if attr_value == "Not found": |
| 75 | + raise ValueError(f"Attribute [{attr}] not found.") |
| 76 | + |
| 77 | + if attr_value == unset_keyword: |
| 78 | + if unset_keyword not in count_as_valid: |
| 79 | + results.append( |
| 80 | + f"[{attr}] should be {'one of ' if len(count_as_valid) > 1 else ''}[{', '.join(count_as_valid)}], but is unset." |
| 81 | + ) |
| 82 | + else: |
| 83 | + attr_value = getattr(attr_value, "name", str(attr_value)) |
| 84 | + if attr_value.lower() not in [v.lower() for v in count_as_valid]: |
| 85 | + results.append( |
| 86 | + f"[{attr}] shoul be {'one of ' if len(count_as_valid) > 1 else ''}[{', '.join(count_as_valid)}], but found [{attr_value}]." |
| 87 | + ) |
| 88 | + |
| 89 | + if len(results) > 0: |
| 90 | + if self.args.get("resultMsgJSON"): |
| 91 | + msg["message"] = results |
| 92 | + return CheckResult(False, msg) |
| 93 | + else: |
| 94 | + return CheckResult(False, results) |
| 95 | + else: |
| 96 | + return CheckResult(True) |
0 commit comments