How to add rich text block inside array of structured content in Sanity?

7 replies
Last updated: Oct 2, 2023
Hey there! How to add a rich text block inside an array of structured content?I have this object for the content:


{
      title: "Content",
      name: "content",
      type: "array",
      group: "countryInfo",
      of: [
        // { type: "block" },
        { type: "textBlock" },
        { type: "image" },
        { type: "speedTest" },
        { type: "thePricingTable" },
      ],
    },
the `textBlock`object is this:

export default {
  name: "textBlock",
  title: "Text Block",
  type: "array",

  of: [{ type: "block" }],
};
but i get this error:
AI Update

I can see the issue! You're trying to nest an array inside another array, which causes this error in Sanity. The problem is that your textBlock is defined as type: "array", and you're trying to use it inside another array field.

The solution is to define textBlock as an object type, not an array type:

export default {
  name: "textBlock",
  title: "Text Block",
  type: "object", // Changed from "array" to "object"
  fields: [
    {
      name: "content",
      title: "Content",
      type: "array",
      of: [{ type: "block" }],
    }
  ]
};

Now your main content array will work properly:

{
  title: "Content",
  name: "content",
  type: "array",
  group: "countryInfo",
  of: [
    { type: "textBlock" }, // This now references an object, not an array
    { type: "image" },
    { type: "speedTest" },
    { type: "thePricingTable" },
  ],
}

Why this works: In Sanity, when you have an array field, the items in the of array can be either:

  • Simple types like block, image, string
  • Object types (which can themselves contain arrays)

But you cannot directly nest arrays within arrays. By making textBlock an object type that contains an array field, you follow Sanity's schema structure correctly.

Alternative approach: If you just want rich text directly in your content array without the extra wrapper, you can simply use:

{
  title: "Content",
  name: "content",
  type: "array",
  of: [
    { type: "block" }, // Rich text directly
    { type: "image" },
    { type: "speedTest" },
    { type: "thePricingTable" },
  ],
}

This gives you Portable Text rich text editing capabilities mixed with your custom block types, which is the standard pattern for Block Content in Sanity.

Show original thread
7 replies
hey, did you add the textBlock to the /schema/ index.js(ts) file ?
you have to register all schemas there for sanity to find them
Hey, yes i did. I think the issue is that
textBlock
is an array and
content
is also an array. So it means array within an array, so maybe its not possible. I thought maybe there is another way how to approach this.
the error here indicates that what ever you are trying to map is not an array. Make sure you are mapping the right thing
ok, it just worked after 2 days of trying lol
textBlock
should be an object instead of an array
export default {
  name: "textBlock",
  title: "Text Block",
  type: "object",
  fields: [
    {
      name: "textBody",
      title: "TextBody",
      type: "array",

      of: [{ type: "block" }],
    },
  ],
};

cool, good you worked it out..
yeah 2D arrays are not supported, the workaround is using and object/array combo..

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?