You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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 validationnoValidate: ({ document })=>document.showHero===false,// We can also now safely hide the object knowing it won't cause confusing validation errorshidden: ({document})=>document.showHero===false,options: {collapsible: true,},}),],});
Describe alternatives you've considered
For implementing a single optional object I've tried:
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:
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 requiredDescribe 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:
Describe alternatives you've considered
For implementing a single optional object I've tried:
initialValue
anywhereAdditional context
I think this spiritually relates to #2630 but potentially goes further as a feature request, plus that issue is marked as closed.
The text was updated successfully, but these errors were encountered: