Validate number 18.2 with class-validator

7.5k views Asked by At

I need to validate number 18.2 with class-validator. On current stage I know how to validate only decimal places:

export class PaymentBasicData {
    /**
     * Transaction amount.
     */
    @Expose()
    @IsNotEmpty()
    @IsNumber({ maxDecimalPlaces: 2 })
    @Min(0.01)
    @Max(999999999999999999.99)
    public amount: number;
}

I should validate a number with 18 numbers before dot and 2 numbers after dot. How it should looks like?

1

There are 1 answers

0
Violeta On

For the digits part class-validator has a @IsDecimal validation decorator you can use and it accepts options.

For what I know you cannot validate before coma (or point) digits, the maximum value you already used should work.

import { Expose } from "class-transformer";
import { IsDecimal, IsNoEmpty, Max, Min } from "class-validator";

export class PaymentBasicData {
    /**
     * Transaction amount.
     */
    @Expose()
    @IsNotEmpty()
    @IsDecimal({ decimal_digits: "2" })
    @Min(0.01)
    @Max(999999999999999999.99)
    public amount: number;
}