Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for maxDecimalPlaces on IsNumber #381

Merged
merged 3 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -136,7 +136,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 @@ -377,6 +377,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 @@ -479,6 +479,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 @@ -513,10 +518,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