Skip to content
Open
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
13 changes: 7 additions & 6 deletions src/lib/isFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { decimal } from './alpha';
export default function isFloat(str, options) {
assertString(str);
options = options || {};
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
const splittingChar = options.locale ? decimal[options.locale] : '.';
const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${splittingChar}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`);
if (str === '' || str === '.' || str === '-' || str === '+') {
return false;
}
const value = parseFloat(str.replace(',', '.'));
const value = parseFloat(str.replace(splittingChar, '.'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another bug fixed. I don't know why it could not be detected before.

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);
value >= (options.min ?? -Infinity) &&
value <= (options.max ?? Infinity) &&
value < (options.lt ?? Infinity) &&
value > (options.gt ?? -Infinity);
Comment on lines +14 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null returns true only for min and max. Otherwise is false. I had to use this kind of identity instead.

}

export const locales = Object.keys(decimal);
14 changes: 8 additions & 6 deletions src/lib/isInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ export default function isInt(str, options) {

// Get the regex to use for testing, based on whether
// leading zeroes are allowed or not.
let regex = (
options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ?
const regex = (
// this strict equality check is required
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);
// When options.[option] is undefined or null, create an always-true check
const minCheckPassed = str >= (options.min ?? -Infinity);
const maxCheckPassed = str <= (options.max ?? Infinity);
const ltCheckPassed = str < (options.lt ?? Infinity);
const gtCheckPassed = str > (options.gt ?? -Infinity);

return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}
77 changes: 77 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3532,6 +3532,29 @@ describe('Validators', () => {
'',
],
});
test({
validator: 'isInt',
args: [{ allow_leading_zeroes: null }],
valid: [
'13',
'123',
'0',
'123',
'-0',
'+1',
'01',
'-01',
'000',
'-000',
'+000',
],
invalid: [
'100e10',
'123.123',
' ',
'',
],
});
test({
validator: 'isInt',
args: [{
Expand Down Expand Up @@ -3589,6 +3612,27 @@ describe('Validators', () => {
'a',
],
});
test({
validator: 'isInt',
args: [{
min: null,
max: undefined,
gt: null,
lt: undefined,
}],
valid: [
'300',
'-123',
'14',
'0',
],
invalid: [
'100e10',
'123.123',
' ',
'',
],
});
});

it('should validate floats', () => {
Expand Down Expand Up @@ -3785,6 +3829,39 @@ describe('Validators', () => {
'-5.5',
],
});
test({
validator: 'isFloat',
args: [{
min: null,
max: undefined,
gt: null,
lt: undefined,
}],
valid: [
'123',
'123.',
'123.123',
'-123.123',
'-0.123',
'+0.123',
'0.123',
'.0',
'-.123',
'+.123',
'01.123',
'-0.22250738585072011e-307',
],
invalid: [
'+',
'-',
' ',
'',
'.',
'foo',
'20.foo',
'2020-01-06T14:31:00.135Z',
],
});
test({
validator: 'isFloat',
args: [{
Expand Down