Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zod-prisma

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-prisma

A Prisma generator that creates Zod schemas for all of your models

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
decreased by-38.98%
Maintainers
1
Weekly downloads
 
Created
Source

NPM Contributors Forks Stargazers Issues MIT License


Logo

Zod Prisma

A custom prisma generator that creates Zod schemas from your Prisma model.
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage
  4. Examples
  5. Roadmap
  6. Contributing
  7. License
  8. Contact

About The Project

I got tired of having to manually create Zod schemas for my Prisma models and of updating them everytime I made schema changes. This provides a way of automatically generating them with your prisma

Built With

Getting Started

To get a local copy up and running follow these simple steps.

Prerequisites

This project utilizes yarn and if you plan on contributing, you should too.

  • npm
    npm install -g yarn
    

Installation

  1. Add zod-prisma as a dev dependency

    yarn add -D zod-prisma # Not yet published
    
  2. Add the zod-prisma generator to your schema.prisma

    generator zod {
      provider      = "zod-prisma"
      output        = "./zod"
      relationModel = "default" # Do not export model without relations.
      # relationModel = true # Create and export both plain and related models.
      # relationModel = false # Do not generate related model
    
      modelCase = "PascalCase" # (default) Output models using pascal case (ex. UserModel, PostModel)
      # modelCase = "camelCase" # Output models using camel case (ex. userModel, postModel)
      
      modelSuffix = "Model" # (default) Suffix to apply to your prisma models when naming Zod schemas
    }
    
  3. Run npx prisma generate to generate your zod schemas

  4. Import the generated schemas form your selected output location

Usage

JSDoc Generation

Rich-comments in the Prisma schema will be transformed into JSDoc for the associated fields:

Note: make sure to use a triple-slash. Double-slash comments won't be processed.

model Post {
  /// The unique identifier for the post
  /// @default {Generated by database}
  id String @id @default(uuid())

  /// A brief title that describes the contents of the post
  title String

  /// The actual contents of the post.
  contents String
}

Generated code:

export const PostModel = z.object({
  /**
   * The unique identifier for the post
   * @default {Generated by database}
   */
  id: z.string().uuid(),
  /**
   * A brief title that describes the contents of the post
   */
  title: z.string(),
  /**
   * The actual contents of the post.
   */
  contents: z.string(),
})

Extending Zod Fields

You can also use the @zod keyword in rich-comments in the Prisma schema to extend your Zod schema fields:

model Post {
  id String @id @default(uuid()) /// @zod.uuid()

  /// @zod.max(255, { message: "The title must be shorter than 256 characters" })
  title String

  contents String /// @zod.max(10240)
}

Generated code:

export const PostModel = z.object({
  id: z.string().uuid(),
  title: z.string().max(255, { message: "The title must be shorter than 256 characters" }),
  contents: z.string().max(10240),
})

Examples

For examples, please refer to the Examples Directory or the Functional Tests

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Carter Grimmeisen - Carter.Grimmeisen@uah.edu

Project Link: https://github.com/CarterGrimmeisen/zod-prisma

FAQs

Package last updated on 15 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc