{"id":16663,"library":"prisma-class-validator-generator","title":"Prisma Class Validator Generator","description":"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.","status":"active","version":"6.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/omar-dulaimi/prisma-class-validator-generator","tags":["javascript","prisma","prisma-client","prisma-schema","class-validator","prisma-generator","prisma-class-validator-generator"],"install":[{"cmd":"npm install prisma-class-validator-generator","lang":"bash","label":"npm"},{"cmd":"yarn add prisma-class-validator-generator","lang":"bash","label":"yarn"},{"cmd":"pnpm add prisma-class-validator-generator","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for the generator to run and for the generated models to integrate with Prisma Client. The user's project must have Prisma installed.","package":"prisma","optional":false},{"reason":"The generated models are decorated with `class-validator` decorators, making `class-validator` a runtime dependency for the consumer's application.","package":"class-validator","optional":false},{"reason":"Often 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.","package":"class-transformer","optional":true}],"imports":[{"note":"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.","wrong":"const User = require('./generated/models');","symbol":"User","correct":"import { User } from './generated/models';"},{"note":"The core function from `class-validator` to perform validation on an instance of a generated class.","wrong":"const validate = require('class-validator').validate;","symbol":"validate","correct":"import { validate } from 'class-validator';"},{"note":"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.","wrong":"import { plainToClass } from 'class-validator';","symbol":"plainToClass","correct":"import { plainToClass } from 'class-transformer';"}],"quickstart":{"code":"/* schema.prisma */\ngenerator client {\n  provider = \"prisma-client\"\n  output   = \"../generated/prisma-client\"\n}\n\ngenerator class_validator {\n  provider = \"prisma-class-validator-generator\"\n  output   = \"./generated\"\n}\n\nmodel User {\n  id    Int     @id @default(autoincrement())\n  email String  @unique\n  name  String?\n  posts Post[]\n}\n\nmodel Post {\n  id        Int      @id @default(autoincrement())\n  createdAt DateTime @default(now())\n  updatedAt DateTime @updatedAt\n  title     String\n  content   String?\n  published Boolean  @default(false)\n  viewCount Int      @default(0)\n  author    User?    @relation(fields: [authorId], references: [id])\n  authorId  Int?\n  rating    Float\n}\n\n/* Usage in TypeScript file */\nimport { User } from './generated/models';\nimport { validate } from 'class-validator';\nimport { plainToClass } from 'class-transformer';\n\nasync function validateUser(userData: any) {\n  const userInstance = plainToClass(User, userData);\n  const errors = await validate(userInstance);\n\n  if (errors.length > 0) {\n    console.error('Validation failed:', errors);\n    return false;\n  } else {\n    console.log('Validation successful!');\n    return true;\n  }\n}\n\n// Example usage:\nvalidateUser({ email: 'test@example.com', name: 'John Doe' });\nvalidateUser({ email: 'invalid-email', name: 123 }); // This should fail\n\n// To run this, first ensure `prisma generate` has been executed:\n// npx prisma generate\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Migrate any custom logic or validations related to Prisma `Bytes` fields to use `Uint8Array` instead of Node.js `Buffer` instances.","message":"Version 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.","severity":"breaking","affected_versions":">=6.0.0"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=5.0.0 <6.0.0"},{"fix":"Ensure your `schema.prisma` file includes a `generator client` block, e.g., `generator client { provider = \"prisma-client\" output = \"../generated/prisma-client\" }`.","message":"The 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.","severity":"gotcha","affected_versions":">=0.2.3"},{"fix":"Upgrade your Node.js environment to version 18.18+, 20.9+, or 22.11+ and your TypeScript dependency to 5.8+.","message":"Version 6.x requires Node.js 18+ and TypeScript 5.8+. Using older versions may lead to build errors or runtime incompatibilities.","severity":"gotcha","affected_versions":">=6.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Run `npm install prisma-class-validator-generator` or `yarn add prisma-class-validator-generator` in your project.","cause":"The `prisma-class-validator-generator` package is not installed or not accessible in the project's node_modules.","error":"Error: Generator 'class_validator' could not be found."},{"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.","cause":"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.","error":"Cannot find module './generated/models' or './generated/models/User'"},{"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);`","cause":"`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.","error":"Validation fails unexpectedly for incoming JSON objects, even with seemingly correct data."}],"ecosystem":"npm"}