Skip to content

Commit d2b9965

Browse files
authored
Merge pull request #588 from manchicken/master
Added support for `lt` and `gt` in `isInt()`.
2 parents 61104b4 + dd2f65a commit d2b9965

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#### HEAD
2+
- Added support for `lt` and `gt` into `isInt()`
3+
([#588](https://github.com/chriso/validator.js/pull/588))
4+
15
#### 6.1.0
26

37
- Added support for greater or less than in `isFloat()`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Passing anything other than a string is an error.
8282
- **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier).
8383
- **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
8484
- **isIn(str, values)** - check if the string is in a array of allowed values.
85-
- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`).
85+
- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
8686
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
8787
- **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
8888
- **isLowercase(str)** - check if the string is lowercase.

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)