Data Validation

Headless supported

Data validation applies rules to cells and checks entered values against them. It catches input errors early and keeps the data in a sheet accurate and consistent.

Univer supports the following validation types:

  • Number
  • Integer
  • Text length
  • Date
  • Checkbox
  • Dropdown list (single/multiple selection)
  • Custom formula

Plugin mode

Installation

Shell
pnpm add @univerjs/data-validation @univerjs/sheets-data-validation @univerjs/sheets-data-validation-ui

Usage

TypeScript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverDataValidationPlugin } from '@univerjs/data-validation'import { UniverSheetsDataValidationPlugin } from '@univerjs/sheets-data-validation'import { UniverSheetsDataValidationUIPlugin } from '@univerjs/sheets-data-validation-ui'import SheetsDataValidationEnUS from '@univerjs/sheets-data-validation-ui/locale/en-US'import '@univerjs/sheets-data-validation-ui/lib/index.css'import '@univerjs/sheets-data-validation/facade'const univer = new Univer({  locale: LocaleType.EN_US,  locales: {    [LocaleType.EN_US]: mergeLocales(      SheetsDataValidationEnUS,     ),  },})univer.registerPlugin(UniverDataValidationPlugin)univer.registerPlugin(UniverSheetsDataValidationPlugin)univer.registerPlugin(UniverSheetsDataValidationUIPlugin)

Plugin configuration

UniverSheetsDataValidationUIPlugin

  • showEditOnDropdown
    booleanDefault: true
    Show the edit button in the list dropdown.
  • showSearchOnDropdown
    booleanDefault: true
    Show the search box in the list dropdown.
TypeScript
univer.registerPlugin(UniverSheetsDataValidationUIPlugin, {  /**   * Whether to display the edit button in the drop-down menu   * @default true   */  showEditOnDropdown: true,  /**   * Whether to display the search box in the drop-down menu   * @default true   */  showSearchOnDropdown: true,})

For all available options, see IUniverSheetsDataValidationUIConfig.

Mobile support

Configure the mobile editor first. In this page's plugin-mode setup, replace the desktop registrations below with the mobile exports from the same packages, and complete the replacement before opening a document. Keep the model plugins, styles, locales, and Facade imports, and do not register both variants.

PackageDesktop registrationMobile registration
@univerjs/sheets-data-validation-uiUniverSheetsDataValidationUIPluginUniverSheetsDataValidationMobileUIPlugin

Preset mode

Installation

Shell
pnpm add @univerjs/preset-sheets-data-validation

Usage

TypeScript
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsDataValidationPreset } from '@univerjs/preset-sheets-data-validation'import UniverPresetSheetsDataValidationEnUS from '@univerjs/preset-sheets-data-validation/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-data-validation/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.EN_US,  locales: {    [LocaleType.EN_US]: mergeLocales(      UniverPresetSheetsCoreEnUS,      UniverPresetSheetsDataValidationEnUS,     ),  },  presets: [    UniverSheetsCorePreset(),    UniverSheetsDataValidationPreset(),   ],})

Preset configuration

UniverSheetsDataValidationPreset

  • showEditOnDropdown
    booleanDefault: true
    Show the edit button in the list dropdown.
  • showSearchOnDropdown
    booleanDefault: true
    Show the search box in the list dropdown.
TypeScript
UniverSheetsDataValidationPreset({  /**   * Whether to display the edit button in the drop-down menu   * @default true   */  showEditOnDropdown: true,  /**   * Whether to display the search box in the drop-down menu   * @default true   */  showSearchOnDropdown: true,})

For all available options, see IUniverSheetsDataValidationPresetConfig.

Value matching

List validation ignores case, so green satisfies an option written Green, and a referenced list follows the same rule. Number and integer rules accept numeric text only. Booleans and blank text do not satisfy them.

Facade API

Complete Facade API type definitions are in FacadeAPI.

Importing

Plugin mode note

The Facade package is imported manually only in plugin mode. Preset mode already includes the matching package, so no additional import is required.

TypeScript
import '@univerjs/sheets-data-validation/facade'

Add a data validation rule

univerAPI.newDataValidation() creates a builder and returns an FDataValidationBuilder. Call its methods in a chain to construct the rule.

The main methods on FDataValidationBuilder are:

MethodDescription
buildBuild the rule.
requireCheckboxRequire a boolean value, which the cell displays as a checkbox.
requireDateAfterSet the type to DATE and require a date after the given date.
requireDateBeforeSet the type to DATE and require a date before the given date.
requireDateBetweenSet the type to DATE and require a date within the given range.
requireDateEqualToSet the type to DATE and require a date equal to the given date.
requireDateNotBetweenSet the type to DATE and require a date outside the given range.
requireDateOnOrAfterSet the type to DATE and require a date on or after the given date.
requireDateOnOrBeforeSet the type to DATE and require a date on or before the given date.
requireFormulaSatisfiedRequire the given formula to evaluate to true.
requireNumberBetweenRequire a number between the two given numbers, inclusive.
requireNumberEqualToRequire a number equal to the given value.
requireNumberGreaterThanRequire a number greater than the given value.
requireNumberGreaterThanOrEqualToRequire a number greater than or equal to the given value.
requireNumberLessThanRequire a number less than the given value.
requireNumberLessThanOrEqualToRequire a number less than or equal to the given value.
requireNumberNotBetweenRequire a number that is not between the two given numbers and not equal to either.
requireNumberNotEqualToRequire a number not equal to the given value.
requireValueInListRequire a value from a list of specific values. Supports single/multiple selection and showing or hiding the dropdown.
requireValueInRangeRequire a value within a specific range. Supports single/multiple selection and showing or hiding the dropdown.
setOptionsSet the options for the rule.
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Create a new data validation rule that requires a number between 1 and 10 for the range A1:B10const fRange = fWorksheet.getRange('A1:B10')const rule = univerAPI.newDataValidation()  .requireNumberBetween(1, 10)  .setOptions({    allowBlank: true,    showErrorMessage: true,    error: 'Please enter a number between 1 and 10',  })  .build()fRange.setDataValidation(rule)

Clear data validations of a range

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Clear data validation of range A1:B10const fRange = fWorksheet.getRange('A1:B10')fRange.setDataValidation(null)

Get data validations of range / worksheet

The returned rule object is an FDataValidation instance, which exposes the rule's criteria, options, and other settings.

The main methods on FDataValidation are:

MethodDescription
copyCreate a new FDataValidationBuilder instance from the current rule object.
getCriteriaTypeGet the rule's validation type.
getCriteriaValuesGet the values used to evaluate the criteria.
getHelpTextGet the help text, which provides guidance to users.
getRangesGet the ranges the rule applies to.
setCriteriaSet the rule's criteria.
setOptionsSet the rule's options.
setRangesSet the ranges the rule applies to.
deleteDelete the rule from the worksheet.
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Get all data validation rules in current sheet.const rulesOfSheet = fWorksheet.getDataValidations()// Get a data validation rule by rule ID.const ruleById = fWorksheet.getDataValidation(rulesOfSheet[0]?.rule?.uid)const fRange = fWorksheet.getRange('A1:B10')// Get first data validation rule in current range.const ruleOfRange = fRange.getDataValidation()console.log('ruleOfRange:', ruleOfRange, ruleOfRange?.getCriteriaValues())// Get all data validation rules in current range.const rulesOfRange = fRange.getDataValidations()

Update/Delete data validation

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Create a new data validation rule that requires a number equal to 20 for the range A1:B10const fRange = fWorksheet.getRange('A1:B10')const rule = univerAPI.newDataValidation()  .requireNumberEqualTo(20)  .build()fRange.setDataValidation(rule)const newRuleRange = fWorksheet.getRange('C1:D10')// Update the rule after 3 secondssetTimeout(() => {  fRange.getDataValidation()    // Change the range to C1:D10    .setRanges([newRuleRange])    // Change the criteria to require a number between 1 and 10    .setCriteria(      univerAPI.Enum.DataValidationType.DECIMAL,      [univerAPI.Enum.DataValidationOperator.BETWEEN, '1', '10'],    )    // Change option to stop input on validation errors and show error message    .setOptions({      allowBlank: true,      showErrorMessage: true,      error: 'Please enter a valid value',      errorStyle: univerAPI.Enum.DataValidationErrorStyle.STOP,    })}, 3000)// Delete the rule after 6 secondssetTimeout(() => {  newRuleRange.getDataValidation().delete()}, 6000)

Get validation errors

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Get all data validation errors for current workbook.const workbookErrors = await fWorkbook.getAllDataValidationErrorAsync()console.log('workbookErrors:', workbookErrors)// Get all data validation errors for current sheet.const worksheetErrors = await fWorksheet.getAllDataValidationErrorAsync()console.log('worksheetErrors:', worksheetErrors)// Get data validation errors for current range.const fRange = fWorksheet.getRange('A1:B10')const rangeErrors = await fRange.getDataValidationErrorAsync()console.log('rangeErrors:', rangeErrors)

Each error object contains the following fields:

  • sheetName: Name of the worksheet where the error occurred
  • row: Row index of the cell that triggered the error
  • column: Column index of the cell that triggered the error
  • ruleId: ID of the rule that triggered the error
  • inputValue: Input value that triggered the error
  • rule: Snapshot of the rule content

Get validator status

Validator status is a value of the DataValidationStatus enum:

  • valid: The cell value passes validation
  • invalid: The cell value fails validation
  • validating: The cell is being validated
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Get data validation validator status for current workbook.fWorkbook.getValidatorStatus().then((status) => {  console.log('===statusOfWorkbook', status)})// Get data validation validator status for current sheet.fWorksheet.getValidatorStatusAsync().then((status) => {})// Get data validation validator status for current range.const fRange = fWorksheet.getRange('A1:B10')fRange.getValidatorStatus().then((status) => {  console.log('===tatusOfRange', status)})

Event listeners

Complete event type definitions are in Events.

The data validation module emits events when rules are added, updated, or deleted, and when validation status changes. Use univerAPI.addEvent() to listen to any of these events.

Validation rule change events

univerAPI.Event.SheetDataValidationChanged fires after a validation rule changes.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.SheetDataValidationChanged, (params) => {  const { origin, worksheet, workbook, changeType, oldRule, rule } = params})// Remove the event listener, use `disposable.dispose()`

univerAPI.Event.BeforeSheetDataValidationAdd fires before a rule is added.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetDataValidationAdd, (params) => {  const { worksheet, workbook, rule } = params  // Cancel the data validation rule addition operation  params.cancel = true})// Remove the event listener, use `disposable.dispose()`

univerAPI.Event.BeforeSheetDataValidationDelete fires before a rule is deleted.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetDataValidationDelete, (params) => {  const { worksheet, workbook, ruleId, rule } = params  // Cancel the data validation rule deletion operation  params.cancel = true})// Remove the event listener, use `disposable.dispose()`

univerAPI.Event.BeforeSheetDataValidationDeleteAll fires before all rules are deleted.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetDataValidationDeleteAll, (params) => {  const { worksheet, workbook, rules } = params  // Cancel the data validation rule deletion operation  params.cancel = true})// Remove the event listener, use `disposable.dispose()`

Validation rule update events

univerAPI.Event.BeforeSheetDataValidationCriteriaUpdate fires before a rule's criteria change.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetDataValidationCriteriaUpdate, (params) => {  const { worksheet, workbook, ruleId, rule, newCriteria } = params  // Cancel the data validation rule criteria update operation  params.cancel = true})// Remove the event listener, use `disposable.dispose()`

univerAPI.Event.BeforeSheetDataValidationRangeUpdate fires before a rule's ranges change.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetDataValidationRangeUpdate, (params) => {  const { worksheet, workbook, ruleId, rule, newRanges } = params  // Cancel the data validation rule range update operation  params.cancel = true})// Remove the event listener, use `disposable.dispose()`

univerAPI.Event.BeforeSheetDataValidationOptionsUpdate fires before a rule's options change.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeSheetDataValidationOptionsUpdate, (params) => {  const { worksheet, workbook, ruleId, rule, newOptions } = params  // Cancel the data validation rule options update operation  params.cancel = true})// Remove the event listener, use `disposable.dispose()`

Validation status events

univerAPI.Event.SheetDataValidatorStatusChanged fires when a cell's validation status changes.

TypeScript
const disposable = univerAPI.addEvent(univerAPI.Event.SheetDataValidatorStatusChanged, (params) => {  const { worksheet, workbook, row, column, status, rule } = params})// Remove the event listener, use `disposable.dispose()`

Every event includes the following common parameters:

  • workbook: Current workbook instance
  • worksheet: Current worksheet instance

Some events also include:

  • rule: The related validation rule object
  • ruleId: Unique identifier of the rule
  • status: Validation status (only in SheetDataValidatorStatusChanged)
  • newCriteria: New validation criteria (only in BeforeSheetDataValidationCriteriaUpdate)
  • newRanges: New validation ranges (only in BeforeSheetDataValidationRangeUpdate)
  • newOptions: New validation options (only in BeforeSheetDataValidationOptionsUpdate)

For any event whose name begins with Before, set params.cancel = true in the callback to stop the operation.

Error style

errorStyle controls what happens when invalid data is entered. It accepts a DataValidationErrorStyle value:

  • STOP: Rejects the invalid entry
  • WARNING: Accepts the invalid entry and shows a warning
  • INFO: Accepts the invalid entry and shows an information message
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:B10')const rule = univerAPI.newDataValidation()  .requireNumberBetween(1, 10)  .setOptions({    allowBlank: true,    showErrorMessage: true,    error: 'Please enter a number between 1 and 10',    errorStyle: univerAPI.Enum.DataValidationErrorStyle.STOP,  })  .build()fRange.setDataValidation(rule)

Change list render mode

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const fRange = fWorksheet.getRange('A1:B10')fRange.getDataValidation()?.setOptions({  // support TEXT, ARROW, CUSTOM  // default is `CUSTOM`  renderMode: univerAPI.Enum.DataValidationRenderMode.TEXT,})

How is this guide?

© 2026 DreamNum Co., Ltd.