Handle deeply nested validation errors

Open
#30 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

  • #31 by @MichaelJRM — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
django, python
Domain
api, backend

Research direction

The issue does not name a file or test. Start by tracing the validation-error formatting path that handles SUPPORT_MULTIPLE_EXCEPTIONS and nested dictionaries and lists. Reproduce the supplied structure, then verify that the output flattens nested errors into the stated attributes, codes, details, and list entries.

Written by the indexing model from the issue text.

Description

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",
        },
    ],
}
Dominant language
Python
Stars
68
Forks
14
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PostHog/drf-exceptions-hog

All issues in PostHog/drf-exceptions-hog

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.