Skip to content
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
fix: handle undefined and null values in isInt and isFloat
  • Loading branch information
Daniyal-Qureshi committed Jun 6, 2024
commit 72c41abd8822b8ecc1a23f962b9931b1f1f73087
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 @@ -15,10 +16,10 @@ export default function isInt(str, options) {
);

// 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 @@ -4155,6 +4155,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 @@ -4384,6 +4456,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