Member-only story

Nest.js and the Custom Validation Pipe

Better validations

Sulaiman Olaosebikan
Better Programming
5 min readNov 18, 2020

--

Photo from Morioh.

Nest.js is a progressive Node.js framework for building server-side applications. Under the hood, it uses the Express framework and can also be configured to use Fastify. It is built with and fully supports TypeScript (one of the reasons why I love this framework).

Like Angular, Nest provides us with pipes. Pipes are basically classes or functions that can take input data, transform it, and output the transformed data.

Pipes in Nest are annotated with the @injectable() decorator. Typically, in all applications, pipes can be used for the purposes of transformation and validation. In this article, I will be writing about the validation use case of a pipe.

Say we are building an application that exposes a POST endpoint to create a new item on a shopping list and you need the user to provide the following as payload to the endpoint:

  • Name (required)
  • Description (required)
  • Quantity (optional)
  • Unit price (required)

Firstly, we need to create a controller that handles the request and we need to specify that the controller should expect a body object as data from the request. The data is typed as any (for now):

--

--

Sulaiman Olaosebikan
Sulaiman Olaosebikan

Written by Sulaiman Olaosebikan

A full-stack software developer. Follow me on twitter https://twitter.com/horlabyc

Responses (2)

What are your thoughts?