createSelectSchema function for validation
createSelectSchema generates an arktype validator for select queries. It is imported from 'drizzle-orm/arktype' and accepts a table, view, or enum as an argument. The validator returns ArkErrors on invalid data or the parsed data matching the table's full schema. If a query does not select all columns, validation will fail if all columns are defined in the table as non-optional.
createSelectSchema example with table validation
The following example demonstrates createSelectSchema with a CockroachDB table:
```ts
import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createSelectSchema } from 'drizzle-orm/arktype';
import type { ArkErrors } from 'arktype';
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: ArkErrors | { id: number; name: string; age: number } = userSelectSchema(rows[0]); // Error: `age` is not returned
const rows = await db.select().from(users).limit(1);
const parsed: ArkErrors | { id: number; name: string; age: number } = userSelectSchema(rows[0]); // Will parse successfully
```
This shows that a partial select query fails validation against the full table schema.
createInsertSchema function for insert validation
createInsertSchema generates an arktype validator for insert operations. It is imported from 'drizzle-orm/arktype' and accepts a table as an argument. The validator returns ArkErrors on invalid data or the parsed data. Fields marked as non-nullable in the table are required during insert unless they have a default value or are auto-generated.
createInsertSchema example with required field validation
The following example demonstrates createInsertSchema with insert validation:
```ts
import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createInsertSchema } from 'drizzle-orm/arktype';
import { ArkErrors } from "arktype";
const users = cockroachTable('users', {
id: int4().primaryKey().generatedAlwaysAsIdentity(),
name: text().notNull(),
age: int4().notNull()
});
const userInsertSchema = createInsertSchema(users);
const user = { name: 'John' };
const parsed: ArkErrors | { name: string, age: number } = userInsertSchema(user); // Error: `age` is not defined
const user = { name: 'Jane', age: 30 };
const parsed: ArkErrors | { name: string, age: number } = userInsertSchema(user); // Will parse successfully
if (parsed instanceof ArkErrors) {
console.error(parsed.summary);
process.exit(1);
}
await db.insert(users).values(parsed);
```
This shows that auto-generated fields (like id) are not required, but other non-nullable fields must be provided.
createUpdateSchema function for update validation
createUpdateSchema generates an arktype validator for update operations. It is imported from 'drizzle-orm/arktype' and accepts a table as an argument. The validator returns ArkErrors on invalid data or the parsed data. All fields are optional in update schemas, allowing partial updates.
createUpdateSchema example with optional fields
The following example demonstrates createUpdateSchema with update validation:
```ts
import { int4, cockroachTable, text } from 'drizzle-orm/cockroach-core';
import { createUpdateSchema } from 'drizzle-orm/arktype';
import { eq } from "drizzle-orm";
import { ArkErrors } from 'arktype';
const users = cockroachTable('users', {
id: int4().primaryKey().generatedAlwaysAsIdentity(),
name: text().notNull(),
age: int4().notNull()
});
const userUpdateSchema = createUpdateSchema(users);
const user = { age: 35 };
const parsed: ArkErrors | { name?: string | undefined, age?: number | undefined } = userUpdateSchema(user); // Will parse successfully
if (parsed instanceof ArkErrors) {
console.error(parsed.summary);
process.exit(1);
}
await db.update(users).set(parsed).where(eq(users.name, 'Jane'));
```
This shows that update schemas make all fields optional, allowing updates to single columns.
createSelectSchema for read validation
createSelectSchema generates a schema that validates data queried from the database, used to validate API responses. The schema enforces that only fields actually selected from the database are validated. If you select specific columns but try to validate against a schema that expects all columns, validation will fail.
createSelectSchema supports views and enums
The createSelectSchema function works not only with tables but also with views and enums defined in MSSQL.
createInsertSchema for insert validation
createInsertSchema generates a schema that validates data before insertion into the database, used to validate API requests. The schema includes only required fields that are not auto-generated (like primary keys with identity). Fields that are auto-generated do not appear in the insert schema.
createSelectSchema with table returning selected columns
When using createSelectSchema, if the query does not select all columns from a table, validation will fail when attempting to validate the results. The schema expects only the columns that are selected in the query.
Full createInsertSchema example with MSSQL table
Example: import { int, mssqlTable, text } from 'drizzle-orm/mssql-core'; import { createInsertSchema } from 'drizzle-orm/arktype'; import { ArkErrors } from 'arktype'; const users = mssqlTable('users', { id: int().primaryKey().identity(), name: text().notNull(), age: int().notNull() }); const userInsertSchema = createInsertSchema(users); const user = { name: 'Jane', age: 30 }; const parsed: ArkErrors | { name: string, age: number } = userInsertSchema(user); if (parsed instanceof ArkErrors) { console.error(parsed.summary); process.exit(1); } await db.insert(users).values(parsed);