Register now - Learn how Tecovas roped in success with Sanity and Shopify 🀠

Error encountered when reconfiguring schema for author document type in Sanity Studio.

11 replies
Last updated: Feb 17, 2022
Hey everyone:I've been working on an existing codebase and configured a schema for an author document type. Initially I set it up differently from the other document schemas and in order to better utilize their GROQ queries I am trying to reconfigure my current schema to better match.
Original setup:

import { BsPen } from "react-icons/bs";

import { figure } from "../shared/figure";
import { portableTextSimple } from "../shared/portableTextSimple";
import { portableText } from "../shared/portableText";
import { slug } from "../shared/slug";

import type { Rule } from "@sanity/types";

export default {
  title: "Author",
  name: "author",
  type: "document",
  fields: [
    {
      title: "Name",
      name: "name",
      type: "string",
      validation: (Rule: Rule) => Rule.required(),
    },
    slug({
      source: "name",
      basePath: "/author",
    }),
    {
      title: "Title",
      name: "title",
      type: "string",
      validation: (Rule: Rule) => Rule.required(),
    },
    figure({ title: "Image", name: "image" }),
    portableTextSimple({
      options: {
        validation: (Rule: Rule) => Rule.max(250),
      },
    }),
    {
      title: "Featured Posts",
      name: "featuredPosts",
      type: "array",
      of: [{ type: "reference", to: [{ type: "blogPost" }] }],
      validation: (Rule: Rule) => Rule.required().max(3),
    },
    portableText({
      title: "Featured Posts Content",
      name: "featuredPostsContent",
    }),
  ],
  preview: {
    select: {
      name: "name",
      slug: "slug.current",
    },
    prepare({ name, slug }) {
      return {
        title: `[Author] - ${name || "Untitled"}`,
        description: slug ? `URL: ${slug}` : "",
        media: BsPen,
      };
    },
  },
};
What I moved to:

//import Tabs from "sanity-plugin-tabs";

import { BsFilePost } from "react-icons/bs";

import { figure } from "../shared/figure";
import { portableTextSimple } from "../shared/portableTextSimple";
import { portableText } from "../shared/portableText";
import { slug } from "../shared/slug";
import { tabFix } from "../shared/tabFix";

import type { Rule } from "@sanity/types";

export default {
  title: "Author",
  name: "author",
  type: "document",
  //inputComponent: Tabs,
  fieldsets: [
    { name: "main", title: "Content" },
    { name: "seo", title: "SEO / Meta" },
  ],
  fields: [
    {
      type: "object",
      name: "content",
      fieldset: "main",
      fields: [
        {
          ...tabFix(),
        },
        {
          title: "Name",
          name: "name",
          type: "string",
          validation: (Rule: Rule) => Rule.required(),
        },
        slug({
          source: "name",
          basePath: "/author",
        }),
        {
          title: "Title",
          name: "title",
          type: "string",
          validation: (Rule: Rule) => Rule.required(),
        },
        figure({
          title: "Image",
          name: "image",
        }),
        portableTextSimple({
          options: {
            validation: (Rule: Rule) => Rule.max(250),
          },
        }),
        {
          title: "Featured Posts",
          name: "featuredPosts",
          type: "array",
          of: [{ type: "reference", weak: true, to: [{ type: "blogPost" }] }],
          validation: (Rule) => Rule.max(5),
        },
        {
          title: "Carousel Label - Only shown on Mobile",
          name: "carouselLabel",
          type: "string",
        },
        portableText({
          title: "Featured Posts Content",
          name: "featuredPostsContent",
        }),
      ],
    },
    {
      type: "meta",
      name: "seo",
      fieldset: "seo",
    },
  ],
  preview: {
    select: {
      title: "content.title",
      slug: "content.slug.current",
    },
    prepare({ title, slug }) {
      return {
        title: `[Author] - ${title || "Untitled"}`,
        description: slug ? `URL: ${slug}` : "",
        media: BsFilePost,
      };
    },
  },
};
However, when I try to view the content in Sanity studio I get a "Property value missing
_type
" error, attached below.Any assistance would be appreciated. Thank you!
Feb 16, 2022, 11:26 PM
User you'll need to spread the return value from the imported types, eg
slug
,
portableText
,
portableTextSimple
figure
, so they return objects. I would also not remove the Tabs component as those chunk out page data and seo data into separate objects consumed by the page/page modules and layout component respectively.
Feb 17, 2022, 12:15 AM
eg..
...slug({})
,
...portableText({})
etc..
Feb 17, 2022, 12:15 AM
user M
β€” this is my code he's working on and I can somewhat confidently say this probably isn't a bug with sanity based on what I can see πŸ‘
Feb 17, 2022, 12:18 AM
So I get an error when I try and use the Tabs component (first image), so that's why I was leaving them out for now.I spread out the return value but I'm still getting the same errors. It has also this other warning (second image) about having fields that weren't defined in the schema. A friend suggested this might be due to the data already having a structure and then the schema being changed. Do you have any ideas
user V
?
Feb 17, 2022, 12:26 AM
Yeah i'd remove the data and re-enter it. That error is specific to data not fitting the existing content model.
Feb 17, 2022, 12:27 AM
Can't speak to the Tabs issue.
Feb 17, 2022, 12:28 AM
Did you update the studio versions? Possible there's a conflict there.
Feb 17, 2022, 12:28 AM
It is implemented on other documents across the schema.
Feb 17, 2022, 12:29 AM
So unless you've commented them all out, it's probable the error is specific to the document you're editing.,
Feb 17, 2022, 12:29 AM
Okay, I got it working. Your suggestion to remove everything worked, and the Tabs no longer has that weird issue. Thanks User!
Feb 17, 2022, 12:36 AM
For sure. Good luck in there bud, hope it's not too chaotic πŸ‘
Feb 17, 2022, 12:37 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?