Help articles

Object type has an invalid field definition

Sanity Studio reports The "fields" property must be an array of fields, Found 2 fields with name "myField" in object, or Invalid field name "_myField". Field names cannot start with underscores "_" as it's reserved for system fields. depending on which part of the definition is wrong.

Documents or object types must define which fields they have. The fields property must be an array of field definitions, where both name and type are required, and each field having a unique name.

Additionally, field names must start with a letter and can only contain letters, numbers, and underscores. Field names can't start with an underscore, which is reserved for system fields. We recommend using camel case convention for field names.

Two other definition problems report the same error. An object with an empty fields array reports Object should have at least one field. A field definition that isn't an object reports Incorrect type for field definition - should be an object, followed by what it saw instead.

import {defineField, defineType} from 'sanity'

export const myObject = defineType({
  type: 'object',
  name: 'myObject',
  // fields must be defined, and it must be an array
  fields: [
    defineField({
      name: 'myField', // field name is required and must be unique
      type: 'string', // field type is required
    }),
    // ...
    defineField({
      // 💥 ERROR: a field named "myField" is already defined on this object
      name: 'myField',
      type: 'string',
    }),
  ],
})

Was this page helpful?