Skip to content

Validation - Definition

Eicrud's validation happens at the controller level (between the client, and your services).

Entities and commands DTOs are validated the same way.

export class CmdDto {
    @IsString()
    @IsOptional()
    arg: string;
}

You can use any class-validator decorator in your DTOs.

Note

Fields that aren't annotated with a class-validator decorator won't be allowed in the DTO. You can use the @Allow decorator to bypass that rule, but we recommend using proper validation instead.

Nested Validation

class-validator's @ValidateNested will only work when decorator @$Type is applied.

import { $Type } from '@eicrud/core/validation'

export class CmdDto {
    @$Type(Slice)
    @ValidateNested()
    firstSlice: Slice;
}
export class Slice {
    @IsInt()
    @IsOptional()
    size?: number = 1;
    @IsString()
    name: string;
}

Eicrud decorators

Eicrud offers custom decorators you can use.

$MaxSize(size: number, addPerTrustPoint?: number)

Specify the max length of the stringified argument.

import { $MaxSize } from '@eicrud/core/validation'

export class CmdDto {
    @$MaxSize(300)
    bio: string;
}

Warning

By default, every DTO field has a max stringified size of 50 (specified in ValidationOptions->defaultMaxSize). This means you need to decorate fields with @$MaxSize(x) to bypass this limit. Using class-validator's @MaxLength won't affect that limit. Setting defaultMaxSize to 0 disables that check.

$MaxArLength(length: number, addPerTrustPoint?: number)

Specify the max length of an array argument.

import { $MaxArLength } from '@eicrud/core/validation'

export class CmdDto {
    @$Type(Seed)
    @$MaxArLength(5)
    seeds: Seed[];
}

Note

@$MaxArLength must be used with @$Type decorator, or else @$MaxSize will be applied.

Warning

By default, @$Type DTO fields have a max length of 20 (specified in ValidationOptions->defaultMaxArLength). This means you need to decorate fields with @$MaxArLength(x) to bypass this limit.

Validation Pipe

You can use CrudValidationPipe to apply Eicrud's validation and transforms to your own NestJS controllers.

import { CrudValidationPipe } from '@eicrud/core/validation';

@Get('hello')
async get(@Query(new CrudValidationPipe()) query: MyQuery) {
    return `Hello ${query.name}`;
}

export class MyQuery {
    @IsOptional()
    @IsString()
    @$Transform((value) => value.toUpperCase())
    name: string;
}

Comments