gridcn2.0.0
Validation examples

Validation example

Try a function rule, a soft rule, a transforming rule, and a Standard Schema column for Zod, Joi, and Valibot in one live grid.

Each column of this grid rejects a bad value in its own way. A list above the grid names the rule of each column. Type a value that breaks a rule and press Enter. A blocking rule keeps the editor open and shows the message next to the cell. A soft rule commits the value and flags the cell.

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

What the demo shows

  • Age uses a function rule. It rejects a number below 18 and fires a toast from inside the function. The toast has a fixed id, so a paste with many bad cells shows one toast.
  • Email uses a sync Standard Schema. It rejects a value without an @.
  • SKU uses an async Standard Schema with about 800 ms of latency. SKU-001 and SKU-002 already exist. See Async validation example.
  • Price uses a transforming schema. The text 42.9 commits as 43, because the schema output is what the cell stores.
  • Notes uses validationMode: "warn". A note over 12 characters commits, and the cell stays flagged until a valid commit.
  • Zod Score, Joi Code, and Valibot Tag each use a library schema. Break each one to read its message.

To try the bulk path, copy a column of mixed ages from a spreadsheet and paste it onto Age. The valid cells commit and the invalid cells drop.

Demo columns

The demo lists the kind of every column above the grid. Age, Email, SKU, Price, Zod Score, Joi Code, and Valibot Tag use a blocking rule. Notes uses validationMode: "warn", a soft rule. Soft rules compares the two kinds.

Every cell that shows an error also shows its message on hover, and carries it in aria-description. A cell that is not being edited shows a tooltip. A cell with an open editor shows no tooltip popover. It carries the message in its title attribute and shows it on the line next to the cell.

A server rejection set with actions.setCellErrors looks like a soft flag. It has the same ring, tint, and tooltip, and the value stays in the grid. The cell shows the line when the user opens it. The next commit of a new value clears the error. See Server errors example.

Write the rules

validate takes a function that returns a message or null, or a Standard Schema. The grid detects a schema by its "~standard" key, so a library needs no adapter. This grid uses a function rule, a soft rule, and a Zod schema:

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

type Person = { id: string; age: number; note: string; score: number };

const columns = defineColumns<Person>()([
  {
    id: "age",
    header: "Age",
    accessorKey: "age",
    type: "number",
    validate: (value) =>
      typeof value === "number" && value < 18 ? "Must be 18 or older" : null,
  },
  {
    id: "note",
    header: "Note",
    accessorKey: "note",
    validate: (value) =>
      typeof value === "string" && value.length > 12 ? "Too long" : null,
    validationMode: "warn",
  },
  {
    id: "score",
    header: "Score",
    accessorKey: "score",
    type: "number",
    validate: z.number().int().min(0).max(100),
  },
]);

export function People({ people }: { people: Person[] }) {
  return (
    <DataGrid defaultData={people} columns={columns} getRowId={(p) => p.id} />
  );
}

Joi and Valibot schemas go in the same validate key. Joi rejects an empty string unless the schema adds .empty(""), as the Joi Code column does. A schema checks the cell value, not the row. Match the schema to the type of the column: a number column takes z.number().

On this page