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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With