Getting Uncaught Error cannot create property ‘validation’ on string ‘string’ when I have fields property as well as a validation property. If I get rid of either of those it works, but together it’s causing problems. Any idea what gives?
This error comes from combining type: 'string' with fields. A string can't have sub-fields. If you need a group of fields, the type has to be object (or document). Once it is, fields and validation work fine together.
Why it happens
The string type is a single short text value. Its properties are things like name, title, description, initialValue, placeholder and options. There's no fields property. The object type is the one that takes fields. An object needs at least one field, and each field needs its own name and type.
So a definition like the one below is invalid. Removing either fields or validation can hide the error, but the definition is still wrong:
// ❌ a string can't have fields
{
name: 'address',
type: 'string',
fields: [/* ... */],
validation: (rule) => rule.required(),
}The fix
Set the type to object. Validation can go on the object itself, on each field inside it, or both:
import {defineField, defineType} from 'sanity'
export const address = defineType({
name: 'address',
title: 'Address',
type: 'object',
// object-level validation, e.g. the whole object must be filled in
validation: (rule) => rule.required(),
fields: [
defineField({
name: 'street',
type: 'string',
// field-level validation uses the string rules
validation: (rule) => rule.required().max(100),
}),
defineField({
name: 'postalCode',
type: 'string',
validation: (rule) => rule.length(5).warning('Postal codes are usually 5 characters'),
}),
],
})Add the type to your schema types in defineConfig, then use it anywhere as a field type: defineField({name: 'shippingAddress', type: 'address'}).
Validation rules depend on the type:
- Object rules:
required(),skip(),custom(fn),error()/warning()/info()andvalueOfField(path). For checks that compare several sub-fields, usecustom()on the object. - String rules:
required(),min(),max(),length(),uppercase(),lowercase(),email(),regex(),custom()and the message helpers.warning()andinfo()don't block publishing.
If you only wanted a string
If you meant a plain string, delete fields and keep validation. For a fixed set of choices, use options.list (for example options: {list: ['sci-fi', 'western']}), not sub-fields.
Other things to check
The invalid field definition help page lists more causes of fields errors:
fieldsmust be an array of field definitions.- Every entry must be an object with
nameandtype. A bare string in the array is reported as "Incorrect type for field definition - should be an object". - Field names must be unique, must start with a letter, can only contain letters, numbers and underscores, and can't start with
_. - An empty
fieldsarray is reported as "Object should have at least one field".
Show original thread1 reply
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.