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.
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;
}
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 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.
$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.