gridcn2.0.0
Validation examples

Async validation example

Check a value against a server with a Standard Schema that returns a promise, and see a single edit and a bulk paste wait for the answer.

The SKU column is the fourth column. It runs a fake uniqueness check with about 800 ms of latency. SKU-001 and SKU-002 already exist. Type a SKU and press Enter to see the pending state. Paste several SKUs to see the bulk path.

ColumnRuleKind
Age18 or older. Also fires a toast.Blocking
EmailMust contain an @.Blocking
SKUSKU-001 and SKU-002 are taken. The check takes about 800 ms and the input is read-only meanwhile.Blocking
PriceMust be a number. A decimal commits rounded: 42.9 becomes 43.Blocking
NotesAt most 12 characters.Soft
Zod ScoreInteger from 0 to 100.Blocking
Joi CodeUppercase letters, digits, or dashes.Blocking
Valibot TagAt least 3 characters.Blocking
  • Every message: hover the cell to read it, also while the editor is open (the browser shows it as a native tooltip).
  • Blocking rule: Enter, Tab, or a click on another cell keeps the editor open, and the value does not commit. The message also shows in a line next to the cell. Escape cancels the edit and restores the old value.
  • Soft rule (Notes): the value commits and the editor closes. The cell stays flagged. If you edit it again, the line shows next to the cell. A valid value clears the flag.
  • Paste: copy a column of ages (some under 18) and paste it onto Age. Valid cells commit and invalid cells drop. Notes drops its invalid cells on paste too.
Name
Age
Email
SKU
Price
Notes
Zod Score
Joi Code
Valibot Tag
Ada Lovelace
28
ada@example.com
SKU-101
42
First algorithm
87
ADA-1843
math
Grace Hopper
34
grace@example.com
SKU-102
18
Compiled it
94
GRACE-1906
compilers
Alan Turing
41
alan@example.com
SKU-103
99
Broke Enigma
99
TURING-1912
computability
Katherine Johnson
25
katherine@example.com
SKU-104
7
Trajectory math
91
KATH-1918
orbital
Margaret Hamilton
22
margaret@example.com
SKU-105
56
Apollo nav
96
MARG-1936
software
Radia Perlman
30
radia@example.com
SKU-106
31
Spanning trees
88
RADIA-1951
networking
Tim Berners-Lee
19
tim@example.com
SKU-107
64
Invented the web
98
TIM-1955
web
Barbara Liskov
45
barbara@example.com
SKU-108
12
Subtyping
93
BARB-1939
types
pnpm dlx shadcn@latest add @gridcn/data-grid-validation-demo

Make a schema async

Any Standard Schema whose validate returns a promise is async. The function form of validate stays synchronous. This column rejects a SKU that the server already holds:

import { z } from "zod";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";

async function isSkuFree(sku: string) {
  const response = await fetch(`/api/sku/${encodeURIComponent(sku)}`);
  return response.ok;
}

type Item = { id: string; sku: string };

const columns = defineColumns<Item>()([
  {
    id: "sku",
    header: "SKU",
    accessorKey: "sku",
    validate: z.string().refine(isSkuFree, "Already in use"),
  },
]);

export function Items({ items }: { items: Item[] }) {
  return (
    <DataGrid defaultData={items} columns={columns} getRowId={(i) => i.id} />
  );
}

A single edit

  • The editor stays open while the promise is pending. The built-in editors set their input to readOnly, never disabled, so Escape still cancels.
  • If the schema resolves with issues, the message shows like a sync rejection and editing continues.
  • If the schema resolves with a value, the cell commits that value and then makes the move that Enter asked for.
  • Escape, a newer commit, or an unmounted cell drops the pending result. A stale verdict never lands on a cell the user has left.

Paste, fill, and updateCells

Each of these paths holds the batch while the schema resolves, and so does import:

  • The grid runs at most 32 validations at the same time. A paste of 10,000 cells makes 32 calls at a time.
  • Nothing changes until every verdict is in. The accepted cells then commit together as one onDataChange batch.
  • A rejected cell drops, and the other cells still commit. onValidationError reports the rejected cells. Its sources are paste, fill, Delete or Cut, find and replace, updateCells, and the toggle of an activate cell.
  • A held batch drops when a sort or filter changes, or when a target row leaves the data.
  • The import dialog keeps its Import button disabled while it validates.

While the schema runs, useDataGridGatePending() holds "validate" and the grid root carries data-gate-pending="validate". A newer paste replaces the held paste. At 200 ms per cell, 10,000 cells take about 60 seconds at 32 calls at a time. To check a large paste against a remote service, validate the whole block once in the processPaste override of Change copy and paste.

To skip validation for values that your server already checked, call actions.updateCells(patches, { skipValidation: true }).

On this page