Skip to content

Commit f055c11

Browse files
WikiRiktux-tn
andauthored
feat(isMACAddress): add EUI-64 validation (#1865)
* feat(isMACAddress): add EUI-64 validation * Update src/lib/isMACAddress.js Co-authored-by: Sarhan Aissi <tux-tn@users.noreply.github.com> * Add possible values of eui to README.md Co-authored-by: Rik Smale <WikiRik@users.noreply.github.com> Co-authored-by: Sarhan Aissi <tux-tn@users.noreply.github.com>
1 parent 9c12b4c commit f055c11

3 files changed

Lines changed: 148 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Validator | Description
142142
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', pt-PT', 'sq-AL', 'pt-BR']` or `any`)
143143
**isLocale(str)** | check if the string is a locale
144144
**isLowercase(str)** | check if the string is lowercase.
145-
**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'.
145+
**isMACAddress(str [, options])** | 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'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64.
146146
**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
147147
**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).
148148
**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: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import assertString from './util/assertString';
22

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})$/;
3+
const macAddress48 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/;
4+
const macAddress48NoSeparators = /^([0-9a-fA-F]){12}$/;
5+
const macAddress48WithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/;
6+
const macAddress64 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$/;
7+
const macAddress64NoSeparators = /^([0-9a-fA-F]){16}$/;
8+
const macAddress64WithDots = /^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$/;
69

710
export default function isMACAddress(str, options) {
811
assertString(str);
12+
if (options?.eui) {
13+
options.eui = String(options.eui);
14+
}
915
/**
1016
* @deprecated `no_colons` TODO: remove it in the next major
1117
*/
12-
if (options && (options.no_colons || options.no_separators)) {
13-
return macAddressNoSeparators.test(str);
18+
if (options?.no_colons || options?.no_separators) {
19+
if (options.eui === '48') {
20+
return macAddress48NoSeparators.test(str);
21+
}
22+
if (options.eui === '64') {
23+
return macAddress64NoSeparators.test(str);
24+
}
25+
return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str);
1426
}
15-
16-
return macAddress.test(str)
17-
|| macAddressWithDots.test(str);
27+
if (options?.eui === '48') {
28+
return macAddress48.test(str) || macAddress48WithDots.test(str);
29+
}
30+
if (options?.eui === '64') {
31+
return macAddress64.test(str) || macAddress64WithDots.test(str);
32+
}
33+
return isMACAddress(str, { eui: '48' }) || isMACAddress(str, { eui: '64' });
1834
}

test/validators.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,14 @@ describe('Validators', () => {
810810
'01 02 03 04 05 ab',
811811
'01-02-03-04-05-ab',
812812
'0102.0304.05ab',
813+
'ab:ab:ab:ab:ab:ab:ab:ab',
814+
'FF:FF:FF:FF:FF:FF:FF:FF',
815+
'01:02:03:04:05:06:07:ab',
816+
'01:AB:03:04:05:06:07:08',
817+
'A9 C5 D4 9F EB D3 B6 65',
818+
'01 02 03 04 05 06 07 ab',
819+
'01-02-03-04-05-06-07-ab',
820+
'0102.0304.0506.07ab',
813821
],
814822
invalid: [
815823
'abc',
@@ -822,6 +830,67 @@ describe('Validators', () => {
822830
'01-02 03:04 05 ab',
823831
'0102.03:04.05ab',
824832
'900f/dffs/sdea',
833+
'01:02:03:04:05:06:07',
834+
'01:02:03:04:05:06:07:z0',
835+
'01:02:03:04:05:06::ab',
836+
'1:2:3:4:5:6:7:8',
837+
'AB:CD:EF:GH:01:02:03:04',
838+
'A9C5 D4 9F EB D3 B6 65',
839+
'01-02 03:04 05 06 07 ab',
840+
'0102.03:04.0506.07ab',
841+
'900f/dffs/sdea/54gh',
842+
],
843+
});
844+
test({
845+
validator: 'isMACAddress',
846+
args: [{
847+
eui: '48',
848+
}],
849+
valid: [
850+
'ab:ab:ab:ab:ab:ab',
851+
'FF:FF:FF:FF:FF:FF',
852+
'01:02:03:04:05:ab',
853+
'01:AB:03:04:05:06',
854+
'A9 C5 D4 9F EB D3',
855+
'01 02 03 04 05 ab',
856+
'01-02-03-04-05-ab',
857+
'0102.0304.05ab',
858+
],
859+
invalid: [
860+
'ab:ab:ab:ab:ab:ab:ab:ab',
861+
'FF:FF:FF:FF:FF:FF:FF:FF',
862+
'01:02:03:04:05:06:07:ab',
863+
'01:AB:03:04:05:06:07:08',
864+
'A9 C5 D4 9F EB D3 B6 65',
865+
'01 02 03 04 05 06 07 ab',
866+
'01-02-03-04-05-06-07-ab',
867+
'0102.0304.0506.07ab',
868+
],
869+
});
870+
test({
871+
validator: 'isMACAddress',
872+
args: [{
873+
eui: '64',
874+
}],
875+
valid: [
876+
'ab:ab:ab:ab:ab:ab:ab:ab',
877+
'FF:FF:FF:FF:FF:FF:FF:FF',
878+
'01:02:03:04:05:06:07:ab',
879+
'01:AB:03:04:05:06:07:08',
880+
'A9 C5 D4 9F EB D3 B6 65',
881+
'01 02 03 04 05 06 07 ab',
882+
'01-02-03-04-05-06-07-ab',
883+
'0102.0304.0506.07ab',
884+
],
885+
invalid: [
886+
'ab:ab:ab:ab:ab:ab',
887+
'FF:FF:FF:FF:FF:FF',
888+
'01:02:03:04:05:ab',
889+
'01:AB:03:04:05:06',
890+
'A9 C5 D4 9F EB D3',
891+
'01 02 03 04 05 ab',
892+
'01-02-03-04-05-ab',
893+
'0102.0304.05ab',
825894
],
826895
});
827896
});
@@ -837,6 +906,10 @@ describe('Validators', () => {
837906
'FFFFFFFFFFFF',
838907
'0102030405ab',
839908
'01AB03040506',
909+
'abababababababab',
910+
'FFFFFFFFFFFFFFFF',
911+
'01020304050607ab',
912+
'01AB030405060708',
840913
],
841914
invalid: [
842915
'abc',
@@ -852,6 +925,56 @@ describe('Validators', () => {
852925
'01020304ab',
853926
'123456',
854927
'ABCDEFGH0102',
928+
'01:02:03:04:05:06:07',
929+
'01:02:03:04:05:06::ab',
930+
'1:2:3:4:5:6:7:8',
931+
'AB:CD:EF:GH:01:02:03:04',
932+
'ab:ab:ab:ab:ab:ab:ab:ab',
933+
'FF:FF:FF:FF:FF:FF:FF:FF',
934+
'01:02:03:04:05:06:07:ab',
935+
'01:AB:03:04:05:06:07:08',
936+
'01020304050607',
937+
'010203040506ab',
938+
'12345678',
939+
'ABCDEFGH01020304',
940+
],
941+
});
942+
test({
943+
validator: 'isMACAddress',
944+
args: [{
945+
no_separators: true,
946+
eui: '48',
947+
}],
948+
valid: [
949+
'abababababab',
950+
'FFFFFFFFFFFF',
951+
'0102030405ab',
952+
'01AB03040506',
953+
],
954+
invalid: [
955+
'abababababababab',
956+
'FFFFFFFFFFFFFFFF',
957+
'01020304050607ab',
958+
'01AB030405060708',
959+
],
960+
});
961+
test({
962+
validator: 'isMACAddress',
963+
args: [{
964+
no_separators: true,
965+
eui: '64',
966+
}],
967+
valid: [
968+
'abababababababab',
969+
'FFFFFFFFFFFFFFFF',
970+
'01020304050607ab',
971+
'01AB030405060708',
972+
],
973+
invalid: [
974+
'abababababab',
975+
'FFFFFFFFFFFF',
976+
'0102030405ab',
977+
'01AB03040506',
855978
],
856979
});
857980
});

0 commit comments

Comments
 (0)