Skip to content

Commit

Permalink
fix: IsNumber validator now works when maxDecimalPlaces=0 (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtron committed Mar 18, 2020
1 parent faa8a79 commit b8aa922
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/validation/Validator.ts
Expand Up @@ -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;
Expand Down
35 changes: 24 additions & 11 deletions test/functional/validation-functions-and-decorators.spec.ts
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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}`;
Expand Down Expand Up @@ -3354,7 +3367,7 @@ describe("isHash", function() {
"39485729348",
"%&FHKJFvk",
];

testHash(algorithm, validValues, invalidValues);
});

Expand All @@ -3373,7 +3386,7 @@ describe("isHash", function() {
"39485729348",
"%&FHKJFvk",
];

testHash(algorithm, validValues, invalidValues);
});

Expand All @@ -3392,7 +3405,7 @@ describe("isHash", function() {
"39485729348",
"%&FHKJFvk",
];

testHash(algorithm, validValues, invalidValues);
});

Expand All @@ -3411,7 +3424,7 @@ describe("isHash", function() {
"39485729348",
"%&FHKJFvk",
];

testHash(algorithm, validValues, invalidValues);
});

Expand All @@ -3430,9 +3443,9 @@ describe("isHash", function() {
"39485729348",
"%&FHKJFvk",
];

testHash(algorithm, validValues, invalidValues);
});
});
});

describe("IsISSN", function() {
Expand Down

0 comments on commit b8aa922

Please sign in to comment.