DTO pattern, class-validator , class-transformer , built-in Pipes,
custom Pipes, and global ValidationPipe setup
These notes explain how NestJS receives data from a request, checks
whether that data is valid, and converts it into the format your
application expects.
The examples use a simple API for creating and fetching posts. You can
adapt the same ideas to users, profiles, authentication, or other
modules.
1. Why do we need validation?
Imagine your API has an endpoint that creates a post. It expects a
request body like this:
{ "content" : "Learning NestJS" , "visibility" : "public" }
Enter fullscreen mode
Exit fullscreen mode
But a client might accidentally send:
{ "content" : 12345 , "visibility" : "everyone" }
Enter fullscreen mode
Exit fullscreen mode
Without validation, incorrect data may travel deeper into your
application and cause unexpected behaviour or database errors.
Validation helps us check incoming data at the API boundary.…