npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-class-validator

v1.1.3

Published

Automatically generator class-validator schema from Prisma client

Downloads

3,197

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.

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]
`);
`/**
 * 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