Skip to content

Commit

Permalink
Merge pull request #842 from excelciordan/master
Browse files Browse the repository at this point in the history
Add isIPRange validator
  • Loading branch information
chriso authored Jun 12, 2018
2 parents 186f8d1 + 9d6009b commit 8e924a2
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
node_modules
coverage
package-lock.json
yarn.lock
yarn.lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
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 Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
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
40 changes: 40 additions & 0 deletions lib/isIPRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
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 parts = str.split('/');

// parts[0] -> ip, parts[1] -> subnet
if (parts.length !== 2) {
return false;
}

if (!subnetMaybe.test(parts[1])) {
return false;
}

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

return (0, _isIP2.default)(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
}
module.exports = exports['default'];
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
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
25 changes: 25 additions & 0 deletions src/lib/isIPRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assertString from './util/assertString';
import isIP from './isIP';

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

export default function isIPRange(str) {
assertString(str);
const parts = str.split('/');

// parts[0] -> ip, parts[1] -> subnet
if (parts.length !== 2) {
return false;
}

if (!subnetMaybe.test(parts[1])) {
return false;
}

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

return isIP(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
}
23 changes: 23 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
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
24 changes: 24 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,29 @@ function isMACAddress(str) {
return macAddress.test(str);
}

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

function isIPRange(str) {
assertString(str);
var parts = str.split('/');

// parts[0] -> ip, parts[1] -> subnet
if (parts.length !== 2) {
return false;
}

if (!subnetMaybe.test(parts[1])) {
return false;
}

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

return isIP(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
}

function isBoolean(str) {
assertString(str);
return ['true', 'false', '1', '0'].indexOf(str) >= 0;
Expand Down Expand Up @@ -1578,6 +1601,7 @@ var validator = {
isURL: isURL,
isMACAddress: isMACAddress,
isIP: isIP,
isIPRange: isIPRange,
isFQDN: isFQDN,
isBoolean: isBoolean,
isAlpha: isAlpha,
Expand Down
3 changes: 2 additions & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 8e924a2

Please sign in to comment.