Skip to content

groq-invalid-type-filter false positive for discriminated union object types in arrays #27

@nkgentile

Description

@nkgentile

Bug Description

The sanity/groq-invalid-type-filter rule incorrectly flags _type filters on array items (discriminated union members) as invalid document types.

Example

Given a schema with object types used as discriminated unions in arrays:

// Object types (NOT document types)
defineType({
  name: 'textBlock',
  type: 'object',
  fields: [{ name: 'text', type: 'text' }],
})

defineType({
  name: 'imageBlock',
  type: 'object',
  fields: [{ name: 'asset', type: 'image' }],
})

// Document type with polymorphic array
defineType({
  name: 'post',
  type: 'document',
  fields: [
    {
      name: 'content',
      type: 'array',
      of: [{ type: 'textBlock' }, { type: 'imageBlock' }],
    },
  ],
})

This valid GROQ query:

*[_type == "post"] {
  "images": content[_type == "imageBlock"]
}

Produces the error:

Document type "imageBlock" does not exist in schema

Expected Behavior

The rule should distinguish between:

  1. Document type filters (top-level *[_type == "..."]) - should be validated against schema document types
  2. Array item filters (array[_type == "..."]) - should recognize object types used as discriminated union members

Current Behavior

All _type == "..." filters are validated against document types only, causing false positives for legitimate discriminated union patterns.

Environment

  • @sanity/eslint-plugin: 0.3.0
  • @sanity/groq-lint: 0.0.3

Workaround

Currently suppressing with eslint-disable comments, but this masks real issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions