Repository: validatorjs/validator.js (js)

Issue:
isDivisibleBy string comparison inconsistent
The function is `->isDivisibleBy` will use the first number in a string to do the comparison, even if the string is not a valid number. This is problematic when passing a date string in because `2020-01-01T00:15:38.184Z`  / `2` will pass but `2019-01-01T00:15:38.184Z` will not.

**Actual Results:**
 
```js
const validator = require('validator');
validator.isDivisibleBy('2020-a', 2); // true
validator.isDivisibleBy('2019-a.', 2); // false
validator.isDivisibleBy('2#bar', 2); // true
validator.isDivisibleBy('1#foo', 2); // false
```

**Expected Result:**

```js
const validator = require('validator');
validator.isDivisibleBy('2020-a', 2); // false
validator.isDivisibleBy('2019-a.', 2); // false
validator.isDivisibleBy('2#bar', 2); // false
validator.isDivisibleBy('1#foo', 2); // false
```

Pull request description:
fix(toFloat): verify the string can be safely converted to a float
Verify the string can be safely converted to a float. This is needed because `parseFloat('2020-01-06T14:31:00.135Z')` will return `2020`, even though it should return `NaN`. This fixes an issue checking `isDivisibleBy('2020-01-06T14:31:00.135Z', 2)`.

Resolves #1226 

Patch:
diff --git a/src/lib/toFloat.js b/src/lib/toFloat.js
index eb3eb5a..fa7d7b7 100644
--- a/src/lib/toFloat.js
+++ b/src/lib/toFloat.js
@@ -1,6 +1,7 @@
-import assertString from './util/assertString';
+import isFloat from './isFloat';
 
 export default function toFloat(str) {
-  assertString(str);
+  if (!isFloat(str)) return NaN;
+
   return parseFloat(str);
 }


Test patch:
diff --git a/test/sanitizers.js b/test/sanitizers.js
index 869b34f..c523fe0 100644
--- a/test/sanitizers.js
+++ b/test/sanitizers.js
@@ -137,6 +137,7 @@ describe('Sanitizers', () => {
         '2.': 2.0,
         '-2.5': -2.5,
         '.5': 0.5,
+        '2020-01-06T14:31:00.135Z': NaN,
         foo: NaN,
       },
     });
diff --git a/test/validators.js b/test/validators.js
index 8a9e775..c2f2a2f 100644
--- a/test/validators.js
+++ b/test/validators.js
@@ -2335,6 +2335,8 @@ describe('Validators', () => {
         '',
         '.',
         'foo',
+        '20.foo',
+        '2020-01-06T14:31:00.135Z',
       ],
     });
 
@@ -3187,6 +3189,7 @@ describe('Validators', () => {
         '101',
         'foo',
         '',
+        '2020-01-06T14:31:00.135Z',
       ],
     });
   });