new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Drizzle · MySQL · all subjects

validation/valibot

36 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Valibot data type mapping for MySQL longtext

MySQL longtext() maps to Valibot pipe(string(), maxLength(4_294_967_295)).

Valibot data type mapping for MySQL text types with enum

MySQL tinytext({ enum: ... }), mediumtext({ enum: ... }), text({ enum: ... }), longtext({ enum: ... }), char({ enum: ... }), and varchar({ enum: ... }) all map to Valibot enum(enum) schema.

createSelectSchema generates validation schema for SELECT queries

The createSelectSchema function from 'drizzle-orm/valibot' generates a Valibot validation schema that matches the shape of data returned by a SELECT query on a MySQL table. The schema includes all columns defined in the table, and validation will fail if expected columns are missing from the query result.

createInsertSchema for INSERT data validation

The createInsertSchema function from 'drizzle-orm/valibot' generates a validation schema for INSERT operations. Primary key columns with autoincrement are optional in the schema, while other columns maintain their NOT NULL constraints from the table definition.

createUpdateSchema for UPDATE data validation

The createUpdateSchema function from 'drizzle-orm/valibot' generates a validation schema for UPDATE operations. All columns in the update schema are optional, allowing partial updates to be validated.

Valibot schemas support MySQL views

createSelectSchema can be used with MySQL views created using mysqlView, generating validation schemas for view query results.

Valibot data type mapping for MySQL boolean

MySQL boolean type maps to Valibot boolean() schema.

Valibot data type mapping for MySQL enum

MySQL mysqlEnum('name', ['val1', 'val2']) maps to Valibot enum({ val1: 'val1', val2: 'val2' }) schema.

Valibot data type mapping for MySQL date and datetime

MySQL date({ mode: 'date' }), datetime({ mode: 'date' }), and timestamp({ mode: 'date' }) all map to Valibot date() schema.

Valibot data type mapping for MySQL string types

MySQL binary, date({ mode: 'string' }), datetime({ mode: 'string' }), decimal, time, timestamp({ mode: 'string' }), and varbinary all map to Valibot string() schema.

Valibot data type mapping for MySQL longblob

MySQL longblob({ mode: 'string' }) and longblob() map to Valibot pipe(string(), maxLength(4_294_967_295)).

Valibot data type mapping for MySQL varchar

MySQL varchar({ length: ... }) maps to Valibot pipe(string(), maxLength(length)).

Valibot data type mapping for MySQL varbinary with length

MySQL varbinary({ length: ... }) maps to Valibot pipe(string(), regex(/^[01]*$/), maxLength(length)).

Valibot data type mapping for MySQL double and real

MySQL double() and real() map to Valibot pipe(number(), minValue(-140_737_488_355_328), maxValue(140_737_488_355_327)), representing 48-bit integer lower and upper limit.

Valibot data type mapping for MySQL unsigned double and real

MySQL double({ unsigned: true }) and real({ unsigned: true }) map to Valibot pipe(number(), minValue(0), maxValue(281_474_976_710_655)), representing unsigned 48-bit integer lower and upper limit.

Valibot data type mapping for MySQL unsigned decimal number mode

MySQL decimal({ mode: 'number', unsigned: true }) maps to Valibot pipe(number(), minValue(0), maxValue(9_007_199_254_740_991)).

Valibot data type mapping for MySQL unsigned bigint number mode

MySQL bigint({ mode: 'number', unsigned: true }) maps to Valibot pipe(number(), minValue(0), maxValue(9_007_199_254_740_991), int()), using JavaScript min and max safe integers.

Valibot data type mapping for MySQL unsigned bigint string mode

MySQL bigint({ mode: 'string', unsigned: true }) maps to Valibot pipe(string(), regex(/^\d+$/), transform((v) => BigInt(v)), minValue(0n), maxValue(9_223_372_036_854_775_807n), transform((v) => v.toString())).

Valibot data type mapping for MySQL bigint bigint mode

MySQL bigint({ mode: 'bigint' }) maps to Valibot pipe(bigint(), minValue(-9_223_372_036_854_775_808n), maxValue(9_223_372_036_854_775_807n)), representing 64-bit integer lower and upper limit.

Valibot data type mapping for MySQL unsigned bigint bigint mode

MySQL bigint({ mode: 'bigint', unsigned: true }) maps to Valibot pipe(bigint(), minValue(0n), maxValue(18_446_744_073_709_551_615n)), representing unsigned 64-bit integer lower and upper limit.

Valibot data type mapping for MySQL serial

MySQL serial() maps to Valibot pipe(number(), minValue(0), maxValue(9_007_199_254_740_991), int()), representing JavaScript max safe integer.

Valibot data type mapping for MySQL year

MySQL year() maps to Valibot pipe(number(), minValue(1_901), maxValue(2_155), int()).

Valibot data type mapping for MySQL json

MySQL json() maps to Valibot union([union([string(), number(), boolean(), null_()]), array(any()), record(string(), any())]).

Example of createSelectSchema validation failing on partial columns

When using createSelectSchema with a partial SELECT query (selecting only specific columns), the parse() call will fail if the schema expects columns that were not selected. Only selecting id and name when age is required will cause a validation error. Selecting all columns will succeed.

Example of createInsertSchema with autoincrement primary key

When using createInsertSchema on a table with an autoincrement primary key, the id column becomes optional in the insert schema (id?: number | undefined), while required columns like name and age remain required.

Example of createUpdateSchema with all optional columns

When using createUpdateSchema, all columns including id, name, and age become optional, allowing partial updates where only specific fields need to be provided.

Drizzle supports Valibot validation library

Drizzle ORM provides support for Valibot validation library. You can install it alongside drizzle-orm@rc. Valibot validation is available for generating schemas from database table definitions.

createSelectSchema with Valibot for CockroachDB

The createSelectSchema function generates a Valibot schema from a CockroachDB table definition. This schema validates the shape of data returned by select queries. It can be used with the parse function to validate query results.

createSelectSchema example with Valibot

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core'; import { createSelectSchema } from 'drizzle-orm/valibot'; import { parse } from 'valibot'; const users = cockroachTable('users', { id: int4().primaryKey().generatedAlwaysAsIdentity(), name: text().notNull(), age: int4().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 successfully

createSelectSchema supports CockroachDB views and enums

The createSelectSchema function from Valibot support works with CockroachDB views and enums in addition to tables. You can pass a cockroachEnum or cockroachView to createSelectSchema to generate validation schemas.

createInsertSchema with Valibot for CockroachDB

The createInsertSchema function generates a Valibot schema that validates the shape of data to be inserted into the database. This schema can be used to validate API requests before inserting data.

createInsertSchema example with Valibot

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core'; import { createInsertSchema } from 'drizzle-orm/valibot'; import { parse } from 'valibot'; const users = cockroachTable('users', { id: int4().primaryKey().generatedAlwaysAsIdentity(), name: text().notNull(), age: int4().notNull() }); const userInsertSchema = createInsertSchema(users); const user = { name: 'John' }; const parsed: { name: string, age: number } = parse(userInsertSchema, user); // Error: `age` is not defined const user = { name: 'Jane', age: 30 }; const parsed: { name: string, age: number } = parse(userInsertSchema, user); // Will parse successfully await db.insert(users).values(parsed);

createUpdateSchema with Valibot for CockroachDB

The createUpdateSchema function generates a Valibot schema that validates the shape of data to be updated in the database. This schema can be used to validate API requests before updating data. Fields become optional in update schemas.

createUpdateSchema example with Valibot

import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core'; import { createUpdateSchema } from 'drizzle-orm/valibot'; import { parse } from 'valibot'; const users = cockroachTable('users', { id: int4().primaryKey().generatedAlwaysAsIdentity(), name: text().notNull(), age: int4().notNull() }); const userUpdateSchema = createUpdateSchema(users); const user = { age: 35 }; const parsed: { name?: string | undefined, age?: number | undefined } = parse(userUpdateSchema, user); // Will parse successfully await db.update(users).set(parsed).where(eq(users.name, 'Jane'));

Valibot schema refinements for extending and modifying field schemas

Each create schema function (createSelectSchema, createInsertSchema, createUpdateSchema) accepts an optional second parameter for refinements. You can pass a callback function to extend or modify a field's schema, or provide a Valibot schema to completely overwrite it. Callback functions receive the generated schema as input and can use pipe and other Valibot utilities to extend it. A provided schema will overwrite the field entirely, including its nullability.

Valibot schema refinement example with pipe and overwrite

import { int4, jsonb, cockroachTable, text } from 'drizzle-orm/cockroach-core'; import { createSelectSchema } from 'drizzle-orm/valibot'; import { parse, pipe, maxLength, object, string } from 'valibot'; const users = cockroachTable('users', { id: int4().primaryKey().generatedAlwaysAsIdentity(), name: text().notNull(), bio: text(), preferences: jsonb() }); 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, ...);

Give your agent this brain