How to automatically generate Yup schemas from your Prisma schema

Here is what it is advisable do:

1- Set up the generator

 npm set up prisma-yup-generator
Enter fullscreen mode

Exit fullscreen mode

 yarn add prisma-yup-generator
Enter fullscreen mode

Exit fullscreen mode

2- Add the generator to your Prisma schema

generator yup {
  supplier = "prisma-yup-generator"
}
Enter fullscreen mode

Exit fullscreen mode

3- Run npx prisma generate in your schema(or the instance beneath)

mannequin Person {
  id    Int     @id @default(autoincrement())
  e-mail String  @distinctive
  title  String?
  posts Publish[]
}

mannequin Publish {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  title     String
  content material   String?
  printed Boolean  @default(false)
  viewCount Int      @default(0)
  creator    Person?    @relation(fields: [authorId], references: [id])
  authorId  Int?
}
Enter fullscreen mode

Exit fullscreen mode

Now you’ll have all potential Yup schemas generated for you!

Add a Comment

Your email address will not be published. Required fields are marked *