Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for MAC address validation without colons, and Numeric validation without symbols #847

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ Validator | Description
**isLatLong(str)**                     | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
**isLowercase(str)** | check if the string is lowercase.
**isMACAddress(str)** | check if the string is a MAC address.
**isMACAddress(str [, options])** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{noColons: false}`, meaning that it will only validate MAC addresses containing 5 colons.
**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 either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-JO', 'ar-KW', '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.
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{noSymbols: false}`, meaning that it will validate strings that do contain any of `+`, `-`, or `.`.
**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', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. locale list is `validator.isPostalCodeLocales`.).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
Expand Down
6 changes: 5 additions & 1 deletion lib/isMACAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ var _assertString2 = _interopRequireDefault(_assertString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
var macAddressNoColons = /^([0-9a-fA-F]){12}$/;

function isMACAddress(str) {
function isMACAddress(str, options) {
(0, _assertString2.default)(str);
if (options && options.noColons) {
return macAddressNoColons.test(str);
}
return macAddress.test(str);
}
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion lib/isNumeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ var _assertString2 = _interopRequireDefault(_assertString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
var numericNoSymbols = /^[0-9]*$/;

function isNumeric(str) {
function isNumeric(str, options) {
(0, _assertString2.default)(str);
if (options && options.noSymbols) {
return numericNoSymbols.test(str);
}
return numeric.test(str);
}
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion src/lib/isMACAddress.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import assertString from './util/assertString';

const macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
const macAddressNoColons = /^([0-9a-fA-F]){12}$/;

export default function isMACAddress(str) {
export default function isMACAddress(str, options) {
assertString(str);
if (options && options.noColons) {
return macAddressNoColons.test(str);
}
return macAddress.test(str);
}
6 changes: 5 additions & 1 deletion src/lib/isNumeric.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import assertString from './util/assertString';

const numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
const numericNoSymbols = /^[0-9]*$/;

export default function isNumeric(str) {
export default function isNumeric(str, options) {
assertString(str);
if (options && options.noSymbols) {
return numericNoSymbols.test(str);
}
return numeric.test(str);
}
52 changes: 52 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,36 @@ describe('Validators', () => {
});
});

it('should validate MAC addresses without colons', () => {
test({
validator: 'isMACAddress',
args: [{
noColons: true,
}],
valid: [
'abababababab',
'FFFFFFFFFFFF',
'0102030405ab',
'01AB03040506',
],
invalid: [
'abc',
'01:02:03:04:05',
'01:02:03:04::ab',
'1:2:3:4:5:6',
'AB:CD:EF:GH:01:02',
'ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:ab',
'01:AB:03:04:05:06',
'0102030405',
'01020304ab',
'123456',
'ABCDEFGH0102',
],
});
});

it('should validate IP addresses', () => {
test({
validator: 'isIP',
Expand Down Expand Up @@ -1508,6 +1538,28 @@ describe('Validators', () => {
});
});

it('should validate numeric strings without symbols', () => {
test({
validator: 'isNumeric',
args: [{
noSymbols: true,
}],
valid: [
'123',
'00123',
'0',
],
invalid: [
'-0',
'+123',
'123.123',
'-00123',
' ',
'.',
],
});
});

it('should validate ports', () => {
test({
validator: 'isPort',
Expand Down
12 changes: 10 additions & 2 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,13 @@ function isURL(url, options) {
}

var macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
var macAddressNoColons = /^([0-9a-fA-F]){12}$/;

function isMACAddress(str) {
function isMACAddress(str, options) {
assertString(str);
if (options && options.noColons) {
return macAddressNoColons.test(str);
}
return macAddress.test(str);
}

Expand Down Expand Up @@ -599,9 +603,13 @@ function isAlphanumeric(str) {
}

var numeric = /^[+-]?([0-9]*[.])?[0-9]+$/;
var numericNoSymbols = /^[0-9]*$/;

function isNumeric(str) {
function isNumeric(str, options) {
assertString(str);
if (options && options.noSymbols) {
return numericNoSymbols.test(str);
}
return numeric.test(str);
}

Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.