Skip to content

Commit

Permalink
Squashing the many disparit commits into a single commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Guimaraes committed Jun 7, 2018
1 parent 0a9b52d commit 2e6cc37
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -2,4 +2,4 @@
node_modules
coverage
package-lock.json
yarn.lock
yarn.lock
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
#### HEAD

- Accept an array of locales in `isMobilePhone()`
([#742](https://github.com/chriso/validator.js/pull/742))

#### 10.3.0

- Strict Gmail validation in `isEmail()`
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2016 Chris O'Hara <cohara87@gmail.com>
Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -85,6 +85,7 @@ Validator | Description
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
**isIPRange(str)** | check if the string is an IP Range(version 4 only).
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).<br/><br/>`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
Expand All @@ -102,7 +103,7 @@ Validator | Description
**isMACAddress(str)** | check if the string is a MAC address.
**isMD5(str)** | check if the string is a MD5 hash.
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
**isMobilePhone(str, locale [, options])** | check if the string is a mobile phone number,<br/><br/>(locale is one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMobilePhone(str, locale [, options])** | check if the string is a mobile phone number,<br/><br/>(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
**isNumeric(str)** | check if the string contains only numbers.
Expand Down Expand Up @@ -174,7 +175,7 @@ Remember, validating can be troublesome sometimes. See [A list of articles about
## License (MIT)

```
Copyright (c) 2017 Chris O'Hara <cohara87@gmail.com>
Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -48,6 +48,10 @@ var _isIP = require('./lib/isIP');

var _isIP2 = _interopRequireDefault(_isIP);

var _isIPRange = require('./lib/isIPRange');

var _isIPRange2 = _interopRequireDefault(_isIPRange);

var _isFQDN = require('./lib/isFQDN');

var _isFQDN2 = _interopRequireDefault(_isFQDN);
Expand Down Expand Up @@ -297,6 +301,7 @@ var validator = {
isURL: _isURL2.default,
isMACAddress: _isMACAddress2.default,
isIP: _isIP2.default,
isIPRange: _isIPRange2.default,
isFQDN: _isFQDN2.default,
isBoolean: _isBoolean2.default,
isAlpha: _isAlpha2.default,
Expand Down
47 changes: 47 additions & 0 deletions lib/isIPRange.js
@@ -0,0 +1,47 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

exports.default = isIPRange;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

var _isIP = require('./isIP');

var _isIP2 = _interopRequireDefault(_isIP);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var subnetMaybe = /^\d{1,2}$/;

function isIPRange(str) {
(0, _assertString2.default)(str);

var _str$split = str.split('/'),
_str$split2 = _slicedToArray(_str$split, 3),
ip = _str$split2[0],
subnet = _str$split2[1],
err = _str$split2[2];

if (typeof err !== 'undefined') {
return false;
}

if (!subnetMaybe.test(subnet)) {
return false;
}

// Disallow preceding 0 i.e. 01, 02, ...
if (subnet.length > 1 && subnet.startsWith('0')) {
return false;
}

return (0, _isIP2.default)(ip) && subnet <= 32 && subnet >= 0;
}
module.exports = exports['default'];
12 changes: 11 additions & 1 deletion lib/isMobilePhone.js
Expand Up @@ -87,7 +87,17 @@ function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some(function (key) {
if (phones.hasOwnProperty(key)) {
var phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (var key in phones) {
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -10,6 +10,7 @@ import isEmail from './lib/isEmail';
import isURL from './lib/isURL';
import isMACAddress from './lib/isMACAddress';
import isIP from './lib/isIP';
import isIPRange from './lib/isIPRange';
import isFQDN from './lib/isFQDN';

import isBoolean from './lib/isBoolean';
Expand Down Expand Up @@ -106,6 +107,7 @@ const validator = {
isURL,
isMACAddress,
isIP,
isIPRange,
isFQDN,
isBoolean,
isAlpha,
Expand Down
24 changes: 24 additions & 0 deletions src/lib/isIPRange.js
@@ -0,0 +1,24 @@
import assertString from './util/assertString';
import isIP from './isIP';

const subnetMaybe = /^\d{1,2}$/;

export default function isIPRange(str) {
assertString(str);
const [ip, subnet, err] = str.split('/');

if (typeof err !== 'undefined') {
return false;
}

if (!subnetMaybe.test(subnet)) {
return false;
}

// Disallow preceding 0 i.e. 01, 02, ...
if (subnet.length > 1 && subnet.startsWith('0')) {
return false;
}

return isIP(ip, 4) && subnet <= 32 && subnet >= 0;
}
12 changes: 11 additions & 1 deletion src/lib/isMobilePhone.js
Expand Up @@ -76,7 +76,17 @@ export default function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some((key) => {
if (phones.hasOwnProperty(key)) {
const phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (const key in phones) {
Expand Down
66 changes: 57 additions & 9 deletions test/validators.js
Expand Up @@ -654,6 +654,29 @@ describe('Validators', () => {
});
});

it('should validate isIPRange', () => {
test({
validator: 'isIPRange',
valid: [
'127.0.0.1/24',
'0.0.0.0/0',
'255.255.255.0/32',
],
invalid: [
'127.200.230.1/35',
'127.200.230.1/-1',
'1.1.1.1/011',
'::1/64',
'1.1.1/24.1',
'1.1.1.1/01',
'1.1.1.1/1.1',
'1.1.1.1/1.',
'1.1.1.1/1/1',
'1.1.1.1',
],
});
});

it('should validate FQDN', () => {
test({
validator: 'isFQDN',
Expand Down Expand Up @@ -4158,6 +4181,35 @@ describe('Validators', () => {
'081234567891',
],
},
{
locale: ['en-ZA', 'be-BY'],
valid: [
'0821231234',
'+27821231234',
'27821231234',
'+375241234567',
'+375251234567',
'+375291234567',
'+375331234567',
'+375441234567',
'375331234567',
],
invalid: [
'082123',
'08212312345',
'21821231234',
'+21821231234',
'+0821231234',
'12345',
'',
'ASDFGJKLmZXJtZtesting123',
'010-38238383',
'+9676338855',
'19676338855',
'6676338855',
'+99676338855',
],
},
];

let allValid = [];
Expand All @@ -4167,15 +4219,11 @@ describe('Validators', () => {
if (fixture.valid) allValid = allValid.concat(fixture.valid);

if (Array.isArray(fixture.locale)) {
// for fixtures that are shared across multiple locales
// e.g. 'nb-NO' and 'nn-NO'
fixture.locale.forEach((locale) => {
test({
validator: 'isMobilePhone',
valid: fixture.valid,
invalid: fixture.invalid,
args: [locale],
});
test({
validator: 'isMobilePhone',
valid: fixture.valid,
invalid: fixture.invalid,
args: [fixture.locale],
});
} else {
test({
Expand Down

0 comments on commit 2e6cc37

Please sign in to comment.