|
1 | 1 | import json |
2 | | -from typing import Any |
| 2 | +from typing import Any, Tuple, List |
3 | 3 | import time |
4 | 4 | import yaml |
5 | 5 |
|
@@ -213,6 +213,130 @@ def _extract_properties_and_required(schema: dict[Any, Any]) -> tuple[set[str], |
213 | 213 | return all_properties - excluded_fields, required_fields - excluded_fields |
214 | 214 |
|
215 | 215 |
|
| 216 | +def validate_filter_options_structure( |
| 217 | + response: dict[Any, Any], expected_properties: set[str] | None = None |
| 218 | +) -> Tuple[bool, List[str]]: |
| 219 | + """ |
| 220 | + Comprehensive validation of filter_options response structure. |
| 221 | +
|
| 222 | + Validates: |
| 223 | + - Top-level structure (filters object) |
| 224 | + - All property types and their required fields |
| 225 | + - Core properties presence (if specified) |
| 226 | + - String properties: type, values array, distinct values |
| 227 | + - Numeric properties: type, range object, min/max validity |
| 228 | +
|
| 229 | + Args: |
| 230 | + response: The API response to validate |
| 231 | + expected_properties: Optional set of core properties that must be present |
| 232 | +
|
| 233 | + Returns: |
| 234 | + Tuple of (is_valid, list_of_errors) |
| 235 | + """ |
| 236 | + errors = [] |
| 237 | + |
| 238 | + # Validate top-level structure |
| 239 | + if not isinstance(response, dict): |
| 240 | + errors.append("Response should be a dictionary") |
| 241 | + return False, errors |
| 242 | + |
| 243 | + if "filters" not in response: |
| 244 | + errors.append("Response should contain 'filters' object") |
| 245 | + return False, errors |
| 246 | + |
| 247 | + filters = response["filters"] |
| 248 | + if not isinstance(filters, dict): |
| 249 | + errors.append("Filters should be a dictionary") |
| 250 | + return False, errors |
| 251 | + |
| 252 | + if not filters: |
| 253 | + errors.append("Filters object should not be empty") |
| 254 | + return False, errors |
| 255 | + |
| 256 | + # Validate expected core properties if specified |
| 257 | + if expected_properties: |
| 258 | + for prop in expected_properties: |
| 259 | + if prop not in filters: |
| 260 | + errors.append(f"Core property '{prop}' should be present in filter options") |
| 261 | + |
| 262 | + # Validate each property structure |
| 263 | + for prop_name, prop_data in filters.items(): |
| 264 | + if not isinstance(prop_data, dict): |
| 265 | + errors.append(f"Property '{prop_name}' should be a dictionary") |
| 266 | + continue |
| 267 | + |
| 268 | + if "type" not in prop_data: |
| 269 | + errors.append(f"Property '{prop_name}' should have 'type' field") |
| 270 | + continue |
| 271 | + |
| 272 | + prop_type = prop_data["type"] |
| 273 | + if not isinstance(prop_type, str) or not prop_type.strip(): |
| 274 | + errors.append(f"Type for '{prop_name}' should be a non-empty string") |
| 275 | + continue |
| 276 | + |
| 277 | + # Validate string properties |
| 278 | + if prop_type == "string": |
| 279 | + if "values" not in prop_data: |
| 280 | + errors.append(f"String property '{prop_name}' should have 'values' array") |
| 281 | + continue |
| 282 | + |
| 283 | + values = prop_data["values"] |
| 284 | + if not isinstance(values, list): |
| 285 | + errors.append(f"Values for '{prop_name}' should be a list") |
| 286 | + continue |
| 287 | + |
| 288 | + if not values: |
| 289 | + errors.append(f"Values array for '{prop_name}' should not be empty") |
| 290 | + continue |
| 291 | + |
| 292 | + # Validate individual values |
| 293 | + for i, value in enumerate(values): |
| 294 | + if not isinstance(value, str): |
| 295 | + errors.append(f"Value at index {i} for '{prop_name}' should be string, got: {type(value)}") |
| 296 | + elif not value.strip(): |
| 297 | + errors.append(f"Value at index {i} for '{prop_name}' should not be empty or whitespace") |
| 298 | + |
| 299 | + # Check for distinct values (no duplicates) |
| 300 | + try: |
| 301 | + if len(values) != len(set(values)): |
| 302 | + errors.append(f"Values for '{prop_name}' should be distinct (found duplicates)") |
| 303 | + except TypeError: |
| 304 | + errors.append(f"Values for '{prop_name}' should be a list of strings, found unhashable type") |
| 305 | + |
| 306 | + # Validate numeric properties - checking multiple type names since we don't know what the API will return |
| 307 | + elif prop_type in ["number", "numeric", "float", "integer", "int"]: |
| 308 | + if "range" not in prop_data: |
| 309 | + errors.append(f"Numeric property '{prop_name}' should have 'range' object") |
| 310 | + continue |
| 311 | + |
| 312 | + range_obj = prop_data["range"] |
| 313 | + if not isinstance(range_obj, dict): |
| 314 | + errors.append(f"Range for '{prop_name}' should be a dictionary") |
| 315 | + continue |
| 316 | + |
| 317 | + # Check min/max presence |
| 318 | + if "min" not in range_obj: |
| 319 | + errors.append(f"Range for '{prop_name}' should have 'min' value") |
| 320 | + if "max" not in range_obj: |
| 321 | + errors.append(f"Range for '{prop_name}' should have 'max' value") |
| 322 | + |
| 323 | + if "min" in range_obj and "max" in range_obj: |
| 324 | + min_val = range_obj["min"] |
| 325 | + max_val = range_obj["max"] |
| 326 | + |
| 327 | + # Validate min/max are numeric |
| 328 | + if not isinstance(min_val, (int, float)): |
| 329 | + errors.append(f"Min value for '{prop_name}' should be numeric, got: {type(min_val)}") |
| 330 | + if not isinstance(max_val, (int, float)): |
| 331 | + errors.append(f"Max value for '{prop_name}' should be numeric, got: {type(max_val)}") |
| 332 | + |
| 333 | + # Validate logical relationship (min <= max) |
| 334 | + if isinstance(min_val, (int, float)) and isinstance(max_val, (int, float)) and min_val > max_val: |
| 335 | + errors.append(f"Min value ({min_val}) should be <= max value ({max_val}) for '{prop_name}'") |
| 336 | + |
| 337 | + return len(errors) == 0, errors |
| 338 | + |
| 339 | + |
216 | 340 | def validate_model_catalog_configmap_data(configmap: ConfigMap, num_catalogs: int) -> None: |
217 | 341 | """ |
218 | 342 | Validate the model catalog configmap data. |
|
0 commit comments