There is an open feature request for Prisma to support runtime model validation directly at the Schema level. Alternatively, you can leverage the Client Extensions to perform validation. There is an example in this blog post that shows how to perform custom runtime validation.
Answer from Raphael Etim on Stack Overfloworm - How to do Prisma runtime model validation? - Stack Overflow
validation - How to use Prisma with ValidationPipe in NestJS? - Stack Overflow
Add runtime validation to models
prisma validation
you have three solutions for it: Continue Using DTOs, Custom Validation Pipe, hybrid:
The decision largely depends on your project's specific needs and your team's familiarity with these patterns. If maintaining a clear separation and leveraging existing NestJS features is important, continue using DTOs. If reducing redundancy is a priority, consider the dynamic DTO generation or custom validation pipe approach.
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { map } from 'rxjs/operators';
@Injectable()
export class TransformInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler<any>) {
const request = context.switchToHttp().getRequest();
request.body = new TodoCreateDto(request.body.title, request.body.description);
return next.handle().pipe(map(data => data));
}
}
Prisma.TodoCreateInput is not a class but just typescript interface. For nestjs validation pipe to work it requires a javascript class with each property decorated with decorators from class-validator.
There is no way for nestjs validation pipe to validate the request.body using a typescript type or interface.
Extra :-
Although if you want to avoid writing your dto's, Prisma itself validates it's input and if fails it raises PrismaClientValidationError you can catch this globally using Nestjs exception filters and give client a 400 or any other error with prisma exceptoin message.
» npm install zod-prisma-types