TypeORM Validation and Custom Validator Usage


TypeORM Validation and Custom Validator Usage

What is TypeORM Validation and Why is it Important?

TypeORM is a powerful ORM (Object-Relational Mapping) library widely used especially in projects developed with Node.js and TypeScript. In addition to facilitating database operations, it offers great convenience in data validation operations with its entity-based structure. TypeORM validation is extremely important to ensure the accuracy, consistency, and security of data to be saved to the database. In this way, we can store reliable and error-free data both on the client side and the server side.

How is Validation Done with TypeORM?

TypeORM validation operations are generally performed together with the class-validator library. This library allows you to perform field-based validation on your entity classes using various decorators. For example, in a user entity, email and password validation can be performed. Below you can see a basic validation example with TypeORM and class-validator:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { IsEmail, Length } from "class-validator";

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column()
  @IsEmail()
  email!: string;

  @Column()
  @Length(8, 20)
  password!: string;
}

In this structure, with @IsEmail(), it is ensured that the email field has a correct format, and with @Length(8, 20), that the password is at least 8 and at most 20 characters long. These controls performed with TypeORM validation provide great advantages in terms of error management and user experience.

Create Your Own Rules with Custom Validator

Together with TypeORM validation, you can define a custom validator and develop your own special validation rules. Especially in complex business rules or different field relations, ready validation functions may not be sufficient. For this, the class-validator library offers support for ValidatorConstraint and Validate decorators. Here is an example usage of a custom validator:

import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments, Validate } from "class-validator";

@ValidatorConstraint({ async: false })
class IsUsernameValidConstraint implements ValidatorConstraintInterface {
  validate(username: string, args: ValidationArguments) {
    // Should consist of only letters and numbers, 4-15 characters
    return /^[a-zA-Z0-9]{4,15}$/.test(username);
  }
  defaultMessage(args: ValidationArguments) {
    return "Username should only consist of letters and numbers and must be 4-15 characters long.";
  }
}

@Entity()
export class Member {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column()
  @Validate(IsUsernameValidConstraint)
  username!: string;
}

In this example, we created a custom validator called IsUsernameValidConstraint and added a custom rule to the username field. In this way, TypeORM validation easily meets not only the standard rules but also completely project-specific requirements.

Conclusion: TypeORM Validation and Custom Validator Functionality

TypeORM validation and custom validator solutions are indispensable for ensuring data integrity in modern Node.js and TypeScript applications. Thanks to standard and customizable validations, both code readability and security are kept at the highest level. Especially in large projects, you can develop reusable custom validators and easily integrate them throughout the application. Keep control of your data with TypeORM validation and make a difference in your projects!