Search code examples
typescriptvalidationclass-validator

Can I compare number variables with 'class-validator'?


Hi! I have a question about class-validator

My client will request multiple numbers and I need to compare values each other.

I want code like below.

const validator: SomeValidator = new SomeValidator;
validator.first = 1000;
validator.second = 2000; // this will be greater than validator.first
validator.third = 3000; // this will be greater than validator.second

And I tried input variable into @Min() validator like below

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

class SomeValidator {

  @IsInt()
  @Min(0)
  public first: number;

  @IsInt()
  @Min(this.first) // it occurs TS7017
  public second: number;

  @IsInt()
  @Min(this.second) // it occurs TS7017
  public third: number;

}

And I got TS7017 Error.

TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

Is there any perfect way to handle this?

I know there is simple way like below, but I wish there is some way to using class-validator.

if ((first > second) || (second > third)) {
  res.status(400).json({
    message: 'validation failed',
  });
  return next(new Error('some error'));

Solution

  • Yes, you can, but not natively, you will need in this case to create your own decorator that implements this validation. Here's an example:

    First create your decorator validator

    // file validators/is-bigger-than.ts
    import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator';
    
    export function IsBiggerThan(property: string, validationOptions?: ValidationOptions) {
      return function (object: Object, propertyName: string) {
        registerDecorator({
          name: 'isBiggerThan',
          target: object.constructor,
          propertyName: propertyName,
          constraints: [property],
          options: validationOptions,
          validator: {
            validate(value: any, args: ValidationArguments) {
              const [relatedPropertyName] = args.constraints;
              const relatedValue = (args.object as any)[relatedPropertyName];
              return typeof value === 'number' && typeof relatedValue === 'number' && value > relatedValue;
            },
          },
        });
      };
    }
    

    Then import it in your dto

    import { IsBiggerThan } from '../../validators/is-bigger-than';
    
    export class SomeObjectDTO {
      minValue: number;
    
    
      @IsBiggerThan('minValue', {
        message: 'maxValue must be larger than minValue',
      })
      maxValue: number;
    }
    

    Tested and works (See Source of Unit Tests): Unit tests