sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.28k stars 426 forks source link

Optional objects validation regression in v3 #4157

Open ifeltsweet opened 1 year ago

ifeltsweet commented 1 year ago

Sanity Studio v3 has a regression from v2 for https://github.com/sanity-io/sanity/issues/2630.

Specifically, optional empty objects once touched are not unset, even if all nested fields are empty, thus making all nested validations fail.

To Reproduce

Steps to reproduce the behavior:

  1. Create a simple schema with an optional object that has a required nested field.

    {
    title: 'Test',
    name: 'test',
    type: 'document',
    fields: [
    {
      type: 'object',
      name: 'Object',
      fields: [
        {
          type: 'text',
          name: 'nested',
          validation: (Rule) => Rule.required(),
        },
      ],
    },
    ],
    }
  2. Edit the nested field.

  3. Empty the nested field.

  4. Observe that now you are unable to publish the document because nested field validation fails.

Expected behavior

In step 4 the validation should not fail as the whole object should be unset once all nested fields are empty.

Which versions of Sanity are you using?

3.2.6

coreyward commented 1 year ago

I can confirm this is happening for me as well in v3.2.5. The _type property on the object remains even after the last field is cleared.

alexander-young commented 1 year ago

We are having the same issue. It really messes things up when you use Zod when we'd need to expect undefined, an empty object, or the filled object

gpoole commented 2 months ago

This issue appears to be fixed at least as of 3.49.0 which is the version I tested on. Setting and unsetting a field on an optional object seems to behave as expected. Is that right @ifeltsweet?