Skip to content

hidden function on field sometimes called without a currentUser (creates inconsistent state) #12201

@thobas-dnvgl

Description

@thobas-dnvgl

I think the new context.hidden feature (very welcomed addition!) #12050 revealed a race condition, ping @RitaDias.

Describe the bug

I am reading currentUser from the context passed to the hidden function on field. It's called several times, the last time with a currentUser that is null.

To Reproduce

Play with this schema and look at the logs + UI behavior:

import {defineField, defineType} from 'sanity'

export default defineType({
  name: 'demoToBeDeleted',
  type: 'document',
  fields: [
    defineField({
      name: 'name',
      type: 'string',
      hidden: (context) => {
        console.group('hidden function called')
        console.log('context', context)
        console.log('.currentUser', context.currentUser)
        // @ts-expect-error .role exists but is not typed in Sanity's CurrentUser type
        const isAdmin = context.currentUser?.role === 'administrator'
        console.log('isAdmin', isAdmin)
        console.groupEnd()
        return !isAdmin
      },
      validation: (rule, context) =>
        rule.custom((value) => {
          console.group('custom validation function called')
          console.log('Name field value', value)
          console.log('context.hidden', context.hidden)
          console.groupEnd()
          return true
        }),
    }),
  ],
})

The logs:

Image

The studio UI (field is visible):

Image

Expected behavior

The field is visible as I am logged as an administrator, as expected.

But notice that the 3rd time the hidden function is called, currentUser is null so hidden returns true. context.hidden in the validation function is indeed true. But the field is visible in the studio UI as if the UI was not acknowledging the last returned value of hidden()! One one hand it's great, as an admin I should see the field, but on the other hand the validation function believes it's hidden.

Which versions of Sanity are you using?

@sanity/cli (global)          5.10.0 (up to date)
@sanity/asset-utils            2.3.0 (up to date)
@sanity/cli                   5.10.0 (up to date)
@sanity/color                  3.0.6 (up to date)
@sanity/color-input            6.0.4 (up to date)
@sanity/eslint-config-studio   6.0.0 (up to date)
@sanity/icons                  3.7.4 (up to date)
@sanity/image-url              2.0.3 (up to date)
@sanity/table                  2.0.1 (up to date)
@sanity/ui                    3.1.11 (up to date)
@sanity/util                  5.10.0 (up to date)
@sanity/uuid                   3.0.2 (up to date)
@sanity/vision                5.10.0 (up to date)
sanity                        5.10.0 (up to date)

What operating system are you using?
Windows 11.

Which versions of Node.js / npm are you running?

❯ npm -v && node -v
11.10.0
v24.13.1

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions