nestJS class-validator: property is string: check the value is between 100 to 5000
i need to validate dto property that validate string number between 100 and 5000
You can use the following custom validator:
@ValidatorConstraint({ name: 'customText', async: false })
export class CustomTextLength implements ValidatorConstraintInterface {
validate(text: string, args: ValidationArguments) {
const asNum = Number(text);
if ( Number.isNaN(asNum) ) return false;
return asNum > 100 && asNum < 5000;
}
defaultMessage(args: ValidationArguments) {
return 'string number ($value) is too big or too small';
}
}
Nevertheless you should probably just use a transform pipe and then validate the actual result of this transformation to a number.