Registry / database / prisma-class-validator-generator

prisma-class-validator-generator

JSON →
library6.2.0jsnpmunverified

The `prisma-class-validator-generator` is a utility that automates the creation of TypeScript data transfer objects (DTOs) directly from a Prisma schema, automatically decorating them with `class-validator` rules. This eliminates significant boilerplate for developers working with Prisma and `class-validator` for input validation. The package is currently stable at version `6.2.0`, following a relatively active release cadence with semantic versioning adopted since v6.1.0, indicating consistent development and maintenance. Key differentiators include its tight integration with Prisma, providing automatic mapping of Prisma types to appropriate validation decorators, ensuring type-safety, and offering a zero-configuration setup for immediate use. It supports modern Prisma versions, currently `Prisma 6.12+`, and is designed for Node.js 18+ environments with TypeScript 5.8+. This generator is crucial for projects aiming for robust backend validation without manually synchronizing schema changes with validation classes.

npm install prisma-class-validator-generator
INSTALL
IMPORT
SIG · PRISMA-CLASS-VALID
P
prisma-class-validator-generator
databasejavascriptv6.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

User
import { User } from './generated/models';
const User = require('./generated/models');
This imports a generated model class. The path 'generated/models' assumes the default output directory and single file generation. Adjust the path if 'output' is configured differently or `outputToMultipleFiles` is enabled.
validate
import { validate } from 'class-validator';
const validate = require('class-validator').validate;
The core function from `class-validator` to perform validation on an instance of a generated class.
plainToClass
import { plainToClass } from 'class-transformer';
import { plainToClass } from 'class-validator';
Crucial for validating plain JavaScript objects (e.g., from API requests) as `class-validator` requires class instances to apply decorators. `class-transformer` is a common companion library.

This quickstart demonstrates how to configure the `prisma-class-validator-generator` in your Prisma schema, generate the TypeScript models, and then use `class-validator` along with `class-transformer` to validate data against the generated `User` class.

/* schema.prisma */ generator client { provider = "prisma-client" output = "../generated/prisma-client" } generator class_validator { provider = "prisma-class-validator-generator" output = "./generated" } model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] } model Post { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt title String content String? published Boolean @default(false) viewCount Int @default(0) author User? @relation(fields: [authorId], references: [id]) authorId Int? rating Float } /* Usage in TypeScript file */ import { User } from './generated/models'; import { validate } from 'class-validator'; import { plainToClass } from 'class-transformer'; async function validateUser(userData: any) { const userInstance = plainToClass(User, userData); const errors = await validate(userInstance); if (errors.length > 0) { console.error('Validation failed:', errors); return false; } else { console.log('Validation successful!'); return true; } } // Example usage: validateUser({ email: 'test@example.com', name: 'John Doe' }); validateUser({ email: 'invalid-email', name: 123 }); // This should fail // To run this, first ensure `prisma generate` has been executed: // npx prisma generate
Debug
Known issues
breakingVersion 6.0.0 introduced a breaking change for `Bytes` fields, now supporting `Uint8Array` instead of `Buffer`. Projects using `Bytes` types in Prisma schemas will need to update their handling of these fields accordingly.
fix
Migrate any custom logic or validations related to Prisma `Bytes` fields to use `Uint8Array` instead of Node.js `Buffer` instances.
affects: >=6.0.0
breakingVersion 5.0.0 primarily upgraded support to Prisma 5.x. Users on older Prisma versions (4.x or earlier) should expect breaking changes and are required to upgrade their Prisma client and schema to be compatible.
fix
Upgrade your `prisma` and `@prisma/client` packages to version 5.x or newer and run `npx prisma generate`. Review Prisma's official migration guides for specific changes.
affects: >=5.0.0 <6.0.0
gotchaThe generator requires a `prisma-client` or `prisma-client-js` generator to be present and configured in the same `schema.prisma` file for proper type inference and functionality. Without it, the generator may fail or produce incorrect output.
fix
Ensure your `schema.prisma` file includes a `generator client` block, e.g., `generator client { provider = "prisma-client" output = "../generated/prisma-client" }`.
affects: >=0.2.3
gotchaVersion 6.x requires Node.js 18+ and TypeScript 5.8+. Using older versions may lead to build errors or runtime incompatibilities.
fix
Upgrade your Node.js environment to version 18.18+, 20.9+, or 22.11+ and your TypeScript dependency to 5.8+.
affects: >=6.0.0
Errors
Common errors & fixes
Error: Generator 'class_validator' could not be found.
The `prisma-class-validator-generator` package is not installed or not accessible in the project's node_modules.
fix
Run `npm install prisma-class-validator-generator` or `yarn add prisma-class-validator-generator` in your project.
Cannot find module './generated/models' or './generated/models/User'
The generated files do not exist or the import path is incorrect. This usually happens if `npx prisma generate` hasn't been run, or if the `output` path in `schema.prisma` differs from the import statement.
fix
Ensure you've run `npx prisma generate` successfully. Verify the `output` path in your `schema.prisma` generator configuration matches your import path, and check if the files were indeed created at the specified location.
Validation fails unexpectedly for incoming JSON objects, even with seemingly correct data.
`class-validator` requires class instances to apply validation decorators. If you're passing plain JavaScript objects (e.g., from an API request body) directly to `validate()`, the decorators won't be applied.
fix
Use `class-transformer`'s `plainToClass` function to convert your plain object into an instance of the generated class before calling `validate()`. Example: `const instance = plainToClass(MyGeneratedClass, plainObject); await validate(instance);`
Upgrade
Version history
6.2.0latest on npm
Audit
Dependencies
prismarequiredRequired for the generator to run and for the generated models to integrate with Prisma Client. The user's project must have Prisma installed.
class-validatorrequiredThe generated models are decorated with `class-validator` decorators, making `class-validator` a runtime dependency for the consumer's application.
class-transformeroptionalOften used alongside `class-validator` to transform plain objects into class instances, which is necessary for `class-validator` to apply decorators correctly. While not explicitly required by the generator, it's a critical runtime dependency for practical use cases.
Agent activity
10 hits · last 30 days
node
10
Resources
prisma-class-validator-generator — npm install prisma-class-validator-generator · libregistry