Skip to content

Conditional field validation based on other field values #1475

@Isaac-Harper

Description

@Isaac-Harper

Currently, Keystatic field validation (e.g., validation: { isRequired: true }) is static and cannot depend on the values of other fields in the same schema. This limitation makes it difficult to create logical validation rules for related fields.

Use Case

When working with image fields that include accessibility metadata, we want to require alt text only when an image has been uploaded. Currently, we must choose between:

  1. Making alt text always required (annoying when no image is uploaded)
  2. Making alt text optional (editors might forget to add it)

Example Schema

fields.object({
  image: fields.image({
    label: 'Featured Image',
    directory: 'public/images',
    publicPath: '/images/',
  }),
  alt: fields.text({
    label: 'Alt Text',
    // Would like: isRequired only when image is not null
    validation: { isRequired: false },
  }),
})

Proposed Solution

Add support for conditional validation rules:

fields.object({
  image: fields.image({ /* ... */ }),
  alt: fields.text({
    label: 'Alt Text',
    validation: { 
      isRequired: (values) => values.image !== null 
    },
  }),
})

This would enable validation rules that depend on sibling field values, improving editor UX and data integrity.

Additional Context

Similar functionality exists in form libraries like React Hook Form (conditional validation) and Zod (refinements). This pattern is common in CMS tools where field relationships require contextual validation.


Note: I'm interested in contributing to Keystatic and would be happy to implement this feature if there's interest from the maintainers. Please let me know if this aligns with the project roadmap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions