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

Added support for lt and gt in isInt(). #588

Merged
merged 4 commits into from
Oct 17, 2016
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### HEAD
- Added support for `lt` and `gt` into `isInt()`
([#588](https://github.com/chriso/validator.js/pull/588))

#### 6.1.0

- Added support for greater or less than in `isFloat()`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Passing anything other than a string is an error.
- **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier).
- **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
- **isIn(str, values)** - check if the string is in a array of allowed values.
- **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 }`).
- **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).
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
- **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.
- **isLowercase(str)** - check if the string is lowercase.
Expand Down
6 changes: 4 additions & 2 deletions lib/isInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ function isInt(str, options) {
// leading zeroes are allowed or not.
var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? int : intLeadingZeroes;

// Check min/max
// Check min/max/lt/gt
var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;

return regex.test(str) && minCheckPassed && maxCheckPassed;
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}
module.exports = exports['default'];
6 changes: 4 additions & 2 deletions src/lib/isInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ export default function isInt(str, options) {
int : intLeadingZeroes
);

// Check min/max
// Check min/max/lt/gt
let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min);
let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max);
let ltCheckPassed = (!options.hasOwnProperty('lt') || str < options.lt);
let gtCheckPassed = (!options.hasOwnProperty('gt') || str > options.gt);

return regex.test(str) && minCheckPassed && maxCheckPassed;
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}
20 changes: 20 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,26 @@ describe('Validators', function () {
'a',
],
});
test({
validator: 'isInt',
args: [{
gt: 10,
lt: 15,
}],
valid: [
'14',
'11',
'13',
],
invalid: [
'10',
'15',
'17',
'3.2',
'33',
'a',
],
});
});

it('should validate floats', function () {
Expand Down
6 changes: 4 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,13 @@
// leading zeroes are allowed or not.
var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? int : intLeadingZeroes;

// Check min/max
// Check min/max/lt/gt
var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;

return regex.test(str) && minCheckPassed && maxCheckPassed;
return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}

var float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/;
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.