Skip to content

Commit

Permalink
Merge pull request #587 from hkwu/feature/isFloat
Browse files Browse the repository at this point in the history
Add support for greater/less than for isFloat().
  • Loading branch information
chriso authored Oct 6, 2016
2 parents d4691a6 + 1237c55 commit 1b7f4a2
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- New locales
([#585](https://github.com/chriso/validator.js/pull/585))
- Added support for greater or less than in `isFloat()`
([#544](https://github.com/chriso/validator.js/issues/544))

#### 6.0.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Passing anything other than a string is an error.
- **isEmail(str [, options])** - check if the string is an email. `options` is an object which defaults to `{ allow_display_name: false, allow_utf8_local_part: true, require_tld: true }`. If `allow_display_name` is set to true, the validator will also match `Display Name <email-address>`. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched.
- **isEmpty(str)** - check if the string has a length of zero.
- **isFQDN(str [, options])** - check if the string is a fully qualified domain name (e.g. domain.com). `options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`.
- **isFloat(str [, options])** - check if the string is a float. `options` is an object which can contain the keys `min` and/or `max` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`).
- **isFloat(str [, options])** - check if the string is a float. `options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`). `min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.
- **isFullWidth(str)** - check if the string contains any full-width chars.
- **isHalfWidth(str)** - check if the string contains any half-width chars.
- **isHexColor(str)** - check if the string is a hexadecimal color.
Expand Down
2 changes: 1 addition & 1 deletion lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ function isFloat(str, options) {
if (str === '' || str === '.') {
return false;
}
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max);
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max) && (!options.hasOwnProperty('lt') || str < options.lt) && (!options.hasOwnProperty('gt') || str > options.gt);
}
module.exports = exports['default'];
4 changes: 3 additions & 1 deletion src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ export default function isFloat(str, options) {
}
return float.test(str) &&
(!options.hasOwnProperty('min') || str >= options.min) &&
(!options.hasOwnProperty('max') || str <= options.max);
(!options.hasOwnProperty('max') || str <= options.max) &&
(!options.hasOwnProperty('lt') || str < options.lt) &&
(!options.hasOwnProperty('gt') || str > options.gt);
}
40 changes: 40 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,46 @@ describe('Validators', function () {
'5',
],
});
test({
validator: 'isFloat',
args: [{
gt: -5.5,
lt: 10,
}],
valid: [
'9.9',
'1.0',
'0',
'-1',
'7',
'-5.4',
],
invalid: [
'10',
'-5.5',
'a',
'-20.3',
'20e3',
'10.00001',
],
});
test({
validator: 'isFloat',
args: [{
min: -5.5,
max: 10,
gt: -5.5,
lt: 10,
}],
valid: [
'9.99999',
'-5.499999',
],
invalid: [
'10',
'-5.5',
],
});
});

it('should validate hexadecimal strings', function () {
Expand Down
2 changes: 1 addition & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@
if (str === '' || str === '.') {
return false;
}
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max);
return float.test(str) && (!options.hasOwnProperty('min') || str >= options.min) && (!options.hasOwnProperty('max') || str <= options.max) && (!options.hasOwnProperty('lt') || str < options.lt) && (!options.hasOwnProperty('gt') || str > options.gt);
}

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

Large diffs are not rendered by default.

0 comments on commit 1b7f4a2

Please sign in to comment.