J. Doe
J. Doe

Reputation: 582

Nest.js. Where transform (serialize) client's data before it will be use at service

Where should I transform income data from client to use transformed values in service.ts file.

As example, I get the JSON-data from client, which looks like this

{
 phone: 8-999-999-99-99
}

I have a register-client.dto file:

import { IsNotEmpty } from 'class-validator';

export class RegisterClientDTO {
  @IsNotEmpty()
  readonly phone: string;
}

I have a client.controller.ts file:

// imports here...

@Controller('clients')
export class ClientController {
  constructor(private readonly clientService: ClientService) {}

  @Post()
  @UsePipes(new ValidationPipe())
  create(@Body() registerClientDTO: RegisterClientDTO) {
    return this.clientService.create(registerClientDTO);
  }
}

I have a client.service.ts file:

// imports here 
@Injectable()
export class ClientService {
  constructor(
    @InjectRepository(ClientEntity)
    private readonly clientRepository: Repository<ClientEntity>,
  ) {}
  async create(clientDTO: RegisterClientDTO): Promise<any> {
    const { phone } = clientDTO;
    console.log(phone); // phone is 8-999-999-99-99, but i want 89999999999

  }
}

I can do smth like this inside create method in service file: const clearPhone = phone.replace(/[^0-9]/gim, '').

But I want the phone field transformed and validated automatically. Maybe I can do this inside dTO file using @Transform decorator from class-transform package?

Upvotes: 1

Views: 3165

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70181

You can probably achieve this with the @Transform() like so:

import { Transform } from 'class-transformer';
import { IsNotEmpty } from 'class-validator';

export class RegisterClientDTO {
  @IsNotEmpty()
  @Transform(value => value.replace(/[^0-9]/gim, ''))
  readonly phone: string;
}

And make sure to set transform: true in the validation pipe options.

The other option is creating a custom pipe for phone numbers that does this, just make sure if you go with that route you only use the pipe on objects with phone numbers.

Upvotes: 1

Related Questions