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

How to populate a schema with read-only data in Sanity.io

8 replies
Last updated: Oct 13, 2021
Also is it possible to populate an schema with read-only data?
Oct 12, 2021, 3:13 PM
For example:
import { translationKeys } from "../../../translations/translationKeys";

export default {
  name: "translations",
  title: "Translations",
  icon: MdLanguage,
  type: "document",
  // TODO: populate with data
  data: translationKeys,
  fields: [
    {
      name: "allStrings",
      title: "All strings",
      type: "array",
      of: [
        {
          type: "string",
          name: "key",
          title: "Key",
        },
        {
          type: "string",
          name: "value",
          title: "Value",
        },
      ],
    },
  ],
};
Oct 12, 2021, 3:14 PM
Hey User! Fields can take a readOnly property. What's your ultimate goal here? Are you looking to set them to read only after they are published the first time in the Studio? Or add the data via an API then set it to read only?
Oct 12, 2021, 6:42 PM
I have an file with all the translation keys used in the frontend and i want authors to be able to only translate te value's
Oct 13, 2021, 8:02 AM
I have an file with all the translation keys used in the frontend and i want authors to be able to only translate te value's
Oct 13, 2021, 8:02 AM
export const translationKeys = [
  "create-account",
  "create-your-account",
  "facebook-connect",
  "recover-password",
  "sign-in",
  "no-account-yet",
]
Oct 13, 2021, 8:03 AM
This is my approach now:
import { MdLanguage } from "react-icons/md";
import { translationKeys } from "../../../translations/translationKeys";

export default {
  name: "translations",
  title: "Translations",
  icon: MdLanguage,
  type: "document",
  fields: [
    {
      name: "key",
      title: "Key",
      type: "string",
      options: {
        list: translationKeys,
      },
    },
    {
      name: "value",
      title: "Value",
      type: "localeString",
    },
  ],
  preview: {
    select: {
      title: "key",
    },
  },
};
Oct 13, 2021, 8:03 AM
But I feel like there is a cleaner way, like all the keys as documents and the value's as input
Oct 13, 2021, 8:04 AM
Ah, I see now: you want to have list values added to the Studio from a separate location. The way you've gone about it is the correct way to go.
Oct 13, 2021, 7:13 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?