0.1.3 • Published 2 years ago
@nestray/better-validation-pipe v0.1.3
Better Validation Pipe
This pipe extends Nest's built-in validation pipe and makes it a bit more descriptive.
Introduction
The default validation pipe is great, but error it returns is just an array of errors:
{
"statusCode": 400,
"error": "Bad Request",
"message": ["email must be an email", "phone cannot be empty"]
}
This package changes the message
to be an object with field names as keys:
{
"statusCode": 400,
"error": "Bad Request",
"message": {
"email": ["email must be an email"],
"phone": ["phone cannot be empty"]
}
}
So then, on your frontend, you can show each error next to its relavant field, instead of showing all of them at the end of your form
Installation
On Yarn:
yarn add @nestray/better-validation-pipe
On NPM:
npm install @nestray/better-validation-pipe
Motivation
This behavior is achievable by passing a custom exceptionFactory
to the original pipe, but I found myself writing the same exception factory for each one of my projects, so I made this small package.
Inspiration
This is the same way Laravel returns errors; as an object that contains arrays of errors (strings).