react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Members of `oneOf` are validated when invisible

Open RPiAwesomeness opened this issue 2 years ago • 4 comments

Prerequisites

  • [X] I have searched the existing issues
  • [X] I understand that providing a SSCCE example is tremendously useful to the maintainers.
  • [X] I have read the documentation
  • [X] Ideally, I'm providing a shared playground link demonstrating the issue.

What theme are you using?

core

Version

5.1.0

Current Behavior

If there are duplicate members in those oneOf entries, then error messages from all of the options are rendered under the visible field. Similarly, if the schema for an invisible oneOf sub-form is validated then the form submits successfully, even if the currently visible sub-form is not valid.

Expected Behavior

Submitting an invalid form will only validate/show the errors for the specific oneOf sub-form/sub-schema being actively rendered and will only show the oneOf error message once.

Steps To Reproduce

  1. Open the linked playground example
  2. Submit the form without any input
  3. See there are two instances of the error message "must match exactly one schema in oneOf"
  4. See there are three error messages under the Test Nested field, two of which represent invisible fields
  5. Enter a value for Test Nested and click submit
  6. See that the form submits even though the

Environment

- OS: Ubuntu 22.04 (WSL2 on Windows 11)
- Node: 19.4.0
- npm: 9.3.0

Anything else?

This is very similar to #2584 but I believe it to be unique enough to warrant a new issue and at the very least brings attention to the fact it is still an issue under a newer version. It is also related to #3368.

RPiAwesomeness avatar Mar 02 '23 19:03 RPiAwesomeness

This issue came up due to usage of refs for the oneOf schemas, resulting in duplicate field IDs

RPiAwesomeness avatar Mar 02 '23 19:03 RPiAwesomeness

@RPiAwesomeness using your example, clicking the Raw Validate button shows all of the errors as provided by the underlying AJV 8 validator. Unfortunately, our code isn't smart enough to know how to deal with all of these messages given the initial data. I'm not sure that the issue is actually fixable in a general manner to suit all use cases. You do have the ability to provide a transformErrors() function on the form in which, knowing your schema, you could decide to remove some or all of the errors until the user provides enough information in formData to be able to display them properly. Sorry we can't be more helpful here.

heath-freenome avatar Mar 03 '23 16:03 heath-freenome

Since the form is being rendered based on which of the oneOf options is currently selected there's clearly some form of parsing/state management that RJSF has to tell which option is active. Is that accessible? If so, I could use the transformErrors and go off that schemaPath number (#/oneOf/1/...) to strip out any unrelated errors.

I've worked around this but in a very hacky manner and wanted to make sure it was documented/reported. In theory, this should be a fairly straightforward fix of something that goes against how I would expect the library to work in a valid situation and I would be open to submitting a PR for this if that would help.

RPiAwesomeness avatar Mar 06 '23 14:03 RPiAwesomeness

This issue has been automatically marked as possibly close because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you.

stale[bot] avatar Jun 05 '24 00:06 stale[bot]

This issue was closed because of lack of recent activity. Reopen if you still need assistance.

stale[bot] avatar Jul 05 '24 02:07 stale[bot]