Sanity Translation Error: "Invalid property value" - String vs Object Type Mismatch
https://www.youtube.com/watch?v=Rk70FSgTJeQ Minute: 1: 07
On my sanity, it gives an error:
Invalid property value
The property value is stored as a value type that does not match the expected type.
Developer info
The value of this property must be of type object according to the schema.
Mismatching value types typically occur when the schema has recently been changed.
The current value (string){
name: 'blog',
type: 'document',
title: 'Blog',
fields: [
{
name: 'title',
title: 'Title',
type: 'object',
fields: [
{
title: 'English',
name: 'en',
type: 'string'
},
{
title: 'French',
name: 'fr',
type: 'string'
}
]
},
]
}The error doesn't mean the schema is wrong. The document was saved while title was a plain string, and Sanity doesn't change existing content when you edit a schema. The Studio sees a string where the schema now expects an object ({en, fr}), so it flags the value. To fix it, clear or convert the old value. Your schema can stay as it is.
Fix the existing value
One or a few documents in development: clear the old value from the field in the Studio (look for a reset option on the field's warning), or delete the test documents and create new ones. New documents are saved with the object structure.
Real content you want to keep: convert the strings with a migration. Changing a schema never changes or deletes existing content, so you have to do it explicitly. Create a migration with npx sanity@latest migrations create, then:
// migrations/localize-blog-title/index.ts
import {defineMigration, at, set} from 'sanity/migrate'
export default defineMigration({
title: 'Convert blog title string to {en, fr} object',
documentTypes: ['blog'],
migrate: {
document(doc) {
if (typeof doc.title === 'string') {
return [at('title', set({en: doc.title}))]
}
return []
},
},
})# Dry run (default): prints the patches without applying them
npx sanity@latest migrations run localize-blog-title
# Apply the changes
npx sanity@latest migrations run localize-blog-title --no-dry-runBack up the dataset first with sanity datasets export.
Render it in the frontend
title is now an object, so you can't render it directly. Read title.en or title.fr in your component, or select the language in GROQ:
*[_type == "blog"]{ "title": title[$lang] }Choose a localization approach
Today's Studio uses defineConfig and defineType from the sanity package. The v2 part:@sanity/base/schema-creator setup is gone. The localization docs describe two approaches, and you can use both in one project:
Field-level (one document with every language in it): an object with one key per language works, but each new language adds unique attributes to the dataset (
title.en,title.fr, …). The docs recommend the internationalized-array plugin (sanity-plugin-internationalized-array) instead. It stores items withlanguageandvaluefields, and itsfieldTypesoption supports Portable Text, references and custom objects, so it can also handle a localizedcontentfield or per-language images. Query it like this:*[_type == "blog"]{ "title": coalesce(title[language == $language][0].value, "Missing translation") }Document-level (a separate document for each language, each published on its own schedule; the docs recommend this for translating Portable Text): add a
languagefield, or use the @sanity/document-internationalization plugin. It sets the language field and creates atranslation.metadatadocument that links the translations together. Check the plugin's README for its configuration options.
The Course Platform Demo is a Studio and Next.js example with an internationalized schema and localized queries. For machine translation, see Translating content with AI Assist.
Show original thread9 replies
Was this answer helpful?
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.