react-jsonschema-form
react-jsonschema-form copied to clipboard
trim empty objects to allow correct validation of optional objects with mandatory fields
Reasons for making this change
NOT FOR MERGE YET! Test failures remain - see discussion below.
This PR offers a potential solution for the issue of allowing optional objects with required fields to pass validation when none of their values are defined.
The approach is similar to that discussed in Issue 675 and proposed, then withdrawn, in PR 682.
Prior to validation, a clone of formData is generated where all objects with no defined values are removed (note: empty arrays are not removed).
This works nicely - but changes the semantics of the errors. Primarily 'is a required property' errors become 'should be object'. Currently I have not changed the tests so as to display that.
I have raised this PR to essentially ask whether this approach could be used and, if so, to seek advice on what to about the changed errors (I note the conversation here).
If this is related to existing tickets, include links to them as well: https://github.com/mozilla-services/react-jsonschema-form/issues/675 https://github.com/mozilla-services/react-jsonschema-form/pull/682
Checklist
- [ ] I'm updating documentation
- [ ] I've checked the rendering of the Markdown text I've added
- [X ] I'm adding or updating code
- [ ] I'm adding a new feature
- [ ] I've updated the playground with an example use of the feature
Hi @numical , thanks for the PR.
I see that the tests are failing due to the errors saying "should be object"; however, when I try to replicate this error on my local playground, I still get the expected error, "is a required property". Do you know why this is the case?
Also, your changes work, but if HTML5 validation is enabled, then I still get the "required" dialog. Do you think there might be a way to programmatically change this in this PR as well (to conditionally add the required
attribute)?
@numical just checking on this.
any progress here? It would be reallllly beneficial to not get errors on empty nested props with required values
The issue attempted to be fixed by this PR was actually fixed in #3436