Skip to content

Commit

Permalink
fix: handle undefined and null values in isInt and isFloat (validator…
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniyal-Qureshi authored Jun 22, 2024
1 parent 2b6b0fa commit acdebd6
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/lib/isFloat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import isNullOrUndefined from './util/nullUndefinedCheck';
import { decimal } from './alpha';

export default function isFloat(str, options) {
Expand All @@ -10,10 +11,10 @@ export default function isFloat(str, options) {
}
const value = parseFloat(str.replace(',', '.'));
return float.test(str) &&
(!options.hasOwnProperty('min') || value >= options.min) &&
(!options.hasOwnProperty('max') || value <= options.max) &&
(!options.hasOwnProperty('lt') || value < options.lt) &&
(!options.hasOwnProperty('gt') || value > options.gt);
(!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || value >= options.min) &&
(!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || value <= options.max) &&
(!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || value < options.lt) &&
(!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || value > options.gt);
}

export const locales = Object.keys(decimal);
9 changes: 5 additions & 4 deletions src/lib/isInt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import isNullOrUndefined from './util/nullUndefinedCheck';

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

// 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);
let minCheckPassed = (!options.hasOwnProperty('min') || isNullOrUndefined(options.min) || str >= options.min);
let maxCheckPassed = (!options.hasOwnProperty('max') || isNullOrUndefined(options.max) || str <= options.max);
let ltCheckPassed = (!options.hasOwnProperty('lt') || isNullOrUndefined(options.lt) || str < options.lt);
let gtCheckPassed = (!options.hasOwnProperty('gt') || isNullOrUndefined(options.gt) || str > options.gt);

return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}
3 changes: 3 additions & 0 deletions src/lib/util/nullUndefinedCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isNullOrUndefined(value) {
return value === null || value === undefined;
}
153 changes: 153 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4211,6 +4211,78 @@ describe('Validators', () => {
'a',
],
});
test({
validator: 'isInt',
args: [{
min: undefined,
max: undefined,
}],
valid: [
'143',
'15',
'767777575',
],
invalid: [
'10.4',
'bar',
'10a',
'c44',
],
});
test({
validator: 'isInt',
args: [{
gt: undefined,
lt: undefined,
}],
valid: [
'289373466',
'55',
'989',
],
invalid: [
'10.4',
'baz',
'66a',
'c21',
],
});
test({
validator: 'isInt',
args: [{
gt: null,
max: null,
}],
valid: [
'1',
'886',
'84512345',
],
invalid: [
'10.4',
'h',
'1.2',
'+',
],
});
test({
validator: 'isInt',
args: [{
lt: null,
min: null,
}],
valid: [
'289373466',
'55',
'989',
],
invalid: [
',',
'+11212+',
'fail',
'111987234i',
],
});
});

it('should validate floats', () => {
Expand Down Expand Up @@ -4440,6 +4512,87 @@ describe('Validators', () => {
'foo',
],
});
test({
validator: 'isFloat',
args: [{
min: undefined,
max: undefined,
}],
valid: [
'123',
'123.',
'123.123',
'-767.767',
'+111.111',
],
invalid: [
'ab565',
'-,123',
'+,123',
'7866.t',
'123,123',
'123,',
],
});
test({
validator: 'isFloat',
args: [{
gt: undefined,
lt: undefined,
}],
valid: [
'14.34343',
'11.1',
'456',
],
invalid: [
'ab565',
'-,123',
'+,123',
'7866.t',
],
});
test({
validator: 'isFloat',
args: [{
locale: 'ar',
gt: null,
max: null,
}],
valid: [
'13324٫',
'12321',
'444٫83874',
],
invalid: [
'55.55.55',
'1;23',
'+-123',
'1111111l1',
'3.3',
],
});
test({
validator: 'isFloat',
args: [{
locale: 'ru-RU',
lt: null,
min: null,
}],
valid: [
'11231554,34343',
'11,1',
'456',
',311',
],
invalid: [
'ab565',
'-.123',
'+.123',
'7866.t',
'22.3',
],
});
});

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

0 comments on commit acdebd6

Please sign in to comment.