Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nested field validation bug #4268

Merged
merged 4 commits into from
Oct 7, 2022
Merged

fix nested field validation bug #4268

merged 4 commits into from
Oct 7, 2022

Conversation

braxex
Copy link
Contributor

@braxex braxex commented Oct 7, 2022

Description

Well this was a fun one. 2 weeks ago we inadvertently introduced a bug to the codebase that broke nested field validation. Nested field validation depends on some code in schemas.ts that checks if the parent option is checked, and if so, creates validation schema for the nested field. This code depended on the field value matching an expected passed value that we set in mcpar.json. When we changed ChoiceList components (radio & checkbox) to store data in a {name: "", value: ""} object, it broke that method because the value of a given field was no longer a string, but an object. The fix for this was technically pretty easy -- just change that method to look deeper. But because it was now looking in an array of objects, a bit of tweaking was needed.

As I was fixing it, I realized that we actually don't even need to rely on the visibleOptionValue (e.g. "Other, specify") but could instead rely on something else: the parentOptionId (e.g. "option5"). A couple tweaks to a couple other methods later... where we used to have to specify the option value separately in mcpar.json like this...

{
    "name": "option7",
    "label": "Other, specify",
    "children": [
        {
            "id": "bssEntity_entityRole-otherText",
            "type": "textarea",
            "validation": {
                "type": "text",
                "nested": true,
                "parentFieldName": "bssEntity_entityRole",
                "visibleOptionValue": "Other, specify"
            }
        }
    ]
}

... we can now omit the visibleOptionValue (as shown below), which is now parentOptionId and is programmatically set behind the scenes, making mcpar.json management just a bit easier.

{
    "name": "option7",
    "label": "Other, specify",
    "children": [
        {
            "id": "bssEntity_entityRole-otherText",
            "type": "textarea",
            "validation": {
                "type": "text",
                "nested": true,
                "parentFieldName": "bssEntity_entityRole"
            }
        }
    ]
}

Resolves: https://qmacbis.atlassian.net/browse/MDCT-2019
Bug introduced in: #3959

How to test

  1. Create a new report.
  2. Navigate to pages with radios and/or checkboxes and find nested fields. Ensure that they have functioning field-level validation.

Changed Dependencies

n/a

Code author checklist

  • I have performed a self-review of my code
  • I have added thorough tests
  • I have added analytics, if necessary
  • I have updated the documentation, if necessary

Reviewer checklist (two different people)

  • I have done the deep review and verified the items in the above checklist are g2g
  • I have done the lgtm review

Copy link
Contributor

@gmrabian gmrabian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works great. Nice fix!

@braxex
Copy link
Contributor Author

braxex commented Oct 7, 2022

Note: I'm not concerned about the level of test coverage.

@braxex braxex merged commit f5ddeaa into main Oct 7, 2022
@braxex braxex deleted the nested-bug branch October 7, 2022 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants