Skip to content

Commit acdebd6

Browse files
fix: handle undefined and null values in isInt and isFloat (#2416)
1 parent 2b6b0fa commit acdebd6

File tree

4 files changed

+166
-8
lines changed

4 files changed

+166
-8
lines changed

src/lib/isFloat.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assertString from './util/assertString';
2+
import isNullOrUndefined from './util/nullUndefinedCheck';
23
import { decimal } from './alpha';
34

45
export default function isFloat(str, options) {
@@ -10,10 +11,10 @@ export default function isFloat(str, options) {
1011
}
1112
const value = parseFloat(str.replace(',', '.'));
1213
return float.test(str) &&
13-
(!options.hasOwnProperty('min') || value >= options.min) &&
14-
(!options.hasOwnProperty('max') || value <= options.max) &&
15-
(!options.hasOwnProperty('lt') || value < options.lt) &&
16-
(!options.hasOwnProperty('gt') || value > options.gt);
14+
(!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || value >= options.min) &&
15+
(!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || value <= options.max) &&
16+
(!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || value < options.lt) &&
17+
(!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || value > options.gt);
1718
}
1819

1920
export const locales = Object.keys(decimal);

src/lib/isInt.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assertString from './util/assertString';
2+
import isNullOrUndefined from './util/nullUndefinedCheck';
23

34
const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
45
const intLeadingZeroes = /^[-+]?[0-9]+$/;
@@ -12,10 +13,10 @@ export default function isInt(str, options) {
1213
const regex = options.allow_leading_zeroes === false ? int : intLeadingZeroes;
1314

1415
// Check min/max/lt/gt
15-
let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min);
16-
let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max);
17-
let ltCheckPassed = (!options.hasOwnProperty('lt') || str < options.lt);
18-
let gtCheckPassed = (!options.hasOwnProperty('gt') || str > options.gt);
16+
let minCheckPassed = (!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || str >= options.min);
17+
let maxCheckPassed = (!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || str <= options.max);
18+
let ltCheckPassed = (!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || str < options.lt);
19+
let gtCheckPassed = (!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || str > options.gt);
1920

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

src/lib/util/nullUndefinedCheck.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function isNullOrUndefined(value) {
2+
return value === null || value === undefined;
3+
}

test/validators.test.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,6 +4211,78 @@ describe('Validators', () => {
42114211
'a',
42124212
],
42134213
});
4214+
test({
4215+
validator: 'isInt',
4216+
args: [{
4217+
min: undefined,
4218+
max: undefined,
4219+
}],
4220+
valid: [
4221+
'143',
4222+
'15',
4223+
'767777575',
4224+
],
4225+
invalid: [
4226+
'10.4',
4227+
'bar',
4228+
'10a',
4229+
'c44',
4230+
],
4231+
});
4232+
test({
4233+
validator: 'isInt',
4234+
args: [{
4235+
gt: undefined,
4236+
lt: undefined,
4237+
}],
4238+
valid: [
4239+
'289373466',
4240+
'55',
4241+
'989',
4242+
],
4243+
invalid: [
4244+
'10.4',
4245+
'baz',
4246+
'66a',
4247+
'c21',
4248+
],
4249+
});
4250+
test({
4251+
validator: 'isInt',
4252+
args: [{
4253+
gt: null,
4254+
max: null,
4255+
}],
4256+
valid: [
4257+
'1',
4258+
'886',
4259+
'84512345',
4260+
],
4261+
invalid: [
4262+
'10.4',
4263+
'h',
4264+
'1.2',
4265+
'+',
4266+
],
4267+
});
4268+
test({
4269+
validator: 'isInt',
4270+
args: [{
4271+
lt: null,
4272+
min: null,
4273+
}],
4274+
valid: [
4275+
'289373466',
4276+
'55',
4277+
'989',
4278+
],
4279+
invalid: [
4280+
',',
4281+
'+11212+',
4282+
'fail',
4283+
'111987234i',
4284+
],
4285+
});
42144286
});
42154287

42164288
it('should validate floats', () => {
@@ -4440,6 +4512,87 @@ describe('Validators', () => {
44404512
'foo',
44414513
],
44424514
});
4515+
test({
4516+
validator: 'isFloat',
4517+
args: [{
4518+
min: undefined,
4519+
max: undefined,
4520+
}],
4521+
valid: [
4522+
'123',
4523+
'123.',
4524+
'123.123',
4525+
'-767.767',
4526+
'+111.111',
4527+
],
4528+
invalid: [
4529+
'ab565',
4530+
'-,123',
4531+
'+,123',
4532+
'7866.t',
4533+
'123,123',
4534+
'123,',
4535+
],
4536+
});
4537+
test({
4538+
validator: 'isFloat',
4539+
args: [{
4540+
gt: undefined,
4541+
lt: undefined,
4542+
}],
4543+
valid: [
4544+
'14.34343',
4545+
'11.1',
4546+
'456',
4547+
],
4548+
invalid: [
4549+
'ab565',
4550+
'-,123',
4551+
'+,123',
4552+
'7866.t',
4553+
],
4554+
});
4555+
test({
4556+
validator: 'isFloat',
4557+
args: [{
4558+
locale: 'ar',
4559+
gt: null,
4560+
max: null,
4561+
}],
4562+
valid: [
4563+
'13324٫',
4564+
'12321',
4565+
'444٫83874',
4566+
],
4567+
invalid: [
4568+
'55.55.55',
4569+
'1;23',
4570+
'+-123',
4571+
'1111111l1',
4572+
'3.3',
4573+
],
4574+
});
4575+
test({
4576+
validator: 'isFloat',
4577+
args: [{
4578+
locale: 'ru-RU',
4579+
lt: null,
4580+
min: null,
4581+
}],
4582+
valid: [
4583+
'11231554,34343',
4584+
'11,1',
4585+
'456',
4586+
',311',
4587+
],
4588+
invalid: [
4589+
'ab565',
4590+
'-.123',
4591+
'+.123',
4592+
'7866.t',
4593+
'22.3',
4594+
],
4595+
});
44434596
});
44444597

44454598
it('should validate hexadecimal strings', () => {

0 commit comments

Comments
 (0)