prisma-class-validator-generator

5.0.0 • Public • Published

Prisma Class Validator Generator

npm version npm HitCount npm

Automatically generate typescript models of your database with class validator validations ready, from your Prisma Schema. Updates every time npx prisma generate runs.

Buy Me A Coffee

Table of Contents

Supported Prisma Versions

Probably no breaking changes for this library, so try newer versions first.

Note: Starting from Prisma v5, library versions will match Prisma versions.

Prisma 5

  • 5.0.0 and higher

Prisma 4

  • 0.2.0 and higher

Prisma 2/3

  • 0.1.1 and lower

Installation

Using npm:

 npm install prisma-class-validator-generator

Using yarn:

 yarn add prisma-class-validator-generator

Usage

1- Star this repo 😉

2- Add the generator to your Prisma schema

generator class_validator {
  provider = "prisma-class-validator-generator"
}

3- Running npx prisma generate for the following schema.prisma

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
}

will generate the following files

Typescript models with class validator

Inside User model:

import { IsInt, IsDefined, IsString, IsOptional } from "class-validator";
import { Post } from "./";

export class User {
    @IsDefined()
    @IsInt()
    id!: number;

    @IsDefined()
    @IsString()
    email!: string;

    @IsOptional()
    @IsString()
    name?: string;

    @IsDefined()
    posts!: Post[];
}

Additional Options

Option  Description Type  Default
output Output directory for the generated models string ./generated

Use additional options in the schema.prisma

generator class_validator {
  provider   = "prisma-class-validator-generator"
  output     = "./generated-models"
}

Package Sidebar

Install

npm i prisma-class-validator-generator

Weekly Downloads

1,645

Version

5.0.0

License

MIT

Unpacked Size

33.1 kB

Total Files

33

Last publish

Collaborators

  • omar-dulaimi