sanity
sanity copied to clipboard
Multiple errors when schema contains an array of multiple objects
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:
- Go to 'Post' under 'Content'
- Click on button to create a 'Post'
- Select under Contents one of the objects
- 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
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
Thanks for reporting. A couple of things:
- 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(),
},
],
}
- 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.
- 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'}]
}
]
}
- 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?
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?
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
Hoping that helped you out! Let us know if we can assist further!