Skip to content

Commit

Permalink
Merge pull request #733 from frontsideair/isPort
Browse files Browse the repository at this point in the history
Add isPort validator
  • Loading branch information
chriso authored Sep 29, 2017
2 parents 8b3018e + 8a6094d commit c277078
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Validator | Description
**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.
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AT', 'AU', 'BE', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'ES', 'FI', 'FR', 'GB', 'GR', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'TW', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }`.
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ var _isNumeric = require('./lib/isNumeric');

var _isNumeric2 = _interopRequireDefault(_isNumeric);

var _isPort = require('./lib/isPort');

var _isPort2 = _interopRequireDefault(_isPort);

var _isLowercase = require('./lib/isLowercase');

var _isLowercase2 = _interopRequireDefault(_isLowercase);
Expand Down Expand Up @@ -282,6 +286,7 @@ var validator = {
isAlpha: _isAlpha2.default,
isAlphanumeric: _isAlphanumeric2.default,
isNumeric: _isNumeric2.default,
isPort: _isPort2.default,
isLowercase: _isLowercase2.default,
isUppercase: _isUppercase2.default,
isAscii: _isAscii2.default,
Expand Down
17 changes: 17 additions & 0 deletions lib/isPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

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

var _isInt = require('./isInt');

var _isInt2 = _interopRequireDefault(_isInt);

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

function isPort(str) {
return (0, _isInt2.default)(str, { min: 0, max: 65535 });
}
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 @@ -17,6 +17,7 @@ import isBoolean from './lib/isBoolean';
import isAlpha from './lib/isAlpha';
import isAlphanumeric from './lib/isAlphanumeric';
import isNumeric from './lib/isNumeric';
import isPort from './lib/isPort';
import isLowercase from './lib/isLowercase';
import isUppercase from './lib/isUppercase';

Expand Down Expand Up @@ -106,6 +107,7 @@ const validator = {
isAlpha,
isAlphanumeric,
isNumeric,
isPort,
isLowercase,
isUppercase,
isAscii,
Expand Down
5 changes: 5 additions & 0 deletions src/lib/isPort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import isInt from './isInt';

export default function isPort(str) {
return isInt(str, { min: 0, max: 65535 });
}
20 changes: 20 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,26 @@ describe('Validators', function () {
});
});

it('should validate ports', function () {
test({
validator: 'isPort',
valid: [
'0',
'22',
'80',
'443',
'3000',
'8080',
'65535',
],
invalid: [
'',
'-1',
'65536',
],
});
});

it('should validate decimal numbers', function () {
test({
validator: 'isDecimal',
Expand Down
45 changes: 25 additions & 20 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,30 @@ function isNumeric(str) {
return numeric.test(str);
}

var int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
var intLeadingZeroes = /^[-+]?[0-9]+$/;

function isInt(str, options) {
assertString(str);
options = options || {};

// Get the regex to use for testing, based on whether
// leading zeroes are allowed or not.
var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? int : intLeadingZeroes;

// Check min/max/lt/gt
var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;

return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}

function isPort(str) {
return isInt(str, { min: 0, max: 65535 });
}

function isLowercase(str) {
assertString(str);
return str === str.toLowerCase();
Expand Down Expand Up @@ -581,26 +605,6 @@ function isSurrogatePair(str) {
return surrogatePair.test(str);
}

var int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
var intLeadingZeroes = /^[-+]?[0-9]+$/;

function isInt(str, options) {
assertString(str);
options = options || {};

// Get the regex to use for testing, based on whether
// leading zeroes are allowed or not.
var regex = options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ? int : intLeadingZeroes;

// Check min/max/lt/gt
var minCheckPassed = !options.hasOwnProperty('min') || str >= options.min;
var maxCheckPassed = !options.hasOwnProperty('max') || str <= options.max;
var ltCheckPassed = !options.hasOwnProperty('lt') || str < options.lt;
var gtCheckPassed = !options.hasOwnProperty('gt') || str > options.gt;

return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}

var float = /^(?:[-+])?(?:[0-9]+)?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/;

function isFloat(str, options) {
Expand Down Expand Up @@ -1373,6 +1377,7 @@ var validator = {
isAlpha: isAlpha,
isAlphanumeric: isAlphanumeric,
isNumeric: isNumeric,
isPort: isPort,
isLowercase: isLowercase,
isUppercase: isUppercase,
isAscii: isAscii,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit c277078

Please sign in to comment.