Validation
Any application that requires input from client, runs a validation before performing any action on it. We love how NestJS provides Validation Pipe and makes it so simple to apply validation.
In this boilerplate, there are few extra added validators, which you can use as per your requirement.
BaseValidator
This is just an alternative solution for ValidationPipe
, if you wish to continue using DTOs
and ValidationPipe
, please feel free to do so.
All available decorators will still work for you. 😀
This boilerplate includes a BaseValidator
class, which can be used to run validators inside the Services/Providers.
Usage:
import { Injectable } from '@nestjs/common';
import { BaseValidator } from '@app/support/validator';
import { CreateUser } from '../validator';
@Injectable()
export class UserService {
constructor(private validator: BaseValidator) {}
async create(
inputs: Record<string, any>,
): Promise<Record<string, any>> {
await this.validator.fire(inputs, CreateUser);
// add your logic here...
}
}
CreateUser
is just our DTO that you will need to create to define the schema of incoming payload. To learn, how you can create DTOs, click here.
Available Decorators
Exists
Checks if property is available in the db on the specified table
and column
.
Options
Option | Description | Default |
---|---|---|
table | Name of the table | undefined |
column | Column in the table | undefined |
caseInsensitive | Will run case insensitive query, if true | false |
where | extra where clause, will be added in the query, passed as Object | undefined |
import { Exists } from '@app/core/validator';
export class UpdateUser {
@Exists({ table: 'users', column: 'contactNumber' })
contactNumber: number;
}
IsUnique
Checks if property is unique or not in the db on the specified table
and column
.
Options
Option | Description | Default |
---|---|---|
table | Name of the table | undefined |
column | Column in the table | undefined |
caseInsensitive | Will run case insensitive query, if true | false |
where | extra where clause, will be added in the query, passed as Object | undefined |
import { IsUnique } from '@app/core/validator';
export class CreateUser {
@IsUnique({ table: 'users', column: 'contactNumber' })
contactNumber: number;
}
IsEqualToProp
Compares the value of the property to another property in the same class.
import { IsString, IsEqualToProp } from '@app/core/validator';
export class ForgotPassword {
@IsString()
password: string;
@IsEqualToProp('password') // name of the property to be compared with
confirmPasssword: string;
}
IsValueFromConfig
It is very common to store global configuration values in the Config
module and there can be use-case where you may want to compare some property with some property stored in config.
import { IsValueFromConfig } from '@app/core/validator';
export class UpdateDeliveryStatus {
@IsValueFromConfig({ key: 'settings.delivery.status' })
status: number;
}
IsDateInFormatConstraint
Use this decorator if you want to validate the format of the date/timestamp.
Uses moment
package internally.
import { IsDateInFormat } from '@app/support/validator';
export class UpdateProfile {
@IsDateInFormat('DD-MM-YYYY')
dob: string;
}