prisma-to-zod-v4 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Carter Grimmeisen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,317 @@
1
+ <!--
2
+ *** Thanks for checking out the Best-README-Template. If you have a suggestion
3
+ *** that would make this better, please fork the repo and create a pull request
4
+ *** or simply open an issue with the tag "enhancement".
5
+ *** Thanks again! Now go create something AMAZING! :D
6
+ ***
7
+ ***
8
+ ***
9
+ *** To avoid retyping too much info. Do a search and replace for the following:
10
+ *** CarterGrimmeisen, zod-prisma, twitter_handle, Carter.Grimmeisen@uah.edu, Zod Prisma, A custom prisma generator that creates Zod schemas from your Prisma model.
11
+ -->
12
+
13
+ <!-- PROJECT SHIELDS -->
14
+ <!--
15
+ *** I'm using markdown "reference style" links for readability.
16
+ *** Reference links are enclosed in brackets [ ] instead of parentheses ( ).
17
+ *** See the bottom of this document for the declaration of the reference variables
18
+ *** for contributors-url, forks-url, etc. This is an optional, concise syntax you may use.
19
+ *** https://www.markdownguide.org/basic-syntax/#reference-style-links
20
+ -->
21
+
22
+ [![NPM][npm-shield]][npm-url]
23
+ [![Contributors][contributors-shield]][contributors-url]
24
+ [![Forks][forks-shield]][forks-url]
25
+ [![Stargazers][stars-shield]][stars-url]
26
+ [![Issues][issues-shield]][issues-url]
27
+ [![MIT License][license-shield]][license-url]
28
+
29
+ <!-- PROJECT LOGO -->
30
+ <br />
31
+ <p align="center">
32
+ <a href="https://github.com/CarterGrimmeisen/zod-prisma">
33
+ <img src="https://raw.githubusercontent.com/CarterGrimmeisen/zod-prisma/main/images/zod-prisma.svg" alt="Logo" width="120" height="120">
34
+ </a>
35
+ <h3 align="center">Zod Prisma</h3>
36
+ <p align="center">
37
+ A custom prisma generator that creates Zod schemas from your Prisma model.
38
+ <br />
39
+ <a href="https://github.com/CarterGrimmeisen/zod-prisma"><strong>Explore the docs »</strong></a>
40
+ <br />
41
+ <br />
42
+ <a href="https://github.com/CarterGrimmeisen/zod-prisma/blob/main/src/test/functional">View Demo</a>
43
+ ·
44
+ <a href="https://github.com/CarterGrimmeisen/zod-prisma/issues">Report Bug</a>
45
+ ·
46
+ <a href="https://github.com/CarterGrimmeisen/zod-prisma/issues">Request Feature</a>
47
+ </p>
48
+ </p>
49
+
50
+ <!-- TABLE OF CONTENTS -->
51
+ <details open="open">
52
+ <summary><h2 style="display: inline-block">Table of Contents</h2></summary>
53
+ <ol>
54
+ <li>
55
+ <a href="#about-the-project">About The Project</a>
56
+ <ul>
57
+ <li><a href="#built-with">Built With</a></li>
58
+ </ul>
59
+ </li>
60
+ <li>
61
+ <a href="#getting-started">Getting Started</a>
62
+ <ul>
63
+ <li><a href="#prerequisites">Prerequisites</a></li>
64
+ <li><a href="#installation">Installation</a></li>
65
+ </ul>
66
+ </li>
67
+ <li><a href="#usage">Usage</a>
68
+ <ul>
69
+ <li><a href="#jsdoc-generation">JSDoc Generation</a></li>
70
+ <li><a href="#extending-zod-fields">Extending Zod Fields</a></li>
71
+ <li>
72
+ <a href="#importing-helpers">Importing Helpers</a>
73
+ <ul>
74
+ <li><a href="#custom-zod-schema">Custom Zod Schemas</a></li>
75
+ </ul>
76
+ </li>
77
+ <li><a href="#json-fields">JSON Fields</a></li>
78
+ </ul>
79
+ </li>
80
+ <li><a href="#examples">Examples</a></li>
81
+ <li><a href="#roadmap">Roadmap</a></li>
82
+ <li><a href="#contributing">Contributing</a></li>
83
+ <li><a href="#license">License</a></li>
84
+ <li><a href="#contact">Contact</a></li>
85
+ </ol>
86
+ </details>
87
+
88
+ <!-- ABOUT THE PROJECT -->
89
+
90
+ ## About The Project
91
+
92
+ I got tired of having to manually create Zod schemas for my Prisma models and of updating them everytime I made schema changes.
93
+ This provides a way of automatically generating them with your prisma
94
+
95
+ <!-- [![Product Name Screen Shot][product-screenshot]](https://example.com) -->
96
+
97
+ ### Built With
98
+
99
+ - [dts-cli](https://github.com/weiran-zsd/dts-cli)
100
+ - [Zod](https://github.com/colinhacks/zod)
101
+ - [Based on this gist](https://gist.github.com/deckchairlabs/8a11c33311c01273deec7e739417dbc9)
102
+
103
+ <!-- GETTING STARTED -->
104
+
105
+ ## Getting Started
106
+
107
+ To get a local copy up and running follow these simple steps.
108
+
109
+ ### Prerequisites
110
+
111
+ This project uses pnpm.
112
+
113
+ ### Installation
114
+
115
+ 0. **Ensure your tsconfig.json enables the compiler's strict mode.**
116
+ **Zod requires it and so do we, you will experience TS errors without strict mode enabled**
117
+
118
+ 1. Add zod-prisma-v4 as a dev dependency
119
+
120
+ ```sh
121
+ pnpm add -D zod-prisma-v4
122
+ ```
123
+
124
+ 2. Add the zod-prisma-v4 generator to your schema.prisma
125
+
126
+ ```prisma
127
+ generator zod {
128
+ provider = "zod-prisma-v4"
129
+ output = "./zod" // (default) the directory where generated zod schemas will be saved
130
+
131
+ relationModel = true // (default) Create and export both plain and related models.
132
+ // relationModel = "default" // Do not export model without relations.
133
+ // relationModel = false // Do not generate related model
134
+
135
+ modelCase = "PascalCase" // (default) Output models using pascal case (ex. UserModel, PostModel)
136
+ // modelCase = "camelCase" // Output models using camel case (ex. userModel, postModel)
137
+
138
+ modelSuffix = "Model" // (default) Suffix to apply to your prisma models when naming Zod schemas
139
+
140
+ // useDecimalJs = false // (default) represent the prisma Decimal type using as a JS number
141
+ useDecimalJs = true // represent the prisma Decimal type using Decimal.js (as Prisma does)
142
+
143
+ imports = null // (default) will import the referenced file in generated schemas to be used via imports.someExportedVariable
144
+
145
+ // https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields#filtering-by-null-values
146
+ prismaJsonNullability = true // (default) uses prisma's scheme for JSON field nullability
147
+ // prismaJsonNullability = false // allows null assignment to optional JSON fields
148
+ }
149
+ ```
150
+
151
+ 3. Run `npx prisma generate` or `pnpm prisma generate` to generate your zod schemas
152
+ 4. Import the generated schemas form your selected output location
153
+
154
+ <!-- USAGE EXAMPLES -->
155
+
156
+ ## Usage
157
+
158
+ ### JSDoc Generation
159
+
160
+ [Rich-comments](https://www.prisma.io/docs/concepts/components/prisma-schema#comments)
161
+ in the Prisma schema will be transformed into JSDoc for the associated fields:
162
+
163
+ > _Note: make sure to use a triple-slash. Double-slash comments won't be processed._
164
+
165
+ ```prisma
166
+ model Post {
167
+ /// The unique identifier for the post
168
+ /// @default {Generated by database}
169
+ id String @id @default(uuid())
170
+
171
+ /// A brief title that describes the contents of the post
172
+ title String
173
+
174
+ /// The actual contents of the post.
175
+ contents String
176
+ }
177
+ ```
178
+
179
+ Generated code:
180
+
181
+ ```ts
182
+ export const PostModel = z.object({
183
+ /**
184
+ * The unique identifier for the post
185
+ * @default {Generated by database}
186
+ */
187
+ id: z.string().uuid(),
188
+ /**
189
+ * A brief title that describes the contents of the post
190
+ */
191
+ title: z.string(),
192
+ /**
193
+ * The actual contents of the post.
194
+ */
195
+ contents: z.string(),
196
+ })
197
+ ```
198
+
199
+ ### Extending Zod Fields
200
+
201
+ You can also use the `@zod` keyword in rich-comments in the Prisma schema
202
+ to extend your Zod schema fields:
203
+
204
+ ```prisma
205
+ model Post {
206
+ id String @id @default(uuid()) /// @zod.uuid()
207
+
208
+ /// @zod.max(255, { message: "The title must be shorter than 256 characters" })
209
+ title String
210
+
211
+ contents String /// @zod.max(10240)
212
+ }
213
+ ```
214
+
215
+ Generated code:
216
+
217
+ ```ts
218
+ export const PostModel = z.object({
219
+ id: z.string().uuid(),
220
+ title: z.string().max(255, { message: 'The title must be shorter than 256 characters' }),
221
+ contents: z.string().max(10240),
222
+ })
223
+ ```
224
+
225
+ ### Importing Helpers
226
+
227
+ Sometimes its useful to define a custom Zod preprocessor or transformer for your data.
228
+ zod-prisma-v4 enables you to reuse these by importing them via a config options. For example:
229
+
230
+ ```prisma
231
+ generator zod {
232
+ provider = "zod-prisma"
233
+ output = "./zod"
234
+ imports = "../src/zod-schemas"
235
+ }
236
+
237
+ model User {
238
+ username String /// @zod.refine(imports.isValidUsername)
239
+ }
240
+ ```
241
+
242
+ The referenced file can then be used by simply referring to exported members via `imports.whateverExport`.
243
+ The generated zod schema files will now include a namespaced import like the following.
244
+
245
+ ```typescript
246
+ import * as imports from '../../src/zod-schemas'
247
+ ```
248
+
249
+ #### Custom Zod Schema
250
+
251
+ In conjunction with this import option, you may want to utilize an entirely custom zod schema for a field.
252
+ This can be accomplished by using the special comment directive `@zod.custom()`.
253
+ By specifying the custom schema within the parentheses you can replace the autogenerated type that would normally be assigned to the field.
254
+
255
+ > For instance if you wanted to use `z.preprocess`
256
+
257
+ ### JSON Fields
258
+
259
+ JSON fields in Prisma disallow null values. This is to disambiguate between setting a field's value to NULL in the database and having
260
+ a value of null stored in the JSON. In accordance with this zod-prisma-v4 will default to disallowing null values, even if your JSON field is optional.
261
+
262
+ If you would like to revert this behavior and allow null assignment to JSON fields,
263
+ you can set `prismaJsonNullability` to `false` in the generator options.
264
+
265
+ ## Examples
266
+
267
+ <!-- Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources. -->
268
+
269
+ _For examples, please refer to the [Examples Directory](https://github.com/CarterGrimmeisen/zod-prisma/blob/main/examples) or the [Functional Tests](https://github.com/CarterGrimmeisen/zod-prisma/blob/main/src/test/functional)_
270
+
271
+ <!-- ROADMAP -->
272
+
273
+ ## Roadmap
274
+
275
+ See the [open issues](https://github.com/CarterGrimmeisen/zod-prisma/issues) for a list of proposed features (and known issues).
276
+
277
+ <!-- CONTRIBUTING -->
278
+
279
+ ## Contributing
280
+
281
+ 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**.
282
+
283
+ 1. Fork the Project
284
+ 2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
285
+ 3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
286
+ 4. Push to the Branch (`git push origin feature/AmazingFeature`)
287
+ 5. Open a Pull Request
288
+
289
+ <!-- LICENSE -->
290
+
291
+ ## License
292
+
293
+ Distributed under the MIT License. See `LICENSE` for more information.
294
+
295
+ <!-- CONTACT -->
296
+
297
+ ## Contact
298
+
299
+ Carter Grimmeisen - Carter.Grimmeisen@uah.edu
300
+
301
+ Project Link: [https://github.com/CarterGrimmeisen/zod-prisma](https://github.com/CarterGrimmeisen/zod-prisma)
302
+
303
+ <!-- MARKDOWN LINKS & IMAGES -->
304
+ <!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
305
+
306
+ [npm-shield]: https://img.shields.io/npm/v/zod-prisma?style=for-the-badge
307
+ [npm-url]: https://www.npmjs.com/package/zod-prisma
308
+ [contributors-shield]: https://img.shields.io/github/contributors/CarterGrimmeisen/zod-prisma.svg?style=for-the-badge
309
+ [contributors-url]: https://github.com/CarterGrimmeisen/zod-prisma/graphs/contributors
310
+ [forks-shield]: https://img.shields.io/github/forks/CarterGrimmeisen/zod-prisma.svg?style=for-the-badge
311
+ [forks-url]: https://github.com/CarterGrimmeisen/zod-prisma/network/members
312
+ [stars-shield]: https://img.shields.io/github/stars/CarterGrimmeisen/zod-prisma.svg?style=for-the-badge
313
+ [stars-url]: https://github.com/CarterGrimmeisen/zod-prisma/stargazers
314
+ [issues-shield]: https://img.shields.io/github/issues/CarterGrimmeisen/zod-prisma.svg?style=for-the-badge
315
+ [issues-url]: https://github.com/CarterGrimmeisen/zod-prisma/issues
316
+ [license-shield]: https://img.shields.io/github/license/CarterGrimmeisen/zod-prisma.svg?style=for-the-badge
317
+ [license-url]: https://github.com/CarterGrimmeisen/zod-prisma/blob/main/LICENSE
package/bin/cli.js ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('../dist/index')