#Best possible way to handle validation

1 messages · Page 1 of 1 (latest)

dark jasperBOT
#

nestjs Exception filters - Throwing standard exceptions

vast valley
#

You need to create exception filter for necord context and catch validation error

true jacinth
#

@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

vast valley
vast valley
true jacinth
#

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.

dark jasperBOT
#

necord Techniques - Validation

true jacinth
vast valley
true jacinth
#

@vast valley yes

vast valley
#

What dto you have?

true jacinth
#

I am having a value field in it

vast valley
#

Do you use ValidationPipe from nestjs?

true jacinth
#

yes i have used

vast valley
true jacinth
#

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());

true jacinth
vast valley
#

TagDto without any fields?

#

So, ok, how do you use it in controller?

#

I mean declaration

true jacinth
#

@Post()
async tag(@Body() tagDto: TagDto){}

vast valley
#

I guess it’s wrong channel for it question, post it to #1025199348096700476

#

It’s necord-related (lib for discord bots) channel

true jacinth
#

I have posted it but very less response, Thank you for your time @vast valley

vast valley
#

That's the only way I can help.

true jacinth
#

@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

true jacinth
#

@vast valley help me out with some examples or explanations