Skip to content

Commit

Permalink
feat: add support for maxDecimalPlaces on IsNumber (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Blues authored and vlapo committed Nov 1, 2019
1 parent 45b7df7 commit a4dc10e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/validation/ValidationTypeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* Options to be passed to IsURL decorator.
* Options to be passed to IsNumber decorator.
*/
export interface IsNumberOptions {
allowNaN?: boolean;
allowInfinity?: boolean;
}
maxDecimalPlaces?: number;
}
2 changes: 1 addition & 1 deletion src/validation/ValidationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class ValidationTypes {
case this.IS_DATE:
return eachPrefix + "$property must be a Date instance";
case this.IS_NUMBER:
return eachPrefix + "$property must be a number";
return eachPrefix + "$property must be a number conforming to the specified constraints";
case this.IS_INT:
return eachPrefix + "$property must be an integer number";
case this.IS_STRING:
Expand Down
10 changes: 10 additions & 0 deletions src/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@ export class Validator {
return options.allowNaN;
}

if (options.maxDecimalPlaces) {
let decimalPlaces = 0;
if ((value % 1) !== 0) {
decimalPlaces = value.toString().split(".")[1].length;
}
if (decimalPlaces > options.maxDecimalPlaces) {
return false;
}
}

return Number.isFinite(value);
}

Expand Down
15 changes: 14 additions & 1 deletion test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ describe("IsNumber", function() {
someProperty: number;
}

class MaxDecimalPlacesTest {
@IsNumber({ maxDecimalPlaces: 3 })
someProperty: number;
}

it("should fail if NaN passed without allowing NaN values", function (done) {
checkInvalidValues(new MyClass(), [NaN], done);
});
Expand Down Expand Up @@ -602,10 +607,18 @@ describe("IsNumber", function() {

it("should return error object with proper data", function(done) {
const validationType = "isNumber";
const message = "someProperty must be a number";
const message = "someProperty must be a number conforming to the specified constraints";
checkReturnedError(new MyClass(), invalidValues, validationType, message, done);
});

it("should pass if number of decimal places within maxDecimalPlaces", function(done) {
checkValidValues(new MaxDecimalPlacesTest(), [1.123], done);
});

it("should fail if number of decimal places exceeds maxDecimalPlaces", function(done) {
checkInvalidValues(new MaxDecimalPlacesTest(), [1.1234], done);
});

});

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

0 comments on commit a4dc10e

Please sign in to comment.