1

I have an endpoint with no entry params:

async myendpoint(): Promise<any> {
   const customer = await this.customerService.findOne(1);
   if (customer) {
      return await this.customerService.mapToDestination(customer);
   }...
}

Then I have my method mapToDestination where I simply assign vars:

async mapToDestination(customer: Customer): Promise<DestinationDto> {
    const destination: DestinationDto = {
          lastname: customer.lastname,
          firstname: customer.firstname,...

Finally, I have my DTO:

import {IsEmail, IsNotEmpty, IsOptional, IsNumber, IsBoolean, IsString, IsDate, MaxLength, Length, NotEquals} from 'class-validator';
import {ApiProperty} from '@nestjs/swagger';

export class DestinationDto {


  @IsString()
  @IsNotEmpty()
  @MaxLength(32)
  lastname: string;

  @IsString()
  @IsNotEmpty()
  @MaxLength(20)
  firstname: string; ...

I would like my DTO fields to be validated automatically following the decorators when I'm mapping it in my mapToDestination() method. I look through the web and the official documentation and I gave a try to Validators (ValidationPipe) but it does not seem to be my need as it validates the endpoint entry params.

Please, could you explain to me how to achieve this automatic validation? Thanks in advance.

Pauloscorps
  • 564
  • 1
  • 6
  • 16

1 Answers1

2

I won't be "automatic" but you could instantiate your own instance of the validator from class validator and use it against the DTO in your service. Otherwise, it won't ever happen automatically because as you said, the ValidationPipe only works on the entry of the endpoint.

Example

Inside of mapToDestination so long as customer is an instance of DestinationDTO` you can have something like this:

@Injectable()
export class CustomerService {

  async mapToDestination(customer: DestinationDTO) {
    const errors = await validate(customer);
    if (errors) {
      throw new BadRequestException('Some Error Message');
    }
    ...
  }
  
  ...
}
Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • Thank's for your answer. Could you please provide an example of what you're describing as I'm a total newbie to nest / js frameworks? – Pauloscorps Jul 15 '20 at 17:11
  • This doesn't really pertain to the NestJS framework, it's something you'd do naturally in TS, but I'll add a small example – Jay McDoniel Jul 15 '20 at 17:14
  • I should have add TS to the list of my newbie skills. Symfony / PHP developer here and new to back-end oriented JS. – Pauloscorps Jul 15 '20 at 17:15
  • Thanks a lot for your time, I just tried it but I have no errors whereas some of the DTO fields values are null and they've the decorator @IsNotEmpty. I want to validate the destination, not the customer, I slightly edited my post to be more descriptive. Note that I tried your code after my DTO fields assignation `const destination: DestinationDto = {}...` – Pauloscorps Jul 15 '20 at 17:36
  • Sorry about that. Used the wrong script. [Generally, you can find more information here](https://github.com/typestack/class-validator#usage) on class-valdiator – Jay McDoniel Jul 15 '20 at 17:42
  • This is perfect, thanks a lot, you made my day! I just needed to replace `const destination: DestinationDto` by `const destination = new DestinationDto()` – Pauloscorps Jul 15 '20 at 19:45