sanity icon indicating copy to clipboard operation
sanity copied to clipboard

Multiple errors when schema contains an array of multiple objects

Open mikepziegler opened this issue 1 year ago • 2 comments

Describe the bug

I have an array of multiple types of objects, that contain something else. Here is the schema:

export default {
  name: 'post',
  title: 'Post',
  type: 'document',
  fields: [
    {
      name: 'contents',
      title: 'Contents',
      type: 'array',
      of: [
        {
          name: 'galery',
          title: 'Galery',
          type: 'object',
          fields: [
            {
              name: 'galery',
              title: 'Galery',
              type: 'image',
              of: [
                {type: 'image'}
              ]
            },

          ]
        },
        {
          name: 'text',
          title: 'Text',
          type: 'object',
          fields: [
            {
              name: 'text',
              title: 'Text',
              type: 'array',
              of: [{type: 'block'}]
            }
          ]
        },
        {
          name: 'file',
          title: 'File',
          type: 'object',
          fields: [
            {
              name: 'file',
              title: 'File',
              type: 'file',
            }
          ]
        }
      ]
    },
    {
      name: 'publishedAt',
      title: 'Published at',
      type: 'datetime',
      readOnly: true,
      intialValue: (new Date()).toISOString()
    },
  ],
}

When clicking Text, there is no modal for adding text. it just When clicking image, you can select a image, but when you go out of the modal the element shows galery: {asset: {empty}} When clicking file, you can select a file, but it will output {empty}

To Reproduce

Steps to reproduce the behavior:

  1. Go to 'Post' under 'Content'
  2. Click on button to create a 'Post'
  3. Select under Contents one of the objects
  4. See error

Expected behavior

Test, array of blocks: A field should pop up image: an image should be displayed file: the name of the file should be displaying

Screenshots

Here you can see an image about

image

Which versions of Sanity are you using?

@sanity/cli 2.30.3 (up to date) @sanity/base 2.30.3 (up to date) @sanity/core 2.30.3 (up to date) @sanity/default-layout 2.30.3 (up to date) @sanity/default-login 2.30.3 (up to date) @sanity/desk-tool 2.30.3 (up to date) @sanity/eslint-config-studio 2.0.0 (up to date) @sanity/vision 2.30.3 (up to date)

What operating system are you using?

Windows 11

Which versions of Node.js / npm are you running?

v16.16.0

mikepziegler avatar Aug 04 '22 22:08 mikepziegler

Thanks for reporting. A couple of things:

  1. Not sure if you already explored this option, but you could consider putting all of this into a portable text field instead, for (probably) a better editing experience:
export default {
  name: 'post',
  title: 'Post',
  type: 'document',
  fields: [
    {
      name: 'contents',
      title: 'Contents',
      type: 'array',
      of: [
        {type: 'block'},
        {type: 'file'},
        {
          name: 'gallery',
          title: 'Gallery',
          type: 'object',
          fields: [
            {
              name: 'gallery',
              title: 'Gallery',
              type: 'array',
              of: [{type: 'image'}],
            },
          ],
        },
      ],
    },
    {
      name: 'publishedAt',
      title: 'Published at',
      type: 'datetime',
      readOnly: true,
      intialValue: () => new Date().toISOString(),
    },
  ],
}
  1. The gallery object has an incorrect schema declaration: type is image, but it has an of property as well?
{
  name: 'galery',
  title: 'Galery',
  type: 'object',
  fields: [
    {
      name: 'galery',
      title: 'Galery',
      type: 'image',
      of: [
        {type: 'image'}
      ]
    },
  ]
}

I assume you wanted an array of images:

{
  name: 'gallery',
  title: 'Gallery',
  type: 'object',
  fields: [
    {
      name: 'gallery',
      title: 'Gallery',
      type: 'array',
      of: [{type: 'image'}]
    },
  ]
}

You might also want to give it a preview config to it understand what to display in lists.

  1. The text field defined a type named text, which already exists in the core schema (I see we are missing validation here, thanks for reporting that):
{
  name: 'text',
  title: 'Text',
  type: 'object',
  fields: [
    {
      name: 'text',
      title: 'Text',
      type: 'array',
      of: [{type: 'block'}]
    }
  ]
}

You will need to name it something else:

{
  name: 'myText',
  title: 'Text',
  type: 'object',
  fields: [
    {
      name: 'text',
      title: 'Text',
      type: 'array',
      of: [{type: 'block'}]
    }
  ]
}
  1. The file field defines a type named file, which already exists:
{
  name: 'file',
  title: 'File',
  type: 'object',
  fields: [
    {
      name: 'file',
      title: 'File',
      type: 'file',
    }
  ]
}

I think you probably just want the following:

{
  name: 'file',
  title: 'File',
  type: 'file',
}

Does this solve your issues?

rexxars avatar Aug 08 '22 20:08 rexxars

Wait, I copy pasted your first schema, but the UI changed from a list to a rich text editor. How is that? Is it because of the first element of the fields?

mikepziegler avatar Aug 10 '22 17:08 mikepziegler

Wait, I copy pasted your first schema, but the UI changed from a list to a rich text editor. How is that? Is it because of the first element of the fields?

The studio detects that the block type is one of the possible types of the array, which turns it into a portable text (rich text) field

rexxars avatar Aug 11 '22 21:08 rexxars

Hoping that helped you out! Let us know if we can assist further!

rexxars avatar Aug 15 '22 20:08 rexxars