From b8aa9228f8ec335a74ee44ace4a53b83ca050ba8 Mon Sep 17 00:00:00 2001 From: TMTron Date: Wed, 18 Mar 2020 10:53:10 +0100 Subject: [PATCH] fix: IsNumber validator now works when maxDecimalPlaces=0 (#524) --- src/validation/Validator.ts | 2 +- ...alidation-functions-and-decorators.spec.ts | 35 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/validation/Validator.ts b/src/validation/Validator.ts index 5b6c68ce87..733051f239 100644 --- a/src/validation/Validator.ts +++ b/src/validation/Validator.ts @@ -435,7 +435,7 @@ export class Validator { return options.allowNaN; } - if (options.maxDecimalPlaces) { + if (options.maxDecimalPlaces !== undefined) { let decimalPlaces = 0; if ((value % 1) !== 0) { decimalPlaces = value.toString().split(".")[1].length; diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 5d76da0667..abc4cace15 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -471,7 +471,7 @@ describe("IsLatLong", function () { it("should fail if validator.validate said that its invalid", function (done) { checkInvalidValues(new MyClass(), invalidValues, done); - }); + }); }); describe("IsLatitude", function () { @@ -573,6 +573,11 @@ describe("IsNumber", function() { someProperty: number; } + class ZeroDecimalPlacesTest { + @IsNumber({ maxDecimalPlaces: 0 }) + someProperty: number; + } + it("should fail if NaN passed without allowing NaN values", function (done) { checkInvalidValues(new MyClass(), [NaN], done); }); @@ -619,6 +624,14 @@ describe("IsNumber", function() { checkInvalidValues(new MaxDecimalPlacesTest(), [1.1234], done); }); + it("should pass if number of decimal places is zero", function(done) { + checkValidValues(new ZeroDecimalPlacesTest(), [-10, -1, 0, 1, 10], done); + }); + + it("should fail if number of decimal places is not zero", function(done) { + checkInvalidValues(new ZeroDecimalPlacesTest(), [-11.1, -2.2, -0.1, 0.1, 2.2, 11.1], done); + }); + }); describe("IsInt", function() { @@ -3281,19 +3294,19 @@ describe("isHash", function() { it("should not fail if validator.validate said that its valid", function(done) { checkValidValues(new MyClass(), validValues, done); }); - + it("should fail if validator.validate said that its invalid", function(done) { checkInvalidValues(new MyClass(), invalidValues, done); }); - + it("should not fail if method in validator said that its valid", function() { validValues.forEach(value => validator.isHash(value, algorithm).should.be.true); }); - + it("should fail if method in validator said that its invalid", function() { invalidValues.forEach(value => validator.isHash(value, algorithm).should.be.false); }); - + it("should return error object with proper data", function(done) { const validationType = "isHash"; const message = `someProperty must be a hash of type ${algorithm}`; @@ -3354,7 +3367,7 @@ describe("isHash", function() { "39485729348", "%&FHKJFvk", ]; - + testHash(algorithm, validValues, invalidValues); }); @@ -3373,7 +3386,7 @@ describe("isHash", function() { "39485729348", "%&FHKJFvk", ]; - + testHash(algorithm, validValues, invalidValues); }); @@ -3392,7 +3405,7 @@ describe("isHash", function() { "39485729348", "%&FHKJFvk", ]; - + testHash(algorithm, validValues, invalidValues); }); @@ -3411,7 +3424,7 @@ describe("isHash", function() { "39485729348", "%&FHKJFvk", ]; - + testHash(algorithm, validValues, invalidValues); }); @@ -3430,9 +3443,9 @@ describe("isHash", function() { "39485729348", "%&FHKJFvk", ]; - + testHash(algorithm, validValues, invalidValues); - }); + }); }); describe("IsISSN", function() {