|  | 
| 9 | 9 | import requests  # type: ignore | 
| 10 | 10 | import yaml  # type: ignore | 
| 11 | 11 | from jsonschema import Draft202012Validator | 
| 12 |  | -from referencing import Registry, Resource | 
| 13 |  | -from referencing.jsonschema import DRAFT202012 | 
| 14 |  | -from referencing.typing import URI | 
|  | 12 | +from referencing import Registry, Resource  # type: ignore | 
|  | 13 | +from referencing.jsonschema import DRAFT202012  # type: ignore | 
|  | 14 | +from referencing.typing import URI  # type: ignore | 
| 15 | 15 | 
 | 
| 16 | 16 | NEW_VERSIONS = [ | 
| 17 | 17 |     "1.0.0-beta.2", | 
| @@ -292,3 +292,37 @@ def load_schema_config(config_path: str) -> dict: | 
| 292 | 292 |     if "schemas" in data: | 
| 293 | 293 |         return data["schemas"] | 
| 294 | 294 |     return data | 
|  | 295 | + | 
|  | 296 | + | 
|  | 297 | +def extract_relevant_oneof_error(error, instance=None): | 
|  | 298 | +    """Extract the most relevant error from a 'oneOf' validation error. | 
|  | 299 | +
 | 
|  | 300 | +    Given a jsonschema.ValidationError for a 'oneOf' failure, this function returns | 
|  | 301 | +    the most relevant sub-error, with preference given to errors matching the instance's 'type'. | 
|  | 302 | +    If no matching type is found, it falls back to returning the first sub-error. | 
|  | 303 | +
 | 
|  | 304 | +    Args: | 
|  | 305 | +        error (jsonschema.ValidationError): The validation error from a 'oneOf' validation. | 
|  | 306 | +        instance (dict, optional): The instance being validated. If provided and contains a 'type' | 
|  | 307 | +            field, the function will try to find a matching schema for that type. Defaults to None. | 
|  | 308 | +
 | 
|  | 309 | +    Returns: | 
|  | 310 | +        jsonschema.ValidationError: The most relevant sub-error from the 'oneOf' validation. | 
|  | 311 | +            If the error is not a 'oneOf' validation error or has no context, returns the | 
|  | 312 | +            original error unchanged. | 
|  | 313 | +    """ | 
|  | 314 | +    if error.validator == "oneOf" and hasattr(error, "context") and error.context: | 
|  | 315 | +        if instance and "type" in instance: | 
|  | 316 | +            for suberror in error.context: | 
|  | 317 | +                # Try to match the instance 'type' to the schema's 'type' | 
|  | 318 | +                props = suberror.schema.get("properties", {}) | 
|  | 319 | +                type_schema = props.get("type", {}) | 
|  | 320 | +                if ( | 
|  | 321 | +                    isinstance(type_schema, dict) | 
|  | 322 | +                    and "const" in type_schema | 
|  | 323 | +                    and instance["type"] == type_schema["const"] | 
|  | 324 | +                ): | 
|  | 325 | +                    return suberror | 
|  | 326 | +        # Fallback to the first suberror | 
|  | 327 | +        return error.context[0] | 
|  | 328 | +    return error | 
0 commit comments