#Query param validation against an enum

16 messages · Page 1 of 1 (latest)

devout wolf
#

Hello! I'm not sure how to automatically validate queryparams against an enum to return a 400 if the value is not found in the enum. SpringBoot offers this feature.

fervent ledge
#

If you define a DTO for your query params, and use it like this:
@Params() params: ParamsDTO

You can use @IsEnum form class-validator

#

Otherwise you can create a custom pipe that would check it for you.

devout wolf
#

Aha, good tip, thanks I'll try that :)

devout wolf
#

This is what I've got, but the validator doesn't seem to be doing much

spark lark
#

You have the ValidationPipe bound, right?

devout wolf
#

No I don't have a ValidationPipe. I had one before, but I didn't specifically bind it besides using a decorator, and I was declaring the parameter directly in the getCmaData() params rather than use a InitialDataParamsDto

spark lark
#

If you don't use the ValidationPipe then the class-validator decorators aren't gonna help you

devout wolf
#

Hmm I'll have to look into its usage, thanks :)

fervent ledge
#

I just realized you were talking about query params. In that case you need the @Query decorator

devout wolf
#

Yeah :P I presumed it worked the same

#

Ok nice I just had to add @UsePipes(new ValidationPipe()) at the top of the file:

#

Works, thank you!

devout wolf
#

!close