Generate ArkType validation schemas from your Prisma schema.
This package is heavily inspired by and based on the structure of prismabox, which generates TypeBox schemas from Prisma schemas.
- 🎯 Type-safe validation - Generate ArkType schemas that match your Prisma models
- 🔄 Automatic generation - Schemas are generated automatically when you run
prisma generate - 📦 Comprehensive coverage - Generates schemas for models, relations, where clauses, select, include, orderBy, and more
- 🎨 Customizable - Control schema generation with annotations
- 🚀 Zero config - Works out of the box with sensible defaults
npm install prisma-arktype arktype
# or
pnpm add prisma-arktype arktype
# or
yarn add prisma-arktype arktypeAdd the generator to your schema.prisma file:
generator prisma-arktype {
provider = "prisma-arktype"
output = "./generated/validators"
}
model User {
id String @id @default(cuid())
email String @unique
name String?
posts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Post {
id String @id @default(cuid())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}Then run:
npx prisma generateConfigure the generator in your schema.prisma:
generator prisma-arktype {
provider = "prisma-arktype"
output = "./generated/validators"
arktypeImportDependencyName = "arktype"
ignoredKeysOnInputModels = ["id", "createdAt", "updatedAt"]
}| Option | Type | Default | Description |
|---|---|---|---|
output |
string |
"./prisma/generated/validators" |
Output directory for generated schemas |
arktypeImportDependencyName |
string |
"arktype" |
The package name to import from |
ignoredKeysOnInputModels |
string[] |
["id", "createdAt", "updatedAt"] |
Fields to exclude from input models |
For each model, the generator creates multiple schema types:
ModelPlain- Fields without relationshipsModelRelations- Relationship definitions onlyModel- Complete composite schema (Plain & Relations)ModelWhere- Where clause schemaModelWhereUnique- Unique where clause schemaModelCreate- Create input schemaModelUpdate- Update input schemaModelSelect- Select schemaModelInclude- Include schemaModelOrderBy- OrderBy schema
import { type } from "arktype";
import { User, UserCreate, UserWhere } from "./generated/validators";
// Validate a user object
const userResult = User(someUserData);
if (userResult instanceof type.errors) {
console.error(userResult.summary);
} else {
// userResult is validated user data
console.log(userResult);
}
// Validate create input
const createData = {
email: "[email protected]",
name: "John Doe"
};
const createResult = UserCreate(createData);
// ...
// Validate where clauses
const whereClause = {
email: "[email protected]"
};
const whereResult = UserWhere(whereClause);
// ...Control schema generation using annotations in your Prisma schema:
/// @prisma-arktype.hide
model InternalModel {
id String @id
}
model User {
id String @id
/// @prisma-arktype.hide
internalField String
}model User {
id String @id
/// @prisma-arktype.input.hide
computedField String
/// @prisma-arktype.create.input.hide
onlyInUpdates String
/// @prisma-arktype.update.input.hide
onlyInCreates String
}model User {
id String @id
/// @prisma-arktype.typeOverwrite="string.email"
email String
/// @prisma-arktype.typeOverwrite="string.numeric"
phone String
}model User {
id String @id
/// @prisma-arktype.options{minLength: 3, maxLength: 50}
username String
}Prisma types are mapped to ArkType as follows:
| Prisma Type | ArkType Type |
|---|---|
String |
"string" |
Int |
"integer" |
BigInt |
"integer" |
Float |
"number" |
Decimal |
"number" |
Boolean |
"boolean" |
DateTime |
"Date" |
Json |
"unknown" |
Bytes |
"instanceof Buffer" |
| Enums | Union of literal values |
While this package is inspired by prismabox, there are some key differences:
- ArkType vs TypeBox: Uses ArkType's syntax and type system instead of TypeBox
- Simpler type definitions: ArkType's string-based syntax makes schemas more readable
- No nullable wrapper: ArkType handles nullable types directly with union syntax
- Different validation API: Uses ArkType's validation approach
# Clone the repository
git clone https://github.com/yourusername/prisma-arktype.git
cd prisma-arktype
# Install dependencies
pnpm install
# Build the project
pnpm build
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run linter
pnpm lint
# Fix linting issues
pnpm lint:fixThis project uses Changesets for version management and publishing.
When you make changes that should be included in the next release:
pnpm changesetThis will prompt you to:
- Select the type of change (major, minor, patch)
- Provide a description of the changes
Commit the generated changeset file along with your changes.
- Create a changeset for your changes
- Open a PR with your changes and the changeset
- Merge the PR - The GitHub Action will automatically create a "Version Packages" PR
- Review and merge the Version Packages PR - This will:
- Update the version in package.json
- Update the CHANGELOG.md
- Publish the package to npm
- Create a GitHub release
# Build and publish
pnpm releasePrerequisites:
- Set up
NPM_TOKENsecret in GitHub repository settings - Ensure you have publish access to the npm package
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Create a changeset (
pnpm changeset) - Commit your changes following the commit message format (see below)
- Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project uses Conventional Commits. Commit messages are automatically linted using commitlint and lefthook.
Format: <type>(<scope>): <subject>
Types:
feat: New featurefix: Bug fixdocs: Documentation changesstyle: Code style changes (formatting, etc.)refactor: Code refactoringperf: Performance improvementstest: Adding or updating testsbuild: Build system changesci: CI/CD changeschore: Other changes
Examples:
git commit -m "feat: add support for custom type validators"
git commit -m "fix: resolve issue with nullable DateTime fields"
git commit -m "docs: update installation instructions"
git commit -m "refactor: simplify where clause generation"This project uses lefthook to manage git hooks:
- commit-msg: Validates commit message format
- pre-commit: Runs linter and checks for debug statements
- pre-push: Runs tests before pushing
To skip hooks (use sparingly):
git commit --no-verify -m "your message"MIT
This package is heavily based on prismabox by m1212e. Many thanks for the excellent foundation and architecture!