Issue with importing and defining schemas in Sanity.io

10 replies
Last updated: May 22, 2021
Hey guysI have two schemas marketing and adBanner in simple terms marketing is an array of adBanners. I have defined the schemas as bellow.


_export_ default {

title: "Marketing",

name: "marketing",

type: "array",

of: [{ type: "adBanner" }],

};


_export_ default {

title: "Ad Banner",

name: "adBanner",

type: "document",

fields: [

{

title: "Promo Banner type",

name: "promoType",

type: "string",

},

{

title: "Promo Heading",

name: "promoHeading",

type: "string",

},

{

title: "Promo Text",

name: "promoText",

type: "array",

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

},

{

title: "Affiliate marketing link",

name: "promoLink",

type: "url",

},

{

title: "Promo product image",

name: "promoImage",

type: "image",

},

],

};

but when i compile and run i can only see adBanner but not marketing section. When i debug it says

Unknown type: adBanner. Valid types are: marketing, array, block, boolean, datetime, date, document, email, file, geopoint, image, number, object, reference, slug, string, telephone, text and url

Any explanation/fixes for this.
Cheers
AI Update

I can see the issue! The problem is that you're trying to use adBanner as a document type, but then referencing it directly in an array's of property. When you use type: "document", you're creating a top-level document type that appears in your Studio's main navigation. However, you can't directly embed document types in arrays - you need to either use references or change adBanner to an object type.

Here are two ways to fix this:

If you want adBanner items to be embedded directly within the marketing array, change it from a document to an object:

export default {
  title: "Ad Banner",
  name: "adBanner",
  type: "object", // Changed from "document" to "object"
  fields: [
    {
      title: "Promo Banner type",
      name: "promoType",
      type: "string",
    },
    {
      title: "Promo Heading",
      name: "promoHeading",
      type: "string",
    },
    {
      title: "Promo Text",
      name: "promoText",
      type: "array",
      of: [{ type: "block" }],
    },
    {
      title: "Affiliate marketing link",
      name: "promoLink",
      type: "url",
    },
    {
      title: "Promo product image",
      name: "promoImage",
      type: "image",
    },
  ],
};

Then you'll need a parent document to hold the marketing array:

export default {
  title: "Marketing Page",
  name: "marketingPage",
  type: "document",
  fields: [
    {
      title: "Marketing",
      name: "marketing",
      type: "array",
      of: [{ type: "adBanner" }],
    },
  ],
};

Option 2: Use references to keep adBanner as a document

If you want adBanner to remain a standalone document type (appearing in your Studio navigation), use references:

export default {
  title: "Marketing Page",
  name: "marketingPage",
  type: "document",
  fields: [
    {
      title: "Marketing",
      name: "marketing",
      type: "array",
      of: [
        {
          type: "reference",
          to: [{ type: "adBanner" }],
        },
      ],
    },
  ],
};

The key difference: Object types are embedded content that only exist within their parent document. Document types are standalone entries that can be referenced from multiple places. For your use case, Option 1 (object type) is probably what you want unless you need to reuse the same ad banners across multiple marketing campaigns.

Make sure both schema files are properly exported in your schema configuration file (usually schema/index.ts or similar) so Sanity knows about both types!

Hey
user V
- are both imported to schema?
Yes Luke both imported
Weird - that message usually indicates it hasn’t been imported
Want to share your schema file?
import createSchema from "part:@sanity/base/schema-creator";


// Then import schema types from any plugins that might expose them

import schemaTypes from "all:part:@sanity/base/schema-type";

import story from "./stroy";

import author from "./author";

import chapter from "./chapter";

import artworks from "./artworks";

import adBanner from "./adBanner";

import marketing from "./marketing";


// Then we give our schema to the builder and provide the result to Sanity

_export_ default createSchema({

// We name our schema

name: "default",

// Then proceed to concatenate our document type

// to the ones provided by any plugins that are installed

types: schemaTypes.concat([

/* Your types here! */

story,

author,

chapter,

artworks,

adBanner,

marketing,

]),

});
for some reason simple schema which includes type array does not work its weird
_export_ default {

title: "Marketing",

name: "marketing",

type: "array",

of: [{ type: "string" }],

};
got this straight from the docs still not working
hm yeh, I think that might need to be an object with an array inside it
i think i tried that too but not sure about the syntax entirely can you provide me an example using the above schema if no trouble?
Thanks for the help figured it out its due to nested arrays so wrapping it out in a object works. I didnt realise both schema types were arrays.cheers
ah great news Shawin, sorry I wasn't around to debug the schema, but glad you figured it out

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?