Skip to content

Commit b8aa922

Browse files
authored
fix: IsNumber validator now works when maxDecimalPlaces=0 (#524)
1 parent faa8a79 commit b8aa922

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Diff for: src/validation/Validator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ export class Validator {
435435
return options.allowNaN;
436436
}
437437

438-
if (options.maxDecimalPlaces) {
438+
if (options.maxDecimalPlaces !== undefined) {
439439
let decimalPlaces = 0;
440440
if ((value % 1) !== 0) {
441441
decimalPlaces = value.toString().split(".")[1].length;

Diff for: test/functional/validation-functions-and-decorators.spec.ts

+24-11
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ describe("IsLatLong", function () {
471471

472472
it("should fail if validator.validate said that its invalid", function (done) {
473473
checkInvalidValues(new MyClass(), invalidValues, done);
474-
});
474+
});
475475

476476
});
477477
describe("IsLatitude", function () {
@@ -573,6 +573,11 @@ describe("IsNumber", function() {
573573
someProperty: number;
574574
}
575575

576+
class ZeroDecimalPlacesTest {
577+
@IsNumber({ maxDecimalPlaces: 0 })
578+
someProperty: number;
579+
}
580+
576581
it("should fail if NaN passed without allowing NaN values", function (done) {
577582
checkInvalidValues(new MyClass(), [NaN], done);
578583
});
@@ -619,6 +624,14 @@ describe("IsNumber", function() {
619624
checkInvalidValues(new MaxDecimalPlacesTest(), [1.1234], done);
620625
});
621626

627+
it("should pass if number of decimal places is zero", function(done) {
628+
checkValidValues(new ZeroDecimalPlacesTest(), [-10, -1, 0, 1, 10], done);
629+
});
630+
631+
it("should fail if number of decimal places is not zero", function(done) {
632+
checkInvalidValues(new ZeroDecimalPlacesTest(), [-11.1, -2.2, -0.1, 0.1, 2.2, 11.1], done);
633+
});
634+
622635
});
623636

624637
describe("IsInt", function() {
@@ -3281,19 +3294,19 @@ describe("isHash", function() {
32813294
it("should not fail if validator.validate said that its valid", function(done) {
32823295
checkValidValues(new MyClass(), validValues, done);
32833296
});
3284-
3297+
32853298
it("should fail if validator.validate said that its invalid", function(done) {
32863299
checkInvalidValues(new MyClass(), invalidValues, done);
32873300
});
3288-
3301+
32893302
it("should not fail if method in validator said that its valid", function() {
32903303
validValues.forEach(value => validator.isHash(value, algorithm).should.be.true);
32913304
});
3292-
3305+
32933306
it("should fail if method in validator said that its invalid", function() {
32943307
invalidValues.forEach(value => validator.isHash(value, algorithm).should.be.false);
32953308
});
3296-
3309+
32973310
it("should return error object with proper data", function(done) {
32983311
const validationType = "isHash";
32993312
const message = `someProperty must be a hash of type ${algorithm}`;
@@ -3354,7 +3367,7 @@ describe("isHash", function() {
33543367
"39485729348",
33553368
"%&FHKJFvk",
33563369
];
3357-
3370+
33583371
testHash(algorithm, validValues, invalidValues);
33593372
});
33603373

@@ -3373,7 +3386,7 @@ describe("isHash", function() {
33733386
"39485729348",
33743387
"%&FHKJFvk",
33753388
];
3376-
3389+
33773390
testHash(algorithm, validValues, invalidValues);
33783391
});
33793392

@@ -3392,7 +3405,7 @@ describe("isHash", function() {
33923405
"39485729348",
33933406
"%&FHKJFvk",
33943407
];
3395-
3408+
33963409
testHash(algorithm, validValues, invalidValues);
33973410
});
33983411

@@ -3411,7 +3424,7 @@ describe("isHash", function() {
34113424
"39485729348",
34123425
"%&FHKJFvk",
34133426
];
3414-
3427+
34153428
testHash(algorithm, validValues, invalidValues);
34163429
});
34173430

@@ -3430,9 +3443,9 @@ describe("isHash", function() {
34303443
"39485729348",
34313444
"%&FHKJFvk",
34323445
];
3433-
3446+
34343447
testHash(algorithm, validValues, invalidValues);
3435-
});
3448+
});
34363449
});
34373450

34383451
describe("IsISSN", function() {

0 commit comments

Comments
 (0)