Skip to content

Commit de48970

Browse files
authored
fix(isMacAddress): improve regexes and options (#1616)
* fix(isMacAddress): improve Regexes * docs: update docs * Update src/lib/isMACAddress.js * Update src/lib/isMACAddress.js * Update test/validators.js * Update README.md
1 parent ae9a311 commit de48970

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Validator | Description
134134
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL']` or `any`)
135135
**isLocale(str)** | check if the string is a locale
136136
**isLowercase(str)** | check if the string is lowercase.
137-
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_colons: false}`. If `no_colons` is true, the validator will allow MAC addresses without the colons. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'.
137+
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'.
138138
**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
139139
**isMD5(str)** | check if the string is a MD5 hash.<br/><br/>Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA).
140140
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format

src/lib/isMACAddress.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import assertString from './util/assertString';
22

3-
const macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
4-
const macAddressNoColons = /^([0-9a-fA-F]){12}$/;
5-
const macAddressWithHyphen = /^([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])$/;
6-
const macAddressWithSpaces = /^([0-9a-fA-F][0-9a-fA-F]\s){5}([0-9a-fA-F][0-9a-fA-F])$/;
7-
const macAddressWithDots = /^([0-9a-fA-F]{4}).([0-9a-fA-F]{4}).([0-9a-fA-F]{4})$/;
3+
const macAddress = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/;
4+
const macAddressNoSeparators = /^([0-9a-fA-F]){12}$/;
5+
const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/;
86

97
export default function isMACAddress(str, options) {
108
assertString(str);
11-
if (options && options.no_colons) {
12-
return macAddressNoColons.test(str);
9+
/**
10+
* @deprecated `no_colons` TODO: remove it in the next major
11+
*/
12+
if (options && (options.no_colons || options.no_separators)) {
13+
return macAddressNoSeparators.test(str);
1314
}
1415

1516
return macAddress.test(str)
16-
|| macAddressWithHyphen.test(str)
17-
|| macAddressWithSpaces.test(str)
1817
|| macAddressWithDots.test(str);
1918
}

test/validators.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,21 +719,23 @@ describe('Validators', () => {
719719
invalid: [
720720
'abc',
721721
'01:02:03:04:05',
722+
'01:02:03:04:05:z0',
722723
'01:02:03:04::ab',
723724
'1:2:3:4:5:6',
724725
'AB:CD:EF:GH:01:02',
725726
'A9C5 D4 9F EB D3',
726727
'01-02 03:04 05 ab',
727728
'0102.03:04.05ab',
729+
'900f/dffs/sdea',
728730
],
729731
});
730732
});
731733

732-
it('should validate MAC addresses without colons', () => {
734+
it('should validate MAC addresses without separator', () => {
733735
test({
734736
validator: 'isMACAddress',
735737
args: [{
736-
no_colons: true,
738+
no_separators: true,
737739
}],
738740
valid: [
739741
'abababababab',

0 commit comments

Comments
 (0)