🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with custom input field schema in Sanity, resolved by importing custom type in schema.js

3 replies
Last updated: Dec 14, 2020
hey, I’m having problems when building custom input field schema. I’m getting
Unknown type: commercejsProducts. Valid types are: post, author, category, blockContent, array, block, boolean, datetime, date, document, email, file, geopoint, image, number, object, reference, slug, string, telephone, text and url
even though I have this custom
commercejsProducts
field defined in
schemas/commerceProducts.js

import CommercejsProducts from "../components/CommercejsProducts";

export default {
  name: "commercejsProducts",
  title: "Commercejs Products",
  type: "array",
  of: [
    {
      type: "object",
      fields: [
        {
          name: "price",
          type: "object",
          fields: [
            {
              name: "raw",
              type: "number",
            },
          ],
        },
        {
          name: "quantity",
          type: "number",
        },
        {
          name: "media",
          type: "object",
          fields: [
            {
              name: "type",
              type: "string",
            },
          ],
        },
        {
          name: "categories",
          type: "array",
          of: [
            {
              type: "object",
              fields: [
                {
                  name: "id",
                  type: "string",
                },
              ],
            },
          ],
        },
      ],
    },
  ],
  inputComponent: CommercejsProducts,
};
I’m a bit unclear, what exactly doesn’t work? Can Sanity not see
commercejsProducts
at all or is it the fact that I have more nested objects in it?
To clarify, I’m trying to use default
post.js
schema to enter my products there
export default {
...default post.js...
  fields: [
    {
      title: "Related products",
      name: "commercejsProducts",
      type: "commercejsProducts",
    },
  ],
  ...default post.js...
};
any thoughts? ideas?
Dec 13, 2020, 12:12 PM
Dec 13, 2020, 12:16 PM
ahhhhh, never mind, found what went wrong 🤦 For the recrod, when you define custom
type
you need to import it in
/schemas/schema.js

export default createSchema({
  types: schemaTypes.concat([
    [other default types are imported here]
    commercejsProducts, // THAT
  ]),
});
Dec 13, 2020, 12:21 PM
Great to hear that you found the solution 😄
Dec 14, 2020, 7:44 AM

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?