Skip to content

DRF Standardized Errors

DRFStandardizedErrorsMixin

Add support for drf_standardized_errors package.

Source code in saritasa_drf_tools/testing/drf_standardized_errors.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class DRFStandardizedErrorsMixin:
    """Add support for drf_standardized_errors package."""

    def extract_errors_from_response(
        self,
        response: rest_framework.response.Response,
        field: str,
    ) -> list[str]:
        """Extract errors from response."""
        assert response.data  # noqa: S101
        assert "errors" in response.data, response.data  # noqa: S101
        field_errors = [
            error["detail"]
            for error in response.data["errors"]
            if error["attr"] == field
        ]
        assert field_errors, response.data  # noqa: S101
        return field_errors

extract_errors_from_response(response, field)

Extract errors from response.

Source code in saritasa_drf_tools/testing/drf_standardized_errors.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def extract_errors_from_response(
    self,
    response: rest_framework.response.Response,
    field: str,
) -> list[str]:
    """Extract errors from response."""
    assert response.data  # noqa: S101
    assert "errors" in response.data, response.data  # noqa: S101
    field_errors = [
        error["detail"]
        for error in response.data["errors"]
        if error["attr"] == field
    ]
    assert field_errors, response.data  # noqa: S101
    return field_errors