Skip to content

Commit d4715dd

Browse files
author
Michael D. Stemle, Jr
committed
Added support for lt and gt in isInt().
1 parent 1b7f4a2 commit d4715dd

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
([#585](https://github.com/chriso/validator.js/pull/585))
55
- Added support for greater or less than in `isFloat()`
66
([#544](https://github.com/chriso/validator.js/issues/544))
7+
- Added support for `lt` and `gt` into `isInt()`.
78

89
#### 6.0.0
910

lib/isInt.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ function isInt(str, options) {
2222
// leading zeroes are allowed or not.
2323
var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? int : intLeadingZeroes;
2424

25-
// Check min/max
25+
// Check min/max/lt/gt
2626
var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
2727
var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
28+
var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
29+
var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;
2830

29-
return regex.test(str) && minCheckPassed && maxCheckPassed;
31+
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
3032
}
3133
module.exports = exports['default'];

src/lib/isInt.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ export default function isInt(str, options) {
1414
int : intLeadingZeroes
1515
);
1616

17-
// Check min/max
17+
// Check min/max/lt/gt
1818
let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min);
1919
let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max);
20+
let ltCheckPassed = (!options.hasOwnProperty('lt') || str < options.lt);
21+
let gtCheckPassed = (!options.hasOwnProperty('gt') || str > options.gt);
2022

21-
return regex.test(str) && minCheckPassed && maxCheckPassed;
23+
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
2224
}

test/validators.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,26 @@ describe('Validators', function () {
11831183
'a',
11841184
],
11851185
});
1186+
test({
1187+
validator: 'isInt',
1188+
args: [{
1189+
gt: 10,
1190+
lt: 15,
1191+
}],
1192+
valid: [
1193+
'14',
1194+
'11',
1195+
'13',
1196+
],
1197+
invalid: [
1198+
'10',
1199+
'15',
1200+
'17',
1201+
'3.2',
1202+
'33',
1203+
'a',
1204+
],
1205+
});
11861206
});
11871207

11881208
it('should validate floats', function () {

validator.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,13 @@
681681
// leading zeroes are allowed or not.
682682
var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? int : intLeadingZeroes;
683683

684-
// Check min/max
684+
// Check min/max/lt/gt
685685
var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
686686
var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
687+
var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
688+
var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;
687689

688-
return regex.test(str) && minCheckPassed && maxCheckPassed;
690+
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
689691
}
690692

691693
var float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/;

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)