✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Error when using nested arrays in Sanity schema

6 replies
Last updated: Dec 26, 2022
I am getting the non-descript error: "Error: Invalid item type: "[object Object]". Default array input can only contain objects (for now)" with this schema for this schema
/**
 * This is the schema definition for the rich text fields used for
 * for this blog studio. When you import it in schemas.js it can be
 * reused in other parts of the studio with:
 *  {
 *    name: 'someName',
 *    title: 'Some title',
 *    type: 'blockContent'
 *  }
 */
export default {
  title: 'Block Content',
  name: 'blockContent',
  type: 'array',
  of: [
    {
      title: 'Block',
      type: 'block',
      // Styles let you set what your user can mark up blocks with. These
      // correspond with HTML tags, but you can set any title or value
      // you want and decide how you want to deal with it where you want to
      // use your content.
      styles: [
        {title: 'Normal', value: 'normal'},
        {title: 'H1', value: 'h1'},
        {title: 'H2', value: 'h2'},
        {title: 'H3', value: 'h3'},
        {title: 'H4', value: 'h4'},
        {title: 'Quote', value: 'blockquote'},
      ],
      lists: [{title: 'Bullet', value: 'bullet'}],
      // Marks let you mark up inline text in the block editor.
      marks: {
        // Decorators usually describe a single property – e.g. a typographic
        // preference or highlighting by editors.
        decorators: [
          {title: 'Strong', value: 'strong'},
          {title: 'Emphasis', value: 'em'},
        ],
        // Annotations can be any object structure – e.g. a link or a footnote.
        annotations: [
          {
            title: 'URL',
            name: 'link',
            type: 'object',
            fields: [
              {
                title: 'URL',
                name: 'href',
                type: 'url',
              },
            ],
          },
        ],
      },
    },
    // You can add additional types here. Note that you can't use
    // primitive types such as 'string' and 'number' in the same array
    // as a block type.
    {
      type: 'image',
      options: {hotspot: true},
    },
  ],
}
Any thoughts on what I did wrong?
Dec 26, 2022, 2:12 PM
And the above is included in a document type for dynamic pages:
export default {
  
// Setup a 'document' type to house the page builder field

name: "locationPage",
type: "document",
title: "Location Page",
fields: 
	[
		{
			name: 'name',
			type: 'string',
			title: 'Name'
		},
		{
			name: 'cityAndState',
			type: 'string',
			title: 'City and state'
		},
		{
			name: 'href',
			type: 'string',
			title: 'HREF'
		},
		{
		name: 'pageBuilder',
		type: 'array',
		title: 'Page builder',
		of: 
			[
				{ type: 'hero' }, // hero.js (same applies for the other types)
				{ type: 'blockContent' },
				{ type: 'blockWithIllustration' },
				{ type: 'blockWithIllustrationVariable' },
				{ type: 'callToAction' },
				{ type: 'gallery' },
				{ type: 'locationWithContactOverlay' },
				{ type: 'video' },
				// etc...
			]
		}
	]
}

Dec 26, 2022, 2:17 PM
Is this the nested array thing that Sanity does not do/handle?
Dec 26, 2022, 2:17 PM
It is. At least that is what it looks like to me. So I need to convert the blockContent schema to an object with a sub field of array.
Dec 26, 2022, 2:20 PM
/**
 * This is the schema definition for the rich text fields used for
 * for this blog studio. When you import it in schemas.js it can be
 * reused in other parts of the studio with:
 *  {
 *    name: 'someName',
 *    title: 'Some title',
 *    type: 'blockContent'
 *  }
 */
export default {
	name: 'blockContent',
	type: 'object',
  title: 'Block Content',
	fields: [
		{
			name: 'block',
			type: 'array',
			of: [
				{
					title: 'Block',
					type: 'block',
					// Styles let you set what your user can mark up blocks with. These
					// correspond with HTML tags, but you can set any title or value
					// you want and decide how you want to deal with it where you want to
					// use your content.
					styles: [
						{title: 'Normal', value: 'normal'},
						{title: 'H1', value: 'h1'},
						{title: 'H2', value: 'h2'},
						{title: 'H3', value: 'h3'},
						{title: 'H4', value: 'h4'},
						{title: 'Quote', value: 'blockquote'},
					],
					lists: [{title: 'Bullet', value: 'bullet'}],
					// Marks let you mark up inline text in the block editor.
					marks: {
						// Decorators usually describe a single property – e.g. a typographic
						// preference or highlighting by editors.
						decorators: [
							{title: 'Strong', value: 'strong'},
							{title: 'Emphasis', value: 'em'},
						],
						// Annotations can be any object structure – e.g. a link or a footnote.
						annotations: [
							{
								title: 'URL',
								name: 'link',
								type: 'object',
								fields: [
									{
										title: 'URL',
										name: 'href',
										type: 'url',
									},
								],
							},
						],
					},
				},
				// You can add additional types here. Note that you can't use
				// primitive types such as 'string' and 'number' in the same array
				// as a block type.
				{
					type: 'image',
					options: {hotspot: true},
				},
			],
		}
	],
}

Dec 26, 2022, 2:24 PM
So I had to wrap the array in an object basically.
Dec 26, 2022, 2:25 PM
Hello
user A
👋Yes so for now arrays cannot be pasted into arrays directly (
blockContent
is an array, so pasting it directly into an array will throw the error).
More here
Dec 26, 2022, 3:08 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?