Starting from drizzle-orm@1.0.0-beta.15, drizzle-valibot has been deprecated in favor of first-class schema generation support within Drizzle ORM itself
You can still use drizzle-valibot package but all new update will be added to Drizzle ORM directly
valibot
Install the dependencies
npm
yarn
pnpm
bun
npm i valibot
yarn add valibot
pnpm add valibot
bun add valibot
Select schema
Defines the shape of data queried from the database - can be used to validate API responses.
import { int, singlestoreTable, text } from 'drizzle-orm/singlestore-core';import { createSelectSchema } from 'drizzle-orm/valibot';import { parse } from 'valibot';const users = singlestoreTable('users', { id: int().primaryKey().autoincrement(), name: text().notNull(), age: int().notNull()});const userSelectSchema = createSelectSchema(users);const rows = await db.select({ id: users.id, name: users.name }).from(users).limit(1);const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // Error: `age` is not returned in the above queryconst rows = await db.select().from(users).limit(1);const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // Will parse successfully
Column enums are supported through the SingleStore table schema and can be validated with the generated table schemas.
Insert schema
Defines the shape of data to be inserted into the database - can be used to validate API requests.
import { int, singlestoreTable, text } from 'drizzle-orm/singlestore-core';import { createInsertSchema } from 'drizzle-orm/valibot';import { parse } from 'valibot';const users = singlestoreTable('users', { id: int().primaryKey().autoincrement(), name: text().notNull(), age: int().notNull()});const userInsertSchema = createInsertSchema(users);const user = { name: 'John' };const parsed: { name: string, age: number } = parse(userInsertSchema, user); // Error: `age` is not definedconst user = { name: 'Jane', age: 30 };const parsed: { name: string, age: number } = parse(userInsertSchema, user); // Will parse successfullyawait db.insert(users).values(parsed);
Update schema
Defines the shape of data to be updated in the database - can be used to validate API requests.
import { int, singlestoreTable, text } from 'drizzle-orm/singlestore-core';import { createUpdateSchema } from 'drizzle-orm/valibot';import { parse } from 'valibot';const users = singlestoreTable('users', { id: int().primaryKey().autoincrement(), name: text().notNull(), age: int().notNull()});const userUpdateSchema = createUpdateSchema(users);const user = { id: 5, name: 'John' };const parsed: { name?: string | undefined, age?: number | undefined } = parse(userUpdateSchema, user); // Error: `id` is a generated column, it can't be updatedconst user = { age: 35 };const parsed: { name?: string | undefined, age?: number | undefined } = parse(userUpdateSchema, user); // Will parse successfullyawait db.update(users).set(parsed).where(eq(users.name, 'Jane'));
Refinements
Each create schema function accepts an additional optional parameter that you can used to extend, modify or completely overwite a field’s schema. Defining a callback function will extend or modify while providing a Valibot schema will overwrite it.