How to populate a Sanity schema with read-only data?

8 replies
Last updated: Oct 13, 2021
Also is it possible to populate an schema with read-only data?
AI Update

Yes, you can populate a schema with read-only data in Sanity! There are several approaches depending on your use case:

Using the readOnly Property

The simplest way is to use the readOnly property on your field definitions. This makes fields visible but not editable:

{
  name: 'generatedId',
  type: 'string',
  readOnly: true
}

Conditional Read-Only Fields

You can also make fields read-only based on conditions using callback functions, as explained in the Conditional Fields documentation:

{
  name: 'publishedDate',
  type: 'datetime',
  readOnly: ({document}) => document?.status === 'published'
}

The callback receives parameters like parent, document, value, and currentUser, allowing you to create dynamic read-only logic.

Populating Read-Only Data

To actually populate these read-only fields with data, you have a few options:

  1. Initial values - Set default values when documents are created:
{
  name: 'createdAt',
  type: 'datetime',
  readOnly: true,
  initialValue: () => new Date().toISOString()
}
  1. Document actions - Use custom document actions to programmatically set values before saving

  2. Mutations via API - Populate data through the Sanity API (mutations can write to read-only fields - the read-only restriction only applies to the Studio UI)

  3. Sanity Functions - Use Sanity Functions to automatically populate fields when documents are created or updated through event handlers

The key thing to remember is that readOnly only affects the Studio editing interface - it doesn't prevent programmatic updates via the API. This gives you flexibility to populate data through automated processes while preventing manual editing by content creators.

Show original thread
8 replies
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",
        },
      ],
    },
  ],
};
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?
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
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
export const translationKeys = [
  "create-account",
  "create-your-account",
  "facebook-connect",
  "recover-password",
  "sign-in",
  "no-account-yet",
]
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",
    },
  },
};
But I feel like there is a cleaner way, like all the keys as documents and the value's as input
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.

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?