DEV Community

Omar Dulaimi
Omar Dulaimi

Posted on

How to automatically generate Joi schemas from your Prisma schema

I'm sure you have built an API before, where you had to manually create a Joi schema for every endpoint. It was repetitive and time-consuming. But not anymore with the help of the Prisma generator I have recently built.

Here's what you need to do:

1- Install the generator

  • Using npm
 npm install prisma-joi-generator
Enter fullscreen mode Exit fullscreen mode
  • Using yarn:
 yarn add prisma-joi-generator
Enter fullscreen mode Exit fullscreen mode

2- Add the generator to your Prisma schema

generator joi {
  provider = "prisma-joi-generator"
}
Enter fullscreen mode Exit fullscreen mode

3- Run npx prisma generate for your schema(or the example below)

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?
}
Enter fullscreen mode Exit fullscreen mode

Now you will have all possible Joi schemas generated for you!

Joi schemas

Top comments (0)