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

Improve usability of optional objects with required sub-fields #7497

Open
gpoole opened this issue Sep 12, 2024 · 0 comments
Open

Improve usability of optional objects with required sub-fields #7497

gpoole opened this issue Sep 12, 2024 · 0 comments

Comments

@gpoole
Copy link

gpoole commented Sep 12, 2024

Is your feature request related to a problem? Please describe.

The current way Sanity allows optional objects with required sub-fields is to not apply the reuired rule if the entire object is empty (as per #2630). This technically works but I think is often confusing to end users and has lots of ugly edge cases, especially for deeply nested objects.

Some pain points I've found:

  • Clearing out all the fields after you've entered something is not intuitive and hard to explain to end users
  • Using conditional hidden fields can make it confusing or even impossible for users to totally clear out an object
  • Some fields like radio buttons can't be cleared (Support clearing radio fields #4091)
  • Clearing select fields is un-intuitive
  • Setting an initialValue for a single field at any level of an optional object turns on all validation, which is confusing to users and makes optional objects appear required
  • Not possible to implement toggle switches to turn on and off sections of a schema, as validation is applied to hidden objects even if they are understood in to be ignored in the context of the schema

Describe the solution you'd like

I'm not especially attached to a particular solution. A general idea I thought of would be allowing objects to explicitly be marked as not validated. I'm imagining that these "not validated" objects could be treated exactly as though the whole object was empty. I feel that this approach allows maximum flexibility to how this is applied in different schemas.

For example:

defineType({
  name: "page",
  title: "Page",
  type: "document",
  fields: [
    defineField({
      name: "title",
      title: "Title",
      type: "string",
    }),
    defineField({
      name: "showHero",
      type: "boolean",
      title: "Show Hero?",
    }),
    defineField({
      name: "hero",
      title: "Hero",
      type: "heroModule",
      // In this schema, setting showHero to false indicates the object is not expected to be used and we can ignore validation
      noValidate: ({ document }) => document.showHero === false,
      // We can also now safely hide the object knowing it won't cause confusing validation errors
      hidden: ({document}) =>  document.showHero === false,
      options: {
        collapsible: true,
      },
    }),
  ],
});

Describe alternatives you've considered

For implementing a single optional object I've tried:

Additional context

I think this spiritually relates to #2630 but potentially goes further as a feature request, plus that issue is marked as closed.

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

No branches or pull requests

1 participant