Prisma 2+ generator to emit Joi schemas from your Prisma schema

Overview

Prisma Joi Generator

npm version npm HitCount npm

Automatically generate Joi schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have. Updates every time npx prisma generate runs.

Table of Contents

Installation

Using npm:

 npm install prisma-joi-generator

Using yarn:

 yarn add prisma-joi-generator

Usage

1- Star this repo 😉

2- Add the generator to your Prisma schema

generator joi {
  provider = "prisma-joi-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?
}

will generate the following files

Joi Schemas

4- Use generated schemas somewhere in your API logic, like middleware or decorator

import Joi from "joi";
import { PostCreateSchema } from "./prisma/generated/schemas/createOnePost.schema.ts";

app.post('/blog', async (req, res, next) => { 
  const { body } = req; 
  const result = Joi.validate(body, PostCreateSchema); 
});

Additional Options

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

Use additional options in the schema.prisma

generator joi {
  provider   = "prisma-joi-generator"
  output     = "./generated-joi-schemas"
}
Comments
  • Make the path shorten

    Make the path shorten

    Problem

    The generated pathes are tooooooooo looooooong, so please shorten them.

    Suggested solution

    Maybe we can use a single letter to be the folder name, then give the best practice document for the definition of the folder.. just a thouths.

    enhancement 
    opened by Cyrusky 4
  • Support for Prisma 4.x

    Support for Prisma 4.x

    It works perfectly on 3.15.2, but throws this error after running pnpm prisma generate on 4.0.0 :

    Environment variables loaded from .env
    Prisma schema loaded from src/api/database/schema.prisma
    Error: 
    ✔ Generated Prisma Client (4.0.0 | library) to ./node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client in 53ms
    
    TypeError: Cannot read properties of undefined (reading 'enumTypes')
        at Object.generate [as onGenerate] (.../node_modules/.pnpm/[email protected][email protected]/node_modules/prisma-joi-generator/lib/prisma-generator.js:45:36)
        at async LineStream.<anonymous> (.../node_modules/.pnpm/@[email protected]/node_modules/@prisma/generator-helper/dist/generatorHandler.js:33:24)
    
    prisma 
    opened by Alexandre-Fernandez 2
  • Provide option to turn-on or off specific types of generated Joi files.

    Provide option to turn-on or off specific types of generated Joi files.

    Problem

    I need the ability to turn on or off specific generated Joi files. For example, *Upsert* and *Unchecked* I will never use. These files just create added files generated without any addition purpose. The problem comes in when you have a large database with many relationships and foreign relationships. Deployment systems like Docker/Google App Engine and Amazon Beanstalk have max file limits.

    Suggested solution

    In addition to break up the outputted files into directory named by table-name. Provide a list of flags that can turn on and off specific generated file-types. The files I care most about is *Create* and *Update* for my REST API.

    opened by NextStepGuru 1
  • Break up generated Joi files into sub-directories by Table name

    Break up generated Joi files into sub-directories by Table name

    Problem

    I am using the prisma-joi-generator to create Joi validator objects automatically and import them into my Koa.js project. I then use the Joi object to validate my API inputs. All of this works great. However, when I deploy the code to Google App Engine to test my API. Google throws an error stating too many files in a single directory. I have over 70 tables in my database and this generator creates 1300+ files.

    Suggested solution

    Break up the generated files by Table-name sub-directory. For example, if the table name is Post then the sub-directory would be ./generated/post/Post*.ts

    opened by NextStepGuru 1
  • FrontEnd Form Validation

    FrontEnd Form Validation

    Thanks for your work, I've been looking for a way to have one source of truth for both frontend and backend validation from Prisma and this looks very promising!

    Would it be possible to have a "frontend mode" where all the validation rules are put into one file / one file per Model, to be used for form validation? 👍

    enhancement 
    opened by MentalGear 13
Releases(0.2.0)
  • 0.2.0(Jul 13, 2022)

    What's Changed

    • Upgrade-to-prisma-4 by @omar-dulaimi in https://github.com/omar-dulaimi/prisma-joi-generator/pull/5

    Full Changelog: https://github.com/omar-dulaimi/prisma-joi-generator/commits/0.2.0

    Source code(tar.gz)
    Source code(zip)
Owner
Omar Dulaimi
Full Stack Software Engineer
Omar Dulaimi
Prisma 2+ generator to emit Zod schemas from your Prisma schema

Prisma Zod Generator Automatically generate Zod schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Omar Dulaimi 212 Dec 27, 2022
Prisma +2 generator to emit a tRPC shield from your Prisma schema

Prisma tRPC Shield Generator Automatically generate a tRPC Shield from your Prisma Schema. Updates every time npx prisma generate runs. Table of Conte

Omar Dulaimi 27 Dec 24, 2022
Prisma 2+ generator to emit fully implemented tRPC routers

Prisma tRPC Generator Automatically generate fully implemented tRPC routers from your Prisma Schema. This includes routers, app router and of course a

Omar Dulaimi 370 Jan 3, 2023
Prisma 2+ generator to emit a JSON file that can be run with json-server

Prisma JSON Server Generator A Prisma generator that automates creating a JSON file that can be run as a server from your Prisma schema. Explore the o

Omar Dulaimi 14 Jan 7, 2023
A simple CLI to generate a starter schema for keystone-6 from a pre-existing prisma schema.

Prisma2Keystone A tool for converting prisma schema to keystone schema typescript This is a proof of concept. More work is needed Usage npx prisma2key

Brook Mezgebu 17 Dec 17, 2022
Schemix allows you to programmatically create Prisma schemas using TypeScript ⌨️

Schemix Schemix let's you programmatically generate Prisma schemas. As Prisma schemas are not inherently segmentable, Schemix acts as a library to aid

Rida F'kih 197 Jan 5, 2023
A NestJS module that allows you use Prisma, set up multiple Prisma services, and use multi-tenancy in each Prisma service.

NestJS Prisma Module Installation To use this package, first install it: npm i @sabinthedev/nestjs-prisma Basic Usage In order to use this package, yo

Sabin Adams 39 Dec 2, 2022
Create a C# .NET core EntityFramework ORM from your schema.prisma file

A note of forewarning to the would-be user... This was a failure. I'm making a note here: huge regret. It's hard to overstate my dissatisfaction. ?? S

Ian Ray 9 Dec 24, 2022
GraphQL Hive provides all the tools the get visibility of your GraphQL architecture at all stages, from standalone APIs to composed schemas (Federation, Stitching)

GraphQL Hive GraphQL Hive provides all the tools the get visibility of your GraphQL architecture at all stages, from standalone APIs to composed schem

Kamil Kisiela 184 Dec 21, 2022
Merge multiple Prisma schema files, model inheritance, resolving name conflicts and timings reports, all in a simple tool.

Prisma Util What is Prisma Util? • How to use? • The configuration file • Support What is Prisma Util? Prisma Util is an easy to use tool that merges

David Hancu 21 Dec 28, 2022
Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test and also deploy.

GraphQL Blog graphqlblog.com Learn GraphQL by building a blogging engine. Create resolvers, write schemas, write queries, design the database, test an

GraphQLApps 6 Aug 17, 2022
Opinionated collection of TypeScript definitions and utilities for Deno and Deno Deploy. With complete types for Deno/NPM/TS config files, constructed from official JSON schemas.

Schemas Note: You can also import any type from the default module, ./mod.ts deno.json import { type DenoJson } from "https://deno.land/x/[email protected]

deno911 2 Oct 12, 2022
Hadmean is an internal tool generator. It is language agnostic, schema driven, extremely customizable, featured packed, user-friendly and has just one installation step.

Hadmean Report a Bug · Request a Feature · Ask a Question Table of Contents About Quick Demo Motivation Why you should try Hadmean Getting Started Pre

Hadmean 344 Dec 29, 2022
Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Vineeth.TR 16 Dec 6, 2022
This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io)

Alternative Prisma Data Proxy This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io). In order to deploy your project to

AijiUejima 60 Dec 28, 2022
Prisma Generator for Pothos Codegen.

Prisma Generator Pothos Codegen This is a prisma generator that auto generate all input types for Crud Operations to Pothos. Use it as args for crud o

Emanuel 30 Dec 22, 2022
Obsidian text generator Plugin Text generator using GPT-3 (OpenAI)

is a handy plugin for Obsidian that helps you generate text content using the powerful language model GP

null 356 Dec 29, 2022
typescript-to-jsonschema generates JSON Schema files from your Typescript sources.

fast-typescript-to-jsonschema English | 简体中文 a tool generate json schema from typescript. Feature compile Typescript to get all type information conve

yunfly 21 Nov 28, 2022
Build Schema.org graphs for JavaScript Runtimes (Browser, Node, etc). Improve your sites SEO with quick and easy Rich Results.

schema-org-graph-js The quickest and easiest way to build Schema.org graphs for JavaScript Runtimes (Browser, Node, etc). Status: ?? In Development Pl

Harlan Wilton 17 Dec 21, 2022