PostHog / drf-exceptions-hog

Standardized and easy-to-parse API error responses for Django REST Framework (DRF).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handle deeply nested validation errors

MichaelJRM opened this issue · comments

Say we have the following ValidationError with SUPPORT_MULTIPLE_EXCEPTIONS = True

{
        "parent_1": {
            "l1_attr_3": [
                [
                    ErrorDetail(
                        string="This field may not be blank.", code="blank"
                    ),
                ]
            ],
        },
        "parent_2": [
            [
                ErrorDetail(string="This field may not be blank.", code="blank"),
            ]
        ],
        "parent_3": [
            {
                "l1_attr_1": [
                    ErrorDetail(
                        string="This field may not be blank.", code="blank"
                    ),
                ],
                "l1_attr_2": [
                    ErrorDetail(
                        string="This field may not be blank.", code="blank"
                    ),
                ],
            },
            {
                "l1_attr_1": [
                    ErrorDetail(
                        string="This field may not be blank.", code="blank"
                    ),
                ],
            },
        ],
    }

The expected result is

{
    "type": "multiple",
    "code": "multiple",
    "detail": "Multiple exceptions occurred. Please check list for details.",
    "attr": None,
    "list": [
        {
            "type": "validation_error",
            "code": "blank",
            "detail": "This field may not be blank.",
            "attr": "parent_1__l1_attr_3__0",
        },
        {
            "type": "validation_error",
            "code": "blank",
            "detail": "This field may not be blank.",
            "attr": "parent_2__0",
        },
        {
            "type": "validation_error",
            "code": "blank",
            "detail": "This field may not be blank.",
            "attr": "parent_3__0__l1_attr_1",
        },
        {
            "type": "validation_error",
            "code": "blank",
            "detail": "This field may not be blank.",
            "attr": "parent_3__0__l1_attr_2",
        },
        {
            "type": "validation_error",
            "code": "blank",
            "detail": "This field may not be blank.",
            "attr": "parent_3__1__l1_attr_1",
        },
    ],
}

Instead we get this

{
    "type": "multiple",
    "code": "multiple",
    "detail": "Multiple exceptions ocurred. Please check list for details.",
    "attr": None,
    "list": [
        {
            "type": "validation_error",
            "code": "['blank']",
            "detail": "[ErrorDetail(string='This field may not be blank.', code='blank')]",
            "attr": "parent_1__l1_attr_3",
        },
        {
            "type": "validation_error",
            "code": "['blank']",
            "detail": "[ErrorDetail(string='This field may not be blank.', code='blank')]",
            "attr": "parent_2",
        },
        {
            "type": "validation_error",
            "code": "{'l1_attr_1': ['blank'], 'l1_attr_2': ['blank']}",
            "detail": "{'l1_attr_1': [ErrorDetail(string='This field may not be blank.', code='blank')], 'l1_attr_2': [ErrorDetail(string='This field may not be blank.', code='blank')]}",
            "attr": "parent_3",
        },
    ],
}