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 MAC address validation without colons #849

Merged
merged 5 commits into from
Jul 31, 2018
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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)** | 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-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'bn-BD', '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 `+`.
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 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);
}
30 changes: 30 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,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
6 changes: 5 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,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
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.