#Best possible way to handle validation
1 messages · Page 1 of 1 (latest)
You need to create exception filter for necord context and catch validation error
@vast valley not possible through exception filter as it will run after the guard, I want that the error shuold be geneerated before as the field is required in the payload
Exception filter catch all exceptions in execution (guards, pipes,interceptors your main code)
Also you can read necord validation article
I am using Global Pipes to do the validation of the dto , as the data coming from the client side have some field, and that field is getting modified or added to the request object (field is coming from the db service) through the interceptor, as interceptor runs before the pipe so the pipe is not giving me error that the field is missing, please suggest me some ideas to implement the validation of the dto.
Techniques - Validation
@vast valley I need the error as the client hits the request since the field is required
Problem is interceptor which override some fields?
@vast valley yes
What dto you have?
I am having a value field in it
Do you use ValidationPipe from nestjs?
yes i have used
Can you show example of your dto?
app.useGlobalPipes(
new ValidationPipe({
whitelist: true, // Strip properties that are not in the DTO
forbidNonWhitelisted: true, // Throw error for properties not in the DTO
transform: true, // Don't transform payloads
}),
);
app.useGlobalFilters(new AllExceptionsFilter());
It’s looks good
It is something like this
export class TagDto{
@IsNotEmpty()
@IsObject()
value: object
}
TagDto without any fields?
So, ok, how do you use it in controller?
I mean declaration
@Post()
async tag(@Body() tagDto: TagDto){}
I guess it’s wrong channel for it question, post it to #1025199348096700476
It’s necord-related (lib for discord bots) channel
I have posted it but very less response, Thank you for your time @vast valley
I can advise you not to mutate the body object into another enhancers
That's the only way I can help.
@vast valley I have an Idea like what if I build a custom middleware to validate the dto's it will work fine, but then the usecase of global pipes will be gone void
I don’t think so
@vast valley help me out with some examples or explanations
Exception filters - Throwing standard exceptions