prisma-class-validator-generator
Version:
Prisma 2+ generator to emit typescript models of your database with class validator
134 lines (94 loc) • 3.31 kB
Markdown
# Prisma Class Validator Generator
[](https://badge.fury.io/js/prisma-class-validator-generator)
[](https://www.npmjs.com/package/prisma-class-validator-generator)
[](http://hits.dwyl.com/omar-dulaimi/prisma-class-validator-generator)
[](LICENSE)
Automatically generate typescript models of your database with class validator validations ready, from your [Prisma](https://github.com/prisma/prisma) Schema. Updates every time `npx prisma generate` runs.
<p align="center">
<a href="https://www.buymeacoffee.com/omardulaimi">
<img src="https://cdn.buymeacoffee.com/buttons/default-black.png" alt="Buy Me A Coffee" height="41" width="174">
</a>
</p>
## Table of Contents
- [Supported Prisma Versions](#supported-prisma-versions)
- [Installation](#installing)
- [Usage](#usage)
- [Additional Options](#additional-options)
# 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:
```bash
npm install prisma-class-validator-generator
```
Using yarn:
```bash
yarn add prisma-class-validator-generator
```
# Usage
1- Star this repo 😉
2- Add the generator to your Prisma schema
```prisma
generator class_validator {
provider = "prisma-class-validator-generator"
}
```
3- Running `npx prisma generate` for the following schema.prisma
```prisma
model User {
id Int
email String
name String?
posts Post[]
}
model Post {
id Int
createdAt DateTime
updatedAt DateTime
title String
content String?
published Boolean
viewCount Int
author User?
authorId Int?
rating Float
}
```
will generate the following files

Inside `User` model:
```ts
import { IsInt, IsDefined, IsString, IsOptional } from "class-validator";
import { Post } from "./";
export class User {
id!: number;
email!: string;
name?: string;
posts!: Post[];
}
```
## Additional Options
| Option | Â Description | Type | Â Default |
| -------- | ----------------------------------------- | -------- | ------------- |
| `output` | Output directory for the generated models | `string` | `./generated` |
Use additional options in the `schema.prisma`
```prisma
generator class_validator {
provider = "prisma-class-validator-generator"
output = "./generated-models"
}
```