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

anyOf form validation error messages confusing when anyOf items contain properties found in other items #4167

Open
4 tasks done
jroebu14 opened this issue Apr 19, 2024 · 4 comments

Comments

@jroebu14
Copy link
Contributor

jroebu14 commented Apr 19, 2024

Prerequisites

What theme are you using?

core

Version

5.18.2

Current Behavior

I am seeing confusing error messages when interacting with a form that uses anyOf where properties in an anyOf item are also found in other anyOf items.

In this schema for example:

{
  type: "object",
  properties: {
    favouriteThings: {
      anyOf: [
        {
          type: "object",
          properties: {
            favouriteAnimal: {
              type: "string",
            },
            favouriteColour: {
              type: "string",
            },
          },
          required: ["favouriteAnimal"],
        },
        {
          type: "object",
          properties: {
            favouriteAnimal: {
              type: "string",
            },
            favouriteColour: {
              type: "string",
            },
            favouritePerson: {
              type: "string",
            },
          },
          required: ["favouriteAnimal", "favouriteColour"],
        },
        {
          type: "object",
          properties: {
            favouritePerson: {
              type: "string",
            },
          },
          required: ["favouritePerson"],
        },
      ],
    },
  },
};

because favouriteAnimal is a property of all three anyOf items and required in 2 of them, when I chose Option 1 and submit the form without providing a value for favouriteAnimal then I will see two must have required property 'favouriteAnimal' error messages.

Screenshot 2024-04-19 at 07 54 56

You can also see that favouriteColor is showing a required error message even though it is optional for Option 1. This is because it is required in Option 2.

You can also see that favouritePerson is showing a required error message even though it is not a property of Option 1. This is because it is required in Option 3.

Expected Behavior

The form should only show error messages relevant to the current anyOf option selected.

Steps To Reproduce

  1. Go to https://zyh4f3.csb.app/
  2. Make sure "Option 1" is selected
  3. Dirty the form by entering a value in favouriteAnimal and then deleting it.
  4. Submit the form
  5. Observe multiple error messages for favouriteAnimal and an incorrect error message for favouriteColour

Environment

- OS: macOS 14.4.1
- Node: 20.11.0
- npm: 10.2.4

Anything else?

No response

@jroebu14 jroebu14 added bug needs triage Initial label given, to be assigned correct labels and assigned labels Apr 19, 2024
@nickgros
Copy link
Contributor

Seems similar to #1295 / #3795

@nickgros
Copy link
Contributor

@heath-freenome heath-freenome added ajv help wanted validation and removed needs triage Initial label given, to be assigned correct labels and assigned labels Apr 19, 2024
@heath-freenome
Copy link
Member

@jroebu14 This issue is also nearly identical to #3466, except we are dealing with a anyOf here rather than a oneOf. Essentially, AJV validator is validating all options. I've worked around this in my own implementations by providing a transformErrors function to filter the duplicates. Not ideal, but a quick and dirty solution. I'm not sure how rjsf can make this better given that AJV is the culprit for the duplicates.

It may be that we can add a configuration option for our validator-ajv8 to filter duplicates related to anyOf/oneOf automatically. Is this something you'd be willing to build?

@jroebu14
Copy link
Contributor Author

jroebu14 commented Apr 24, 2024

@heath-freenome ah ok this makes sense. Thanks for mentioning how to work around it.

It may be that we can add a configuration option for our validator-ajv8 to filter duplicates related to anyOf/oneOf automatically. Is this something you'd be willing to build?

If you think it's a worthwhile addition then I could certainly look into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants