Class-validator: Shared Validators

Created on 23 Mar 2020  路  3Comments  路  Source: typestack/class-validator

Is it possible to do shared validators? For example build a DTO with class-validator for array, string, number validators that can be used in other places.

function(@Query('id') id:QueryNumberDto)

import { IsNumber, Max, IsNotEmpty } from 'class-validator';
import { Transform } from 'class-transformer'
export class id:QueryNumberDto {
    @IsNumber()
    @IsNotEmpty()
    @Transform(value => Number(value))
    (dynamic): Number;
}

something like that, that in error i can return a dynamic field

awaiting answer question

Most helpful comment

You can also create a new Decorator for your Validator, that calls the other decorators. In JS/TS decorators are just functions that are called.

All 3 comments

You can create own validators https://github.com/typestack/class-validator#custom-validation-decorators or inherit from another classes https://github.com/typestack/class-validator#inheriting-validation-decorators.

You can also create a new Decorator for your Validator, that calls the other decorators. In JS/TS decorators are just functions that are called.

@pietrobelluno
https://github.com/tannerntannern/ts-mixer#mixing-with-decorators

Was this page helpful?
0 / 5 - 0 ratings