Input validation is a check done on your application to ensure that it receives data from users or other sources in a format that it expects to see. Otherwise, your database can quickly fill up with the wrong type of data or even malignant intentioned data, leading to the crashing of the applications, data corruption, and security vulnerability
TypeORM itself does not use any built-in input validation. However, it can easily be used together with external validation libraries-the most popular one is class-validator. That library works pretty well with TypeORM entities, allowing you to declare validation rules using decorators.
Using Class-Validator with TypeORM
Using DTOs (Data Transfer Objects) for Validation in NestJS
1. Class-Validator with TypeORM
class-validator is a pretty strong library that will let you validate JavaScript objects based on decorators. It's very well suited with TypeORM entities.
Steps:
Install the required dependencies:
Annotate your TypeORM entity with validation decorators.
In the following example, we will utilize decorators such as @IsString() and @IsEmail(), to define our validation rules. The rules above ensure the firstName, email, and age data all satisfy some constraints before getting saved into the database.
2. Using DTO in NestJS Application to Validate
NestJS provides a much cleaner approach to input validation using Data Transfer Objects (DTOs) in tandem with class-validator. All the validating logic is kept outside of entities, focused more on validating data which is being sent to APIs.
Example:
Create DTO class:
Use DTO in the controller:
Service Method:
This way, you decouple the validation rules from the entity, which makes your code much more flexible and reusable.
1. Database-Level Validation:
You can also enforce validation at the database level using column constraints. For
example:
But solely depending on database constraints might yield a less informative error message with a slower error detection rate as opposed to application-level validation.
2. Pipes in NestJS:
And if you use NestJS, the built-in validation pipes of the framework automatically validate incoming requests with the help of DTOs that you need to define in your controller, so you will avoid some extra code.
Validating input is one of the basic parts of any application dealing with a database. With TypeORM combined with a class-validator or using DTOs in NestJS, you can easily make your validation logic scalable and maintainable. Such approaches ensure that only valid data gets into your database- and at the same time, always avoids bugs, corrupt data, and security vulnerabilities from arising in the future.
The approach should be chosen concerning the architecture for the application. For less complex validations, the mere use of decorators directly on your entity would be enough, but for much more complex applications, using DTOs with validation pipes in something like NestJS gives a cleaner and more scalable solution.
Ready to transform your business with our technology solutions? Contact us today to Leverage Our NodeJS Expertise.
0