Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regexp for validate in nestjs?

Tags:

nestjs

I want to use regexp in validation in nestjs.

For example:

RegExp

pagePattern    = '[a-z0-9\-]+';

Method

  @Get('/:article')
  getIndex(
   @Param('article')
  ) {

  }

What can I use? ValidationPipe?

like image 909
Vladimir Golub Avatar asked Sep 12 '25 01:09

Vladimir Golub


1 Answers

I would create a DTO class like

class ArticleParamDTO {
  @Matches('[a-z0-9\-]+') // comes from class-validator
  article: string;
}

And then you can use it in the route handler like

@Get(':article')
getIndex(@Param() { article }: ArticleParamDto) {

}

And then as long as you use the ValidationPipe it will all work. Anything that doesn't match will cause a 400 BadRequest

like image 183
Jay McDoniel Avatar answered Sep 15 '25 11:09

Jay McDoniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!