|
| 1 | +from enum import Enum |
| 2 | +from typing import Any, List, Optional, Tuple, TypeVar |
| 3 | + |
| 4 | +RT = TypeVar("RT") |
| 5 | + |
| 6 | + |
| 7 | +class RuleType(Enum): |
| 8 | + MUTUALLY_EXCLUSIVE = "mutually_exclusive" |
| 9 | + CONDITIONAL_EXCLUSIVE = "conditional_exclusive" |
| 10 | + MUTUALLY_INCLUSIVE = "mutually_inclusive" |
| 11 | + CONDITIONAL_INCLUSIVE = "conditional_inclusive" |
| 12 | + |
| 13 | + |
| 14 | +class PropertyRules: |
| 15 | + def __init__(self) -> None: |
| 16 | + self._rules: List[Tuple[RuleType, List[str], List[str]]] = [] |
| 17 | + |
| 18 | + def addMutuallyExclusive(self, *props: str) -> "PropertyRules": |
| 19 | + self._rules.append((RuleType.MUTUALLY_EXCLUSIVE, list(props), [])) |
| 20 | + return self |
| 21 | + |
| 22 | + def addConditionalExclusive(self, source_prop: str, target_props: List[str]) -> "PropertyRules": |
| 23 | + self._rules.append((RuleType.CONDITIONAL_EXCLUSIVE, [source_prop], target_props)) |
| 24 | + return self |
| 25 | + |
| 26 | + def addMutuallyInclusive(self, *props: str) -> "PropertyRules": |
| 27 | + self._rules.append((RuleType.MUTUALLY_INCLUSIVE, list(props), [])) |
| 28 | + return self |
| 29 | + |
| 30 | + def addConditionalInclusive(self, source_prop: str, target_props: List[str]) -> "PropertyRules": |
| 31 | + self._rules.append((RuleType.CONDITIONAL_INCLUSIVE, [source_prop], target_props)) |
| 32 | + return self |
| 33 | + |
| 34 | + def validate_all(self, validated_model: Optional[RT]) -> List[str]: |
| 35 | + if validated_model is None: |
| 36 | + return [] |
| 37 | + |
| 38 | + errors = [] |
| 39 | + for rule_type, source_props, target_props in self._rules: |
| 40 | + # Validate property names during validation |
| 41 | + all_props = source_props + target_props |
| 42 | + for prop in all_props: |
| 43 | + # Skip validation for value-based conditions (PropertyName=Value) |
| 44 | + if "=" in prop: |
| 45 | + prop_name = prop.split("=", 1)[0] |
| 46 | + if "." not in prop_name and not hasattr(validated_model, prop_name): |
| 47 | + errors.append(f"Property '{prop_name}' does not exist in the schema") |
| 48 | + continue |
| 49 | + elif "." not in prop and not hasattr(validated_model, prop): |
| 50 | + errors.append(f"Property '{prop}' does not exist in the schema") |
| 51 | + continue |
| 52 | + |
| 53 | + error = self._validate_rule(validated_model, rule_type, source_props, target_props) |
| 54 | + if error: |
| 55 | + errors.append(error) |
| 56 | + return errors |
| 57 | + |
| 58 | + def _validate_rule( |
| 59 | + self, validated_model: RT, rule_type: RuleType, source_props: List[str], target_props: List[str] |
| 60 | + ) -> Optional[str]: |
| 61 | + if rule_type == RuleType.MUTUALLY_EXCLUSIVE: |
| 62 | + return self._validate_mutually_exclusive(validated_model, source_props) |
| 63 | + if rule_type == RuleType.CONDITIONAL_EXCLUSIVE: |
| 64 | + return self._validate_conditional_exclusive(validated_model, source_props, target_props) |
| 65 | + if rule_type == RuleType.MUTUALLY_INCLUSIVE: |
| 66 | + return self._validate_mutually_inclusive(validated_model, source_props) |
| 67 | + if rule_type == RuleType.CONDITIONAL_INCLUSIVE: |
| 68 | + return self._validate_conditional_inclusive(validated_model, source_props, target_props) |
| 69 | + return None |
| 70 | + |
| 71 | + def _validate_mutually_exclusive(self, validated_model: RT, source_props: List[str]) -> Optional[str]: |
| 72 | + present = [prop for prop in source_props if self._get_property_value(validated_model, prop) is not None] |
| 73 | + if len(present) > 1: |
| 74 | + present_props = " and ".join(f"'{p}'" for p in present) |
| 75 | + return f"Cannot specify {present_props} together." |
| 76 | + return None |
| 77 | + |
| 78 | + def _validate_conditional_exclusive( |
| 79 | + self, validated_model: RT, source_props: List[str], target_props: List[str] |
| 80 | + ) -> Optional[str]: |
| 81 | + source_present = any(self._check_property_condition(validated_model, prop) for prop in source_props) |
| 82 | + if source_present: |
| 83 | + conflicting = [prop for prop in target_props if self._check_property_condition(validated_model, prop)] |
| 84 | + if conflicting: |
| 85 | + conflicting_props = ", ".join(f"'{p}'" for p in conflicting) |
| 86 | + return f"'{source_props[0]}' cannot be used with {conflicting_props}." |
| 87 | + return None |
| 88 | + |
| 89 | + def _validate_mutually_inclusive(self, validated_model: RT, source_props: List[str]) -> Optional[str]: |
| 90 | + present = [prop for prop in source_props if self._get_property_value(validated_model, prop) is not None] |
| 91 | + if 0 < len(present) < len(source_props): |
| 92 | + missing = [prop for prop in source_props if prop not in present] |
| 93 | + missing_props = ", ".join(f"'{p}'" for p in missing) |
| 94 | + present_props = ", ".join(f"'{p}'" for p in present) |
| 95 | + return f"When using {present_props}, you must also specify {missing_props}." |
| 96 | + return None |
| 97 | + |
| 98 | + def _validate_conditional_inclusive( |
| 99 | + self, validated_model: RT, source_props: List[str], target_props: List[str] |
| 100 | + ) -> Optional[str]: |
| 101 | + source_present = any(self._check_property_condition(validated_model, prop) for prop in source_props) |
| 102 | + if source_present: |
| 103 | + missing = [prop for prop in target_props if not self._check_property_condition(validated_model, prop)] |
| 104 | + if missing: |
| 105 | + missing_props = ", ".join(f"'{p}'" for p in missing) |
| 106 | + return f"'{source_props[0]}' requires all of: {missing_props}." |
| 107 | + return None |
| 108 | + |
| 109 | + def _get_property_value(self, validated_model: RT, prop: str) -> Any: |
| 110 | + if "." not in prop: |
| 111 | + return getattr(validated_model, prop, None) |
| 112 | + |
| 113 | + # Handle nested properties recursively |
| 114 | + try: |
| 115 | + first_part, rest = prop.split(".", 1) |
| 116 | + |
| 117 | + # Get the first level value |
| 118 | + if hasattr(validated_model, first_part): |
| 119 | + value = getattr(validated_model, first_part) |
| 120 | + elif isinstance(validated_model, dict) and first_part in validated_model: |
| 121 | + value = validated_model[first_part] |
| 122 | + else: |
| 123 | + return None |
| 124 | + |
| 125 | + # If value is None, return None |
| 126 | + if value is None: |
| 127 | + return None |
| 128 | + |
| 129 | + # Recursively get the rest of the property path |
| 130 | + return self._get_property_value(value, rest) |
| 131 | + except Exception: |
| 132 | + return None |
| 133 | + |
| 134 | + def _check_property_condition(self, validated_model: RT, prop: str) -> bool: |
| 135 | + """Check if property condition is met. Supports 'PropertyName=Value' syntax.""" |
| 136 | + if "=" in prop: |
| 137 | + prop_name, expected_value = prop.split("=", 1) |
| 138 | + actual_value = self._get_property_value(validated_model, prop_name) |
| 139 | + return actual_value is not None and str(actual_value) == expected_value |
| 140 | + return self._get_property_value(validated_model, prop) is not None |
0 commit comments