Skip to content

Latest commit

 

History

History
91 lines (75 loc) · 2.15 KB

define-array-member-helper.md

File metadata and controls

91 lines (75 loc) · 2.15 KB

Enforce "defineArrayMember" helper functions for Sanity schema arrays (sanity-studio/define-array-member-helper)

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

Sanity does not require the use of "defineArrayMember()", even when a field is enclosed in "defineField()". Utilizing this helper function validates the schema while developing, and helps prevent runtime errors.

Rule Details

This rule requires Sanity array members to be wrapped with the "defineArrayMember" helper function.

Examples of incorrect code for this rule:

/*eslint sanity-studio/define-array-member-helper: "error"*/

export const product = {
  name: "product",
  type: "document",
  title: "Product",
  fields: [
    {
      name: "productName",
      type: "string",
      title: "Product name",
    },
    {
      name: "tags",
      type: "array",
      title: "Tags for item",
      of: [
        {
          type: "object",
          name: "tag",
          fields: [
            { type: "string", name: "label" },
            { type: "string", name: "value" },
          ],
        },
      ],
    },
  ],
};

Examples of correct code for this rule:

/*eslint sanity-studio/define-array-member-helper: "error"*/

export const product = {
  name: "product",
  type: "document",
  title: "Product",
  fields: [
    {
      name: "productName",
      type: "string",
      title: "Product name",
    },
    {
      name: "tags",
      type: "array",
      title: "Tags for item",
      of: [
        defineArrayMember({
          type: "object",
          name: "tag",
          fields: [
            { type: "string", name: "label" },
            { type: "string", name: "value" },
          ],
        }),
      ],
    },
  ],
};

When Not To Use It

If you deem using the "defineArrayMember" function unnessary. This rule can be used independently from "define-type-helper" and "define-field-helper".

Further Reading