NestJS REST API: class-validator & class-transformer

Panos Zafeiropoulos
6 min readMar 14, 2022

Summary

Nest.Js is an awesome Node.Js framework to work with for your backend REST API. If you have already used Angular before, you will get familiarized with it, almost immediately.
It is supposed that you are familiar with the NestJS basic building blocks like Modules, Providers, Controllers, Routers, etc.
In this short post, we will try to focus on how to start implementing the class-validator and class-transformer libraries. Both of them are widely used tools for validating REST API Requests’ data and/or transforming your API Responses before sending them back to the client.

Short Intro to Pipes and Interceptors

Before starting implementing the class-validator and class-transformer libraries, it is necessary to have obtained some grasp of how Pipes and Interceptors can be used with NestJS. The NetJS Pipes and Interceptors Documentation is the best source of truth about the subject. However, below we provide a very short intro implementing Pipes and Interceptors.

Pipes

  • Pipes are classes annotated with the @Injectable() decorator (you can think that they are similar to Providers).
  • Pipes implement the PipeTransform generic interface. Therefore…

--

--

Panos Zafeiropoulos

In my sixties and I am getting more and more passionate about programming! I always stay ready for new development challenges!