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

How to populate a list of values in a Sanity schema using a separate file

5 replies
Last updated: Sep 6, 2021
Hey there! I’m stuck with this, maybe somebody is able to give me a hint?

export default {
  name: "platformFeature",
  title: "Plattform Features",
  description: "Ein Feature der Plattform",
  type: "object",
  fields: [
    {
      name: "icon",
      title: "Icon",
      description: "Ein passendes Icon",
      type: "image",
    },
    {
      name: "component",
      title: "Plattform Komponente",
      description: "Zu welcher Komponente gehört dieses Feature?",
      type: "string",
      options: {
        list: [
          { title: "Meeting", value: "meeting" },
          { title: "Player", value: "player" },
          { title: "Server", value: "server" },
          { title: "Collector", value: "collector" },
          { title: "Pro", value: "pro" },
        ],
      },
    },
    {
      name: "heading",
      title: "Feature Heading",
      description: "Ein aussagekräftiger Titel des Features",
      type: "string",
    },
    {
      name: "description",
      title: "Beschreibung",
      description: "Beschreibe das Feature in wenigen Worten",
      type: "text",
    },
  ],
};
This is an
platformFeature
object I’m working on. A
platformComponent
can have
n
features, a
platformFeature
can have one
platformComponent
. The user can select a
component
from a list of options (see
name: "component"
).
If I’m
hardcoding the values in the schema above it works fine. But I’d like to outsource those options to a separate schema file because there will be other contents that will have a
component
value (e.g. product pages, help/blog articles, subject in a contact form etc.). DRY, you know…
The
component
value will not be documents but predefined values the content editor can choose from. How can I include this list of values? I can’t wrap my head around it at the moment.
Sep 3, 2021, 1:30 PM
Not sure, but here is one idea. Create a document then hide that from the UI using deskStructure.
Sep 3, 2021, 1:48 PM
Yep, if it’s not possible otherwise, this would be a solution. I’m going to use deskStructure anyway.
Sep 3, 2021, 3:28 PM
Hey Ralph! To make sure I'm understanding correctly, you want to populate your list of strings using a separate file that contains those values? If that's the case, it's totally possible to do so!
import components from './components'

export default {
  name: "platformFeature",
  title: "Plattform Features",
  description: "Ein Feature der Plattform",
  type: "object",
  fields: [
    {
      name: "icon",
      title: "Icon",
      description: "Ein passendes Icon",
      type: "image",
    },
    {
      name: "component",
      title: "Plattform Komponente",
      description: "Zu welcher Komponente gehört dieses Feature?",
      type: "string",
      options: {
        list: components,
      },
    },
    {
      name: "heading",
      title: "Feature Heading",
      description: "Ein aussagekräftiger Titel des Features",
      type: "string",
    },
    {
      name: "description",
      title: "Beschreibung",
      description: "Beschreibe das Feature in wenigen Worten",
      type: "text",
    },
  ],
};

components.js

export default [
    { title: "Meeting", value: "meeting" },
    { title: "Player", value: "player" },
    { title: "Server", value: "server" },
    { title: "Collector", value: "collector" },
    { title: "Pro", value: "pro" },
]
Sep 3, 2021, 5:38 PM
Ah yes! That was the trick. Obviously I was overthinking it. 😅
Sep 6, 2021, 12:30 PM
Ah yes! That was the trick. Obviously I was overthinking it. 😅
Sep 6, 2021, 12:30 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?