diff --git a/README.md b/README.md index e8b7aedd9..8b5bf6307 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Validator | Description **isDataURI(str)** | check if the string is a [data uri format][Data URI Format]. **isDate(str [, options])** | check if the string is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`.

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject strings different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`.

`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. -**isDivisibleBy(str, number)** | check if the string is a number that is divisible by another. +**isDivisibleBy(str, number)** | check if the string is a number that is divisible by another. The divisor must be a nonzero, finite integer; other divisors return `false`. **isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number]. **isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings or regexp, and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. **isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace: false }`. diff --git a/src/lib/isDivisibleBy.js b/src/lib/isDivisibleBy.js index 2f6c65363..c5b85fe9c 100644 --- a/src/lib/isDivisibleBy.js +++ b/src/lib/isDivisibleBy.js @@ -3,5 +3,6 @@ import toFloat from './toFloat'; export default function isDivisibleBy(str, num) { assertString(str); - return toFloat(str) % parseInt(num, 10) === 0; + const divisor = Number(num); + return Number.isInteger(divisor) && toFloat(str) % divisor === 0; } diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..902212683 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -6366,6 +6366,26 @@ describe('Validators', () => { }); }); + it('should preserve divisors represented using exponent notation', () => { + test({ + validator: 'isDivisibleBy', + args: [1e21], + valid: ['0', '1e21', '2e21'], + invalid: ['1', '2', '1e20'], + }); + }); + + it('should reject zero, fractional and non-finite divisors', () => { + [0, 2.5, -2.5, 0.5, 1e-7, Infinity, -Infinity, NaN].forEach((divisor) => { + test({ + validator: 'isDivisibleBy', + args: [divisor], + valid: [], + invalid: ['0', '1', '-1', '2', '5', '7.5'], + }); + }); + }); + it('should validate luhn numbers', () => { test({ validator: 'isLuhnNumber',