Skip to content

Commit a4dc10e

Browse files
Jasper Bluesvlapo
Jasper Blues
authored andcommittedNov 1, 2019
feat: add support for maxDecimalPlaces on IsNumber (#381)
1 parent 45b7df7 commit a4dc10e

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed
 

‎package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* Options to be passed to IsURL decorator.
2+
* Options to be passed to IsNumber decorator.
33
*/
44
export interface IsNumberOptions {
55
allowNaN?: boolean;
66
allowInfinity?: boolean;
7-
}
7+
maxDecimalPlaces?: number;
8+
}

‎src/validation/ValidationTypes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class ValidationTypes {
150150
case this.IS_DATE:
151151
return eachPrefix + "$property must be a Date instance";
152152
case this.IS_NUMBER:
153-
return eachPrefix + "$property must be a number";
153+
return eachPrefix + "$property must be a number conforming to the specified constraints";
154154
case this.IS_INT:
155155
return eachPrefix + "$property must be an integer number";
156156
case this.IS_STRING:

‎src/validation/Validator.ts

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

438+
if (options.maxDecimalPlaces) {
439+
let decimalPlaces = 0;
440+
if ((value % 1) !== 0) {
441+
decimalPlaces = value.toString().split(".")[1].length;
442+
}
443+
if (decimalPlaces > options.maxDecimalPlaces) {
444+
return false;
445+
}
446+
}
447+
438448
return Number.isFinite(value);
439449
}
440450

‎test/functional/validation-functions-and-decorators.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,11 @@ describe("IsNumber", function() {
568568
someProperty: number;
569569
}
570570

571+
class MaxDecimalPlacesTest {
572+
@IsNumber({ maxDecimalPlaces: 3 })
573+
someProperty: number;
574+
}
575+
571576
it("should fail if NaN passed without allowing NaN values", function (done) {
572577
checkInvalidValues(new MyClass(), [NaN], done);
573578
});
@@ -602,10 +607,18 @@ describe("IsNumber", function() {
602607

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

614+
it("should pass if number of decimal places within maxDecimalPlaces", function(done) {
615+
checkValidValues(new MaxDecimalPlacesTest(), [1.123], done);
616+
});
617+
618+
it("should fail if number of decimal places exceeds maxDecimalPlaces", function(done) {
619+
checkInvalidValues(new MaxDecimalPlacesTest(), [1.1234], done);
620+
});
621+
609622
});
610623

611624
describe("IsInt", function() {

0 commit comments

Comments
 (0)
Please sign in to comment.