NestJS and ‘class validator’ cheat sheet

date
Jun 13, 2022
slug
nestjs-and-class-validator-cheat-sheet
status
Published
tags
nest
validations
summary
NestJs along with `class-validator` is a good combination for validating API responses. Both are well documented but some needed use cases are not covered assuming developer to figure out. Below are some few cheat sheets which helps in defining those cases and you can use these to scale to some extent.
type
Post
NestJs along with `class-validator` is a good combination for validating API responses. Both are well documented but some needed use cases are not covered assuming developer to figure out. Below are some few cheat sheets which helps in defining those cases and you can use these to scale to some extent.

1️⃣ Validating String

Any String

// example.dto.ts
import {
  IsString,
  IsNotEmpty,
  MinLength,
  MaxLength
} from 'class-validator'

export class AnyString {
	@IsString()
	@IsNotEmpty()
	@MinLength(3)
  @MaxLength(65)
	userName: string
}
 

Specific String

// example.dto.ts
import {
  ArrayNotEmpty,
  IsArray,
	IsIn
} from 'class-validator'

const weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];

export class SpecificString {
  @IsArray()
  @ArrayNotEmpty()

	@IsIn(weekdays)
  day: string[]
}

Array of strings

// example.dto.ts
import {
  ArrayNotEmpty,
  Contains,
  IsArray,
  IsString,
  Matches,
  MaxLength
} from 'class-validator'

export class StringArray {
  @IsArray()
  @ArrayNotEmpty()

  // check if every array item is string
  @IsString({ each: true }) 
	// Pass `each: true` when you want this validation to run on every array item

  // Check if every array item has max length
  @MaxLength(6, { each: true })

  // Check if every array item is only alphabets
  @Matches('^[a-zA-Z\\s]+$', undefined, { each: true })

  // CHeck if every array item contains a text
  @Contains('hello', { each: true })
  stringArray: string[]
}
 

Array of Strings with a custom prefix

// example.dto.ts
import {
  ArrayNotEmpty,
  IsArray,
  Validate,
  ValidateNested,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator'

@ValidatorConstraint({ name: 'arrayPrefixValidator' })
export class ArrayPrefixValidator implements ValidatorConstraintInterface {
  validate(values: string[] = []): boolean {
    if (values.length) {
      return values.every((value) => value.startsWith('str-'))
    }
    return false
  }
}

export class StringArray {
  @IsArray()
  @ArrayNotEmpty()

  // Check if every array item contains a prefix str-
  @Validate(ArrayPrefixValidator, { message: 'No str- prefix' })
  stringArray: string[]
}
 
 

2️⃣ Validating Number

Any number

// example.dto.ts

import {
  IsNumber,
  IsNotEmpty,
  MinLength,
  MaxLength
} from 'class-validator'

export class AnyString {
	@IsNumber()
	@IsNotEmpty()
	@MinLength(3)
  @MaxLength(65)
	userName: string
}
 

Specific Number

// example.dto.ts

import {
  IsNumber,
  IsNotEmpty,
  MinLength,
  MaxLength,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator'

@ValidatorConstraint({ name: 'isDividedBy17' })
export class IsDividedBy17 implements ValidatorConstraintInterface {
  validate(value: number): boolean {
    if (value {
      return value % 17 === 0
    }
    return false
  }
}

export class SpecificNumber {
  @IsNumber()
	@IsNotEmpty()

  // Check if number is divisible by 17
  @Validate(IsDividedBy17, { message: 'No divided by 17' })
  specificNumber: number
}
 

Array of numbers

// example.dto.ts

import {
  IsNumber,
  IsNotEmpty,
  MinLength,
  MaxLength,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator'

@ValidatorConstraint({ name: 'isEvenNumber' })
export class IsEvenNumber implements ValidatorConstraintInterface {
  validate(numbers: number): boolean {
    if (numbers {
      return numbers.every(number => number % 2 === 0)
    }
    return false
  }
}

export class SpecificNumber {
  @IsNumber()
	@IsNotEmpty()

  // Check if everynumber is even
	@Validate(IsEvenNumber, { message: 'No divided by 17' })
  numbersList: number[]
}
 
 

3️⃣ Custom Object

For example you have a payload like below
 
{
	movies: [{
		name: "The jurassic park",
		yearOfRelease: 1999,
		languages: ["en"]
		gener: ["sci-fi", "thriller"]
	},
	{
		name: "The Croods",
		yearOfRelease: 2012,
		languages: ["en", "fr", "es"]
		gener: ["animation", "kids"]
	}]
}
 
To validate such object
 
// example.dto.ts


import {
  IsNumber,
  IsNotEmpty,
  MinLength,
  MaxLength,
  ValidatorConstraint,
  ValidatorConstraintInterface
} from 'class-validator'

@ValidatorConstraint({ name: 'isEvenNumber' })
export class IsEvenNumber implements ValidatorConstraintInterface {
  validate(numbers: number): boolean {
    if (numbers {
      return numbers.every(number => number % 2 === 0)
    }
    return false
  }
}

const validLanguages = ["en", "es", "fr"] 
const validGeneres = ["sci-fi", "thriller", "animation", "horror", "vintage"]

// 💡 Types here
export class Movie {
  @IsString()
	@IsNotEmpty()
	@ISRequired()
  name: string

  @IsNotEmpty()
  @IsNumber()
	@ISRequired()
  yearOfRelease: number

  @IsNotEmpty()
  @IsString()
	@IsIn(validLanguages)
	@ISRequired()
  languages: string[]

  @IsNotEmpty()
  @IsString()
	@IsIn(validGeneres)
	@ISRequired()
  genre: string[]
}

export class MoviesList {
  @IsArray()
	@ArrayNotEmpty()
	@Type(() => Movie)
  movies: Movie[]
}
 

Older Version 📦

⚠️ Still in beta. Report 🐞's here