prisma-class-validator

Automatically generator class-validator schema from Prisma client

Usage no npm install needed!

<script type="module">
  import prismaClassValidator from 'https://cdn.skypack.dev/prisma-class-validator';
</script>

README

💎 Prisma Class Validator

Automatically generator class-validator schema from Prisma client projects.

| | Status | | - | - | | Build | Node CI Dependencies GitHub release (latest by date) Snyk Vulnerabilities for GitHub Repo | | Health | License CI CLA Assistant Pull Request Labeler | | PRs | PR Generator CI Merge PRs |

At Koj, we use this package to automatically generate @koj/types from our Prisma schema.

⭐️ Features

The readAndGenerateClassValidator method reads your generated Prisma client (after running npx prisma generate) and automatically generates a class-validator schema.

First, install from npm:

npm install --save-dev prisma-class-validator

Usage with TypeScript/Node.js:

import { readAndGenerateClassValidator } from "prisma-class-validator";
import { writeFile } from "fs/promises";

(async () => {
  const schema = await readAndGenerateClassValidator();
  await writeFile("validator.ts", schema);
})();

Usage with JavaScript/Node.js:

const { readAndGenerateClassValidator } = require("prisma-class-validator");
const { writeFile } = require("fs/promises");

(async () => {
  const schema = await readAndGenerateClassValidator();
  await writeFile("validator.ts", schema);
})();

💻 Example

The generateClassValidatorFromPrismaClient method is used to take a string consisting of generated Prisma schema and converting it to class-validator. In this case, you are expected to read the generated Prisma schema index.d.ts file and supply that as the input.

Example input
generateClassValidatorFromPrismaClient(`
  /**
   * Client
  **/
  
  import * as runtime from '@prisma/client/runtime';
  
  
  /**
   * Model Lead
   */
  
  export type Lead = {
    browser: string | null
    city: string | null
    countryCode: string | null
    createdAt: Date
    email: string
    id: number
    name: string
    operatingSystem: string | null
    region: string | null
    responses: Prisma.JsonValue | null
    timezone: string | null
    updatedAt: Date
  }
  
  /**
   * Model User
   */
  
  export type User = {
    active: boolean
    attributes: Prisma.JsonValue | null
    checkLocationOnLogin: boolean
    countryCode: string
    createdAt: Date
    gender: Gender
  }

  /**
   * Enums
   */

  // Based on
  // https://github.com/microsoft/TypeScript/issues/3192#issuecomment-261720275

  export const Gender: {
    FEMALE: 'FEMALE',
    MALE: 'MALE',
    NONBINARY: 'NONBINARY',
    UNKNOWN: 'UNKNOWN'
  };

  export type Gender = (typeof Gender)[keyof typeof Gender]
`);
Resulting output
`/**
 * DO NOT EDIT THIS FILE MANUALLY
 * ==============================
 *
 * This file is automatically generated by Prisma Class Validator using @prisma/client
 * Source: https://github.com/koj-co/prisma-class-validator/blob/HEAD/scripts/generate-types.ts
 */

import {
  IsOptional,
  IsString,
  IsNumber,
  IsNotEmpty,
  IsBoolean,
  IsObject,
  IsDateString,
  IsIn,
} from "class-validator";

/**
 * Model Lead
 */

export class Lead {
  @IsString()
  @IsOptional()
  browser?: string | null;

  @IsString()
  @IsOptional()
  city?: string | null;

  @IsString()
  @IsOptional()
  countryCode?: string | null;

  @IsDateString()
  @IsNotEmpty()
  createdAt!: Date;

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

  @IsNumber()
  @IsNotEmpty()
  id!: number;

  @IsString()
  @IsNotEmpty()
  name!: string;

  @IsString()
  @IsOptional()
  operatingSystem?: string | null;

  @IsString()
  @IsOptional()
  region?: string | null;

  @IsObject()
  @IsOptional()
  responses?: any | null;

  @IsString()
  @IsOptional()
  timezone?: string | null;

  @IsDateString()
  @IsNotEmpty()
  updatedAt!: Date;
}

/**
 * Model User
 */

export class User {
  @IsBoolean()
  @IsNotEmpty()
  active!: boolean;

  @IsObject()
  @IsOptional()
  attributes?: any | null;

  @IsBoolean()
  @IsNotEmpty()
  checkLocationOnLogin!: boolean;

  @IsString()
  @IsNotEmpty()
  countryCode!: string;

  @IsDateString()
  @IsNotEmpty()
  createdAt!: Date;
  
  @IsString()
  @IsIn(["FEMALE", "MALE", "NONBINARY", "UNKNOWN"])
  @IsNotEmpty()
  gender!: Gender;
}

/**
 * Enums
 */

// Based on
// https://github.com/microsoft/TypeScript/issues/3192#issuecomment-261720275

export let Gender: {
  FEMALE: "FEMALE";
  MALE: "MALE";
  NONBINARY: "NONBINARY";
  UNKNOWN: "UNKNOWN";
};

export type Gender = typeof Gender[keyof typeof Gender];`;

📄 License

MIT © Koj

Koj

An open source project by Koj.
Furnish your home in style, for as low as CHF175/month →