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.
| Column | Rule | Kind |
|---|---|---|
| Age | 18 or older. Also fires a toast. | Blocking |
| Must contain an @. | Blocking | |
| SKU | SKU-001 and SKU-002 are taken. The check takes about 800 ms and the input is read-only meanwhile. | Blocking |
| Price | Must be a number. A decimal commits rounded: 42.9 becomes 43. | Blocking |
| Notes | At most 12 characters. | Soft |
| Zod Score | Integer from 0 to 100. | Blocking |
| Joi Code | Uppercase letters, digits, or dashes. | Blocking |
| Valibot Tag | At 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.
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.9commits as43, 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().
Related
- Validation: the
validatecontract and the soft rules - Transforms: why the schema output commits
- Async validation example: the pending state and the bulk paths
- Cross-field validation example:
validateRow - Server errors example: a rejection after the commit
Streaming rows example
Push batches of cell updates into a live ticker four times a second and choose between an immediate and a deferred re-sort.
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.