Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule.fields not usable for nested data structures #8290

Open
blended-bram opened this issue Jan 16, 2025 · 2 comments
Open

Rule.fields not usable for nested data structures #8290

blended-bram opened this issue Jan 16, 2025 · 2 comments

Comments

@blended-bram
Copy link

Describe the bug

The Rule.fields(...) validation API does not work inside of Rule.all for a field validation rule:

To Reproduce

defineType({
  type: 'object',
  name: 'my.entry',
  fields: [
    defineField({ name: 'name', type: 'string' }),
    defineField({ name: 'label', type: 'string' }),
  ],
})

defineType({
  type: 'document',
  name: 'my.document',
  fields: [
    defineField({
      type: 'my.entry',
      validation: Rule => Rule.all([
        Rule.required(),
        Rule.fields({ 
          name: Rule.required(),
          label: Rule.required(),
        }),
      ]),
    }),
  ],
})

Expected behavior

The validation rules of Rule.fields applies to the fields of the nested data structure.
Instead, the document is publish-able with empty name and label fields.

Screenshots

N/A

Which versions of Sanity are you using?

(I reviewed the code related to validation, it seems that the latest code does not support the nested field validations)

   ╭──────────────────────────────────────────╮
   │                                          │
   │    Update available 3.62.0 → 3.70.0      │
   │   Run npm update @sanity/cli to update   │
   │                                          │
   ╰──────────────────────────────────────────╯

@sanity/cli (global)                   3.62.0 (latest: 3.70.0)
@sanity/document-internationalization   3.1.0 (latest: 3.3.1)
@sanity/eslint-config-studio            2.0.4 (latest: 5.0.1)
@sanity/icons                          2.11.8 (latest: 3.5.7)
@sanity/language-filter                 3.2.2 (latest: 4.0.3)
@sanity/table                           1.1.2 (latest: 1.1.3)
@sanity/uuid                            3.0.2 (up to date)
@sanity/vision                         3.62.0 (latest: 3.70.0)
sanity                                 3.62.0 (latest: 3.70.0)

What operating system are you using?

N/A

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

  • npm: 10.9.0
  • node: v20.14.0

Additional context

N/A

Security issue?

N/A

@blended-bram
Copy link
Author

I have since found out that you can achieve the intended result when setting a field's validation key to an array of validation rules:

validation: [
  Rule => Rule.required(),
  Rule => Rule.fields({
    name: Rule => Rule.required(),
    label: Rule => Rule.required(),
  }),
],

@cngonzalez
Copy link
Member

Hi @blended-bram ! I'm glad you've found a workaround. I agree that the Rule.all is not working as I'd expect, and I've filed it as a bug. Thank you for reporting it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants