valibot
Install the dependencies
npm
yarn
pnpm
bun
npm i drizzle-orm@rc valibot
Select schema
Defines the shape of data queried from the database - can be used to validate API responses.
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createSelectSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
const users = mysqlTable('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 query
const rows = await db.select().from(users).limit(1);
const parsed: { id: number; name: string; age: number } = parse(userSelectSchema, rows[0]); // Will parse successfullyViews are also supported.
import { mysqlView } from 'drizzle-orm/mysql-core';
import { gt } from 'drizzle-orm';
import { createSelectSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
const usersView = mysqlView('users_view').as((qb) => qb.select().from(users).where(gt(users.age, 18)));
const usersViewSchema = createSelectSchema(usersView);
const parsed: { id: number; name: string; age: number } = parse(usersViewSchema, ...);Insert schema
Defines the shape of data to be inserted into the database - can be used to validate API requests.
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createInsertSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
age: int().notNull()
});
const userInsertSchema = createInsertSchema(users);
const user = { name: 'John' };
const parsed: { id?: number | undefined, name: string, age: number } = parse(userInsertSchema, user); // Error: `age` is not defined
const user = { name: 'Jane', age: 30 };
const parsed: { id?: number | undefined, name: string, age: number } = parse(userInsertSchema, user); // Will parse successfully
await 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, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createUpdateSchema } from 'drizzle-orm/valibot';
import { parse } from 'valibot';
import { eq } from "drizzle-orm";
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
age: int().notNull()
});
const userUpdateSchema = createUpdateSchema(users);
const user = { age: 35 };
const parsed: {
id?: number | undefined;
name?: string | undefined;
age?: number | undefined;
} = parse(userUpdateSchema, user); // Will parse successfully
await 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.
import { int, json, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { createSelectSchema } from 'drizzle-orm/valibot';
import { parse, pipe, maxLength, object, string } from 'valibot';
const users = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text().notNull(),
bio: text(),
preferences: json()
});
const userSelectSchema = createSelectSchema(users, {
name: (schema) => pipe(schema, maxLength(20)), // Extends schema
bio: (schema) => pipe(schema, maxLength(1000)), // Extends schema before becoming nullable/optional
preferences: object({ theme: string() }) // Overwrites the field, including its nullability
});
const parsed: {
id: number;
name: string,
bio: string | null;
preferences: {
theme: string;
};
} = parse(userSelectSchema, ...);Data type reference
mysql.boolean();
// Schema
boolean();mysql.mysqlEnum('name', ['val1', 'val2']);
// Schema
enum({ val1: "val1", val2: "val2" });mysql.date({ mode: 'date' });
mysql.datetime({ mode: 'date' });
mysql.timestamp({ mode: 'date' });
// Schema
date();mysql.binary();
mysql.date({ mode: 'string' });
mysql.datetime({ mode: 'string' });
mysql.decimal();
mysql.time();
mysql.timestamp({ mode: 'string' });
mysql.varbinary();
// Schema
string();mysql.tinyblob({ mode: 'string' });
mysql.tinyblob();
pipe(string(), maxLength(255))mysql.blob({ mode: 'string' });
mysql.blob();
pipe(string(), maxLength(65_535))mysql.mediumblob({ mode: 'string' });
mysql.mediumblob();
pipe(string(), maxLength(16_777_215))mysql.longblob({ mode: 'string' });
mysql.longblob();
pipe(string(), maxLength(4_294_967_295))mysql.char({ length: ... });
// Schema
pipe(string(), length(length));mysql.varchar({ length: ... });
// Schema
pipe(string(), maxLength(length));binary({ length: ... });
// Schema
pipe(string(), regex(/^[01]*$/), maxLength(length))mysql.varbinary({ length: ... });
// Schema
pipe(string(), regex(/^[01]*$/), maxLength(length))mysql.tinytext();
// Schema
pipe(string(), maxLength(255));mysql.text();
// Schema
pipe(string(), maxLength(65_535));mysql.mediumtext();
// Schema
pipe(string(), maxLength(16_777_215));mysql.longtext();
// Schema
pipe(string(), maxLength(4_294_967_295));mysql.tinytext({ enum: ... });
mysql.mediumtext({ enum: ... });
mysql.text({ enum: ... });
mysql.longtext({ enum: ... });
mysql.char({ enum: ... });
mysql.varchar({ enum: ... });
// Schema
enum(enum);mysql.tinyint();
// Schema
pipe(number(), minValue(-128), maxValue(127), int()); // 8-bit integer lower and upper limitmysql.tinyint({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(255), int()); // unsigned 8-bit integer lower and upper limitmysql.smallint();
// Schema
pipe(number(), minValue(-32_768), maxValue(32_767), int()); // 16-bit integer lower and upper limitmysql.smallint({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(65_535), int()); // unsigned 16-bit integer lower and upper limitmysql.float();
// Schema
pipe(number(), minValue(-8_388_608), maxValue(8_388_607)); // 24-bit integer lower and upper limitmysql.mediumint();
// Schema
pipe(number(), minValue(-8_388_608), maxValue(8_388_607), int()); // 24-bit integer lower and upper limitmysql.float({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(16_777_215)); // unsigned 24-bit integer lower and upper limitmysql.mediumint({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(16_777_215), int()); // unsigned 24-bit integer lower and upper limitmysql.int();
// Schema
pipe(number(), minValue(-2_147_483_648), maxValue(2_147_483_647), int()); // 32-bit integer lower and upper limitmysql.int({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(4_294_967_295), int()); // unsgined 32-bit integer lower and upper limitmysql.double();
mysql.real();
// Schema
pipe(number(), minValue(-140_737_488_355_328), maxValue(140_737_488_355_327)); // 48-bit integer lower and upper limitmysql.double({ unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(281_474_976_710_655)); // unsigned 48-bit integer lower and upper limitmysql.decimal({ mode: 'number' });
// Schema
pipe(number(), minValue(-9_007_199_254_740_991), maxValue(9_007_199_254_740_991))mysql.decimal({ mode: 'bigint' });
// Schema
pipe(bigint(), minValue(-9_223_372_036_854_775_808n), maxValue(9_223_372_036_854_775_807n))mysql.decimal({ mode: 'number', unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(9_007_199_254_740_991))mysql.decimal({ mode: 'bigint', unsigned: true });
// Schema
pipe(bigint(), minValue(0), maxValue(18_446_744_073_709_551_615n))mysql.bigint({ mode: 'number' });
// Schema
pipe(number(), minValue(-9_007_199_254_740_991), maxValue(9_007_199_254_740_991), int()); // Javascript min. and max. safe integersmysql.bigint({ mode: 'number', unsigned: true });
// Schema
pipe(number(), minValue(0), maxValue(9_007_199_254_740_991), int()); // Javascript min. and max. safe integersmysql.bigint({ mode: 'string' });
// Schema
pipe(
string(),
regex(/^-?\d+$/),
transform((v) => BigInt(v)),
minValue(-9_223_372_036_854_775_808n),
maxValue(9_223_372_036_854_775_807n),
transform((v) => v.toString()),
)mysql.bigint({ mode: 'string', unsigned: true });
// Schema
pipe(
string(),
regex(/^\d+$/),
transform((v) => BigInt(v)),
minValue(0n),
maxValue(9_223_372_036_854_775_807n),
transform((v) => v.toString()),
);mysql.bigint({ mode: 'bigint' });
// Schema
pipe(bigint(), minValue(-9_223_372_036_854_775_808n), maxValue(9_223_372_036_854_775_807n)); // 64-bit integer lower and upper limitmysql.bigint({ mode: 'bigint', unsigned: true });
// Schema
pipe(bigint(), minValue(0n), maxValue(18_446_744_073_709_551_615n)); // unsigned 64-bit integer lower and upper limitmysql.serial();
// Schema
pipe(number(), minValue(0), maxValue(9_007_199_254_740_991), int()); // Javascript max. safe integermysql.year();
// Schema
pipe(number(), minValue(1_901), maxValue(2_155), int());mysql.json();
// Schema
union([union([string(), number(), boolean(), null_()]), array(any()), record(string(), any())]);