skip to Main Content

What’s the right way to validate and transform parameters in Redis/TCP based NestJS microservice. I understand we can use pipelines and validators for API based microservices on HTTP. Should I be contended with manual validations? Are there examples for the same?

2

Answers


  1. Chosen as BEST ANSWER

    Here's the code that did the trick. Thanks to Jay McDoniel

    import { ArgumentsHost, Catch, ExceptionFilter, Logger } from "@nestjs/common";
    import { RpcException } from '@nestjs/microservices';
    
    @Catch(RpcException)
    export class AWSImageOCRRequestFilter implements ExceptionFilter {
      private logger = new Logger('AWSImageOCRRequestFilter');
    
      catch(exception: RpcException, host: ArgumentsHost) {
        this.logger.log(exception.name, exception.message);
        return new RpcException(exception.getError());
      }
    }
    

    Above filter goes into controller for it to kick-in when invoked

    @UseFilters(new AWSImageOCRRequestFilter())
      @MessagePattern('awsimageocr')
      async textractForImage(
        @Payload(new ValidationPipe({ whitelist: true }))
        scorecardReq: ScorecardRequest,
      )
    

    To support this filter, I have made my DTO validation like the code below

    export class ImageOCRRequest {
      @IsString()
      @IsNotEmpty()
      project: string;
      @IsString()
      @IsNotEmpty()
      file: string;
      @IsString()
      @IsNotEmpty()
      filename: string;
    }
    

    If you miss any of the required parameters, you'll see an exception like below

    [Nest] 26692   - 03/23/2021, 5:11:20 PM   [RpcExceptionsHandler] Bad Request Exception +453863ms
    Error: Bad Request Exception
        at ValidationPipe.exceptionFactory (/Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/common/pipes/validation.pipe.js:89:20)
        at ValidationPipe.transform (/Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/common/pipes/validation.pipe.js:65:30)
        at processTicksAndRejections (node:internal/process/task_queues:94:5)
        at async resolveParamValue (/Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/microservices/context/rpc-context-creator.js:106:31)
        at async Promise.all (index 0)
        at async pipesFn (/Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/microservices/context/rpc-context-creator.js:108:13)
        at async /Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/microservices/context/rpc-context-creator.js:43:17
        at async /Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/microservices/context/rpc-proxy.js:11:32
        at async ServerRedis.handleEvent (/Users/XXXXXXX/Projects/Grange/Nest/nestimageservice/node_modules/@nestjs/microservices/server/server.js:63:32)
    

  2. Pipes still work in microservices. You can create DTOs as you usually do for HTTP and bind the ValidatioinPipe in the same way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search