You can inject custom Zod validation logic directly into your schema.prisma file using rich comments (///). This allows you to define specific constraints, error messages, and even custom logic for your generated schemas.
Syntax:
/// @zod.[zod-type + optional[(zod-error-messages)]].[zod validators for scalar-type]
Capabilities:
- Standard Zod methods:
.min(), .max(), .gt(), .lt(), .int(), etc. - Custom logic: Use
@zod.custom.use(...) to pass a full Zod expression (e.g., .refine() or .lazy()). - Imports: Use
/// @zod.import(["import { x } from 'y'"]) at the model level to make external functions available to your custom validators. - Exclusion: Use
/// @zod.omit(["field1", "field2"]) to exclude specific fields from generated schemas.
/// @zod.import(["import { myFunction } from 'mypackage';"])
model MyPrismaScalarsType {
/// @zod.string({ invalid_type_error: "error message" }).cuid()
id String @id @default(cuid())
/// Some comment about string @zod.string.min(3, { message: "min error" }).max(10, { message: "max error" })
string String?
/// @zod.custom.use(z.string().refine((val) => validator.isBIC(val), { message: 'BIC is not valid' }))
bic String?
/// @zod.custom.use(z.lazy(() => InputJsonValue).refine((val) => myFunction(val), { message: 'Is not valid' }))
json Json
/// @zod.custom.omit(["model", "input"])
exclude String?
}