Skip to content

Cell - Validation Component ​

Add @celljs/validation to the dependencies to check request data: a controller or RPC parameter whose type is a class becomes an instance of it (class-transformer) and is validated with its class-validator decorators. Failures answer 400 with the errors as JSON.

ts
import { IsInt, IsString, Min } from 'class-validator';

export class CreateUser {
    @IsString()
    name: string;

    @IsInt()
    @Min(0)
    age: number;
}

@Controller('users')
export class UserController {
    @Post()
    create(@Body() user: CreateUser) { ... }
}

Without this package @celljs/web passes such parameters the plain request data, and still converts String, Number, Boolean and Date parameters (TypeConversionPipe).

yaml
validation:
  detailedOutputDisabled: false   # true: answer 400 without the errors
  transformEnabled: true          # false: hand the method the request data, not the instance
  transformOptions: {}            # options of plainToInstance
  validatorOptions: {}            # options of validate, e.g. whitelist: true