|
1 | | -from typing import Any |
| 1 | +from typing import Any, Tuple, List |
2 | 2 | import yaml |
3 | 3 |
|
4 | 4 | from kubernetes.dynamic import DynamicClient |
@@ -105,6 +105,130 @@ def _extract_properties_and_required(schema: dict[Any, Any]) -> tuple[set[str], |
105 | 105 | return all_properties - excluded_fields, required_fields - excluded_fields |
106 | 106 |
|
107 | 107 |
|
| 108 | +def validate_filter_options_structure( |
| 109 | + response: dict[Any, Any], expected_properties: set[str] | None = None |
| 110 | +) -> Tuple[bool, List[str]]: |
| 111 | + """ |
| 112 | + Comprehensive validation of filter_options response structure. |
| 113 | +
|
| 114 | + Validates: |
| 115 | + - Top-level structure (filters object) |
| 116 | + - All property types and their required fields |
| 117 | + - Core properties presence (if specified) |
| 118 | + - String properties: type, values array, distinct values |
| 119 | + - Numeric properties: type, range object, min/max validity |
| 120 | +
|
| 121 | + Args: |
| 122 | + response: The API response to validate |
| 123 | + expected_properties: Optional set of core properties that must be present |
| 124 | +
|
| 125 | + Returns: |
| 126 | + Tuple of (is_valid, list_of_errors) |
| 127 | + """ |
| 128 | + errors = [] |
| 129 | + |
| 130 | + # Validate top-level structure |
| 131 | + if not isinstance(response, dict): |
| 132 | + errors.append("Response should be a dictionary") |
| 133 | + return False, errors |
| 134 | + |
| 135 | + if "filters" not in response: |
| 136 | + errors.append("Response should contain 'filters' object") |
| 137 | + return False, errors |
| 138 | + |
| 139 | + filters = response["filters"] |
| 140 | + if not isinstance(filters, dict): |
| 141 | + errors.append("Filters should be a dictionary") |
| 142 | + return False, errors |
| 143 | + |
| 144 | + if not filters: |
| 145 | + errors.append("Filters object should not be empty") |
| 146 | + return False, errors |
| 147 | + |
| 148 | + # Validate expected core properties if specified |
| 149 | + if expected_properties: |
| 150 | + for prop in expected_properties: |
| 151 | + if prop not in filters: |
| 152 | + errors.append(f"Core property '{prop}' should be present in filter options") |
| 153 | + |
| 154 | + # Validate each property structure |
| 155 | + for prop_name, prop_data in filters.items(): |
| 156 | + if not isinstance(prop_data, dict): |
| 157 | + errors.append(f"Property '{prop_name}' should be a dictionary") |
| 158 | + continue |
| 159 | + |
| 160 | + if "type" not in prop_data: |
| 161 | + errors.append(f"Property '{prop_name}' should have 'type' field") |
| 162 | + continue |
| 163 | + |
| 164 | + prop_type = prop_data["type"] |
| 165 | + if not isinstance(prop_type, str) or not prop_type.strip(): |
| 166 | + errors.append(f"Type for '{prop_name}' should be a non-empty string") |
| 167 | + continue |
| 168 | + |
| 169 | + # Validate string properties |
| 170 | + if prop_type == "string": |
| 171 | + if "values" not in prop_data: |
| 172 | + errors.append(f"String property '{prop_name}' should have 'values' array") |
| 173 | + continue |
| 174 | + |
| 175 | + values = prop_data["values"] |
| 176 | + if not isinstance(values, list): |
| 177 | + errors.append(f"Values for '{prop_name}' should be a list") |
| 178 | + continue |
| 179 | + |
| 180 | + if not values: |
| 181 | + errors.append(f"Values array for '{prop_name}' should not be empty") |
| 182 | + continue |
| 183 | + |
| 184 | + # Validate individual values |
| 185 | + for i, value in enumerate(values): |
| 186 | + if not isinstance(value, str): |
| 187 | + errors.append(f"Value at index {i} for '{prop_name}' should be string, got: {type(value)}") |
| 188 | + elif not value.strip(): |
| 189 | + errors.append(f"Value at index {i} for '{prop_name}' should not be empty or whitespace") |
| 190 | + |
| 191 | + # Check for distinct values (no duplicates) |
| 192 | + try: |
| 193 | + if len(values) != len(set(values)): |
| 194 | + errors.append(f"Values for '{prop_name}' should be distinct (found duplicates)") |
| 195 | + except TypeError: |
| 196 | + errors.append(f"Values for '{prop_name}' should be a list of strings, found unhashable type") |
| 197 | + |
| 198 | + # Validate numeric properties - checking multiple type names since we don't know what the API will return |
| 199 | + elif prop_type in ["number", "numeric", "float", "integer", "int"]: |
| 200 | + if "range" not in prop_data: |
| 201 | + errors.append(f"Numeric property '{prop_name}' should have 'range' object") |
| 202 | + continue |
| 203 | + |
| 204 | + range_obj = prop_data["range"] |
| 205 | + if not isinstance(range_obj, dict): |
| 206 | + errors.append(f"Range for '{prop_name}' should be a dictionary") |
| 207 | + continue |
| 208 | + |
| 209 | + # Check min/max presence |
| 210 | + if "min" not in range_obj: |
| 211 | + errors.append(f"Range for '{prop_name}' should have 'min' value") |
| 212 | + if "max" not in range_obj: |
| 213 | + errors.append(f"Range for '{prop_name}' should have 'max' value") |
| 214 | + |
| 215 | + if "min" in range_obj and "max" in range_obj: |
| 216 | + min_val = range_obj["min"] |
| 217 | + max_val = range_obj["max"] |
| 218 | + |
| 219 | + # Validate min/max are numeric |
| 220 | + if not isinstance(min_val, (int, float)): |
| 221 | + errors.append(f"Min value for '{prop_name}' should be numeric, got: {type(min_val)}") |
| 222 | + if not isinstance(max_val, (int, float)): |
| 223 | + errors.append(f"Max value for '{prop_name}' should be numeric, got: {type(max_val)}") |
| 224 | + |
| 225 | + # Validate logical relationship (min <= max) |
| 226 | + if isinstance(min_val, (int, float)) and isinstance(max_val, (int, float)) and min_val > max_val: |
| 227 | + errors.append(f"Min value ({min_val}) should be <= max value ({max_val}) for '{prop_name}'") |
| 228 | + |
| 229 | + return len(errors) == 0, errors |
| 230 | + |
| 231 | + |
108 | 232 | def validate_model_catalog_configmap_data(configmap: ConfigMap, num_catalogs: int) -> None: |
109 | 233 | """ |
110 | 234 | Validate the model catalog configmap data. |
|
0 commit comments