prisma-extension-autozod 1.0.3 → 1.0.4
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/README.md +32 -13
- package/package.json +8 -9
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
> Contract-aware compile-time linting and runtime Zod validation for the new Prisma ORM (@prisma/orm-postgres).
|
|
8
8
|
|
|
9
9
|
`prisma-extension-autozod` connects **Zod 4** and **Prisma ORM 8**. It provides two layers of safety:
|
|
10
|
+
|
|
10
11
|
1. **Compile-time Linting**: TypeScript flags any field in your Zod schema that does not exist in your Prisma data contract.
|
|
11
12
|
2. **Runtime Middleware**: Automatically intercepts write operations (`create`, `update`, `insert`) via Prisma 8's `beforeCompile` hook, validates payloads, normalizes column naming conventions, and blocks invalid mutations before SQL reaches your database.
|
|
12
13
|
|
|
@@ -14,9 +15,9 @@
|
|
|
14
15
|
|
|
15
16
|
## Requirements
|
|
16
17
|
|
|
17
|
-
- **Node.js**: `>=
|
|
18
|
+
- **Node.js**: `>=22.0.0`
|
|
18
19
|
- **Prisma ORM**: `@prisma/orm-postgres >= 8.0.0`
|
|
19
|
-
- **Zod**:
|
|
20
|
+
- **Zod**: `>=4.0.0`
|
|
20
21
|
|
|
21
22
|
---
|
|
22
23
|
|
|
@@ -64,18 +65,30 @@ export const userValidator = defineModel(
|
|
|
64
65
|
"User",
|
|
65
66
|
z.object({
|
|
66
67
|
email: z.string().email("Invalid email address"),
|
|
67
|
-
username: z
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
username: z
|
|
69
|
+
.string()
|
|
70
|
+
.min(2, "Username must be at least 2 characters")
|
|
71
|
+
.nullable()
|
|
72
|
+
.optional(),
|
|
73
|
+
name: z
|
|
74
|
+
.string()
|
|
75
|
+
.min(2, "Name must be at least 2 characters")
|
|
76
|
+
.nullable()
|
|
77
|
+
.optional(),
|
|
78
|
+
}),
|
|
70
79
|
);
|
|
71
80
|
```
|
|
72
81
|
|
|
73
82
|
> **Compile-Time Safety in Action:**
|
|
74
83
|
> If you add a field that does not exist in the contract:
|
|
84
|
+
>
|
|
75
85
|
> ```typescript
|
|
76
|
-
> defineModel(
|
|
77
|
-
>
|
|
78
|
-
>
|
|
86
|
+
> defineModel(
|
|
87
|
+
> "User",
|
|
88
|
+
> z.object({
|
|
89
|
+
> nonExistentColumn: z.string(), // Error: Field 'nonExistentColumn' does not exist in table contract
|
|
90
|
+
> }),
|
|
91
|
+
> );
|
|
79
92
|
> ```
|
|
80
93
|
|
|
81
94
|
---
|
|
@@ -128,11 +141,9 @@ await db.orm.public.User.create({
|
|
|
128
141
|
});
|
|
129
142
|
|
|
130
143
|
// Update: Only supplied fields are validated
|
|
131
|
-
await db.orm.public.User
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
name: "Alice Wonder", // Valid; other fields are not required on updates
|
|
135
|
-
});
|
|
144
|
+
await db.orm.public.User.where({ email: "alice@prisma.io" }).update({
|
|
145
|
+
name: "Alice Wonder", // Valid; other fields are not required on updates
|
|
146
|
+
});
|
|
136
147
|
```
|
|
137
148
|
|
|
138
149
|
---
|
|
@@ -140,28 +151,36 @@ await db.orm.public.User
|
|
|
140
151
|
## API Reference
|
|
141
152
|
|
|
142
153
|
### `createZodRegistry<TModels>()`
|
|
154
|
+
|
|
143
155
|
Creates a model validator factory bound to your contract models. Validates that all fields in the schema exist in the contract table, allowing omission of auto-generated or optional fields.
|
|
156
|
+
|
|
144
157
|
```typescript
|
|
145
158
|
const defineModel = createZodRegistry<typeof models.public>();
|
|
146
159
|
const userValidator = defineModel("User", zodSchema);
|
|
147
160
|
```
|
|
148
161
|
|
|
149
162
|
### `createStrictZodRegistry<TModels>()`
|
|
163
|
+
|
|
150
164
|
Creates a strict validator factory that requires all columns in the contract table to be declared in the schema, while also rejecting unknown fields.
|
|
165
|
+
|
|
151
166
|
```typescript
|
|
152
167
|
const defineStrict = createStrictZodRegistry<typeof models.public>();
|
|
153
168
|
const userValidator = defineStrict("User", zodSchema);
|
|
154
169
|
```
|
|
155
170
|
|
|
156
171
|
### `autoZodPrisma8(options)` (alias: `autoZod`)
|
|
172
|
+
|
|
157
173
|
Prisma ORM 8 middleware factory:
|
|
174
|
+
|
|
158
175
|
- `options.contract`: The raw `contract.json` object.
|
|
159
176
|
- `options.validators`: Array of validator entries returned by `createZodRegistry` or `createStrictZodRegistry`.
|
|
160
177
|
|
|
161
178
|
### `extractColumns(contractJson)`
|
|
179
|
+
|
|
162
180
|
Helper function that inspects storage namespaces inside `contract.json` and returns a normalized dictionary of table column names.
|
|
163
181
|
|
|
164
182
|
### `normalizeKey(key: string)`
|
|
183
|
+
|
|
165
184
|
Normalizes strings to lowercase alphanumeric format for reliable case- and separator-insensitive mapping.
|
|
166
185
|
|
|
167
186
|
---
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-extension-autozod",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Contract-aware compile-time linting and runtime Zod validation for Prisma ORM 8 (@prisma/orm-postgres)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -19,13 +19,6 @@
|
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">=22.0.0"
|
|
21
21
|
},
|
|
22
|
-
"scripts": {
|
|
23
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
24
|
-
"test": "vitest run",
|
|
25
|
-
"test:watch": "vitest",
|
|
26
|
-
"typecheck": "tsc --noEmit",
|
|
27
|
-
"prepublishOnly": "npm run typecheck && npm run test && npm run build"
|
|
28
|
-
},
|
|
29
22
|
"keywords": [
|
|
30
23
|
"prisma",
|
|
31
24
|
"prisma8",
|
|
@@ -66,5 +59,11 @@
|
|
|
66
59
|
},
|
|
67
60
|
"dependencies": {
|
|
68
61
|
"@prisma/orm-family-sql": "8.0.0-rc.22"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:watch": "vitest",
|
|
67
|
+
"typecheck": "tsc --noEmit"
|
|
69
68
|
}
|
|
70
|
-
}
|
|
69
|
+
}
|