Skip to content

Validating Arrays

Basic Array Validation

If your field is an array and you want to perform validation of each item in the array, you must specify a special each: true decorator option:

import { MaxLength } from 'class-validator';

export class Post {
    @MaxLength(20, {
        each: true,
    })
    tags: string[];
}

This will validate each item in post.tags array. The same each: true option also works for Set and Map fields.

Array-specific Decorators

There are also several decorators specifically for array validation:

Decorator Description
@ArrayContains(values: any[]) Checks if array contains all values from the given array of values.
@ArrayNotContains(values: any[]) Checks if array does not contain any of the given values.
@ArrayNotEmpty() Checks if given array is not empty.
@ArrayMinSize(min: number) Checks if array's length is greater than or equal to the specified number.
@ArrayMaxSize(max: number) Checks if array's length is less or equal to the specified number.
@ArrayUnique() Checks if all array's values are unique.

Example:

import { ArrayMinSize, ArrayMaxSize, ArrayUnique } from 'class-validator';

export class Post {
    @ArrayMinSize(1)
    @ArrayMaxSize(10)
    @ArrayUnique()
    tags: string[];
}

Need documentation like this for your own product?

This site was built by Sonicar Tech LLC — we help SaaS, B2B, Enterprise, FinTech, and AI companies launch professional, docs-as-code documentation 60% faster and cheaper than building an in-house team, with first drafts delivered in 1 week.

Visit sonicar.tech