def validate_mime_type(
    *, file_path: PathLike[str] | str | None = None, mime_type: str | None = None, check_file_exists: bool = True
) -> str:
    """Validate and detect the MIME type for a given file.

    Args:
        file_path: The path to the file.
        mime_type: Optional explicit MIME type. If provided, this will be validated.
            If not provided, the function will attempt to detect the MIME type.
        check_file_exists: Whether to check if the file exists. Default is True.
            Set to False in tests where you want to validate a mime type without an actual file.

    Raises:
        ValidationError: If the MIME type is not supported or cannot be determined.

    Returns:
        The validated MIME type.
    """
    if file_path and check_file_exists:
        path = Path(file_path)
        if not path.exists():
            raise ValidationError("The file does not exist", context={"file_path": str(path)})

    if not mime_type:
        if not file_path:
            raise ValidationError(
                "Could not determine mime type.",
            )
        path = Path(file_path)

        ext = path.suffix.lower()
        mime_type = EXT_TO_MIME_TYPE.get(ext) or guess_type(path.name)[0]

        if not mime_type:  # pragma: no cover
            raise ValidationError(
                "Could not determine the mime type of the file. Please specify the mime_type parameter explicitly.",
                context={"input_file": str(path), "extension": ext},
            )

    if mime_type in SUPPORTED_MIME_TYPES:
        return mime_type

    for supported_mime_type in SUPPORTED_MIME_TYPES:
        if mime_type.startswith(supported_mime_type):
            return supported_mime_type

    raise ValidationError(
        f"Unsupported mime type: {mime_type}",
        context={"mime_type": mime_type, "supported_mimetypes": ",".join(sorted(SUPPORTED_MIME_TYPES))},
    )
Validate and detect the MIME type for a given file.

    Args:
        file_path: The path to the file.
        mime_type: Optional explicit MIME type. If provided, this will be validated.
            If not provided, the function will attempt to detect the MIME type.
        check_file_exists: Whether to check if the file exists. Default is True.
            Set to False in tests where you want to validate a mime type without an actual file.

    Raises:
        ValidationError: If the MIME type is not supported or cannot be determined.

    Returns:
        The validated MIME type.
    
kreuzberg/_mime_types.py