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

feat(isMACAddress): add EUI-64 validation #1865

Merged
merged 3 commits into from
Nov 17, 2021
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 @@ -142,7 +142,7 @@ Validator | Description
**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`)
**isLocale(str)** | check if the string is a locale
**isLowercase(str)** | check if the string is lowercase.
**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'.
**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.
**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
**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).
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
Expand Down
32 changes: 24 additions & 8 deletions src/lib/isMACAddress.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import assertString from './util/assertString';

const macAddress = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/;
const macAddressNoSeparators = /^([0-9a-fA-F]){12}$/;
const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/;
const macAddress48 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/;
const macAddress48NoSeparators = /^([0-9a-fA-F]){12}$/;
const macAddress48WithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/;
const macAddress64 = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$/;
const macAddress64NoSeparators = /^([0-9a-fA-F]){16}$/;
const macAddress64WithDots = /^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$/;

export default function isMACAddress(str, options) {
assertString(str);
if (options?.eui) {
options.eui = String(options.eui);
}
/**
* @deprecated `no_colons` TODO: remove it in the next major
*/
if (options && (options.no_colons || options.no_separators)) {
return macAddressNoSeparators.test(str);
if (options?.no_colons || options?.no_separators) {
if (options.eui === '48') {
return macAddress48NoSeparators.test(str);
}
if (options.eui === '64') {
return macAddress64NoSeparators.test(str);
}
return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str);
}

return macAddress.test(str)
|| macAddressWithDots.test(str);
if (options?.eui === '48') {
return macAddress48.test(str) || macAddress48WithDots.test(str);
}
if (options?.eui === '64') {
return macAddress64.test(str) || macAddress64WithDots.test(str);
}
return isMACAddress(str, { eui: '48' }) || isMACAddress(str, { eui: '64' });
}
123 changes: 123 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,14 @@ describe('Validators', () => {
'01 02 03 04 05 ab',
'01-02-03-04-05-ab',
'0102.0304.05ab',
'ab:ab:ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:06:07:ab',
'01:AB:03:04:05:06:07:08',
'A9 C5 D4 9F EB D3 B6 65',
'01 02 03 04 05 06 07 ab',
'01-02-03-04-05-06-07-ab',
'0102.0304.0506.07ab',
],
invalid: [
'abc',
Expand All @@ -822,6 +830,67 @@ describe('Validators', () => {
'01-02 03:04 05 ab',
'0102.03:04.05ab',
'900f/dffs/sdea',
'01:02:03:04:05:06:07',
'01:02:03:04:05:06:07:z0',
'01:02:03:04:05:06::ab',
'1:2:3:4:5:6:7:8',
'AB:CD:EF:GH:01:02:03:04',
'A9C5 D4 9F EB D3 B6 65',
'01-02 03:04 05 06 07 ab',
'0102.03:04.0506.07ab',
'900f/dffs/sdea/54gh',
],
});
test({
validator: 'isMACAddress',
args: [{
eui: '48',
}],
valid: [
'ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:ab',
'01:AB:03:04:05:06',
'A9 C5 D4 9F EB D3',
'01 02 03 04 05 ab',
'01-02-03-04-05-ab',
'0102.0304.05ab',
],
invalid: [
'ab:ab:ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:06:07:ab',
'01:AB:03:04:05:06:07:08',
'A9 C5 D4 9F EB D3 B6 65',
'01 02 03 04 05 06 07 ab',
'01-02-03-04-05-06-07-ab',
'0102.0304.0506.07ab',
],
});
test({
validator: 'isMACAddress',
args: [{
eui: '64',
}],
valid: [
'ab:ab:ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:06:07:ab',
'01:AB:03:04:05:06:07:08',
'A9 C5 D4 9F EB D3 B6 65',
'01 02 03 04 05 06 07 ab',
'01-02-03-04-05-06-07-ab',
'0102.0304.0506.07ab',
],
invalid: [
'ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:ab',
'01:AB:03:04:05:06',
'A9 C5 D4 9F EB D3',
'01 02 03 04 05 ab',
'01-02-03-04-05-ab',
'0102.0304.05ab',
],
});
});
Expand All @@ -837,6 +906,10 @@ describe('Validators', () => {
'FFFFFFFFFFFF',
'0102030405ab',
'01AB03040506',
'abababababababab',
'FFFFFFFFFFFFFFFF',
'01020304050607ab',
'01AB030405060708',
],
invalid: [
'abc',
Expand All @@ -852,6 +925,56 @@ describe('Validators', () => {
'01020304ab',
'123456',
'ABCDEFGH0102',
'01:02:03:04:05:06:07',
'01:02:03:04:05:06::ab',
'1:2:3:4:5:6:7:8',
'AB:CD:EF:GH:01:02:03:04',
'ab:ab:ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:06:07:ab',
'01:AB:03:04:05:06:07:08',
'01020304050607',
'010203040506ab',
'12345678',
'ABCDEFGH01020304',
],
});
test({
validator: 'isMACAddress',
args: [{
no_separators: true,
eui: '48',
}],
valid: [
'abababababab',
'FFFFFFFFFFFF',
'0102030405ab',
'01AB03040506',
],
invalid: [
'abababababababab',
'FFFFFFFFFFFFFFFF',
'01020304050607ab',
'01AB030405060708',
],
});
test({
validator: 'isMACAddress',
args: [{
no_separators: true,
eui: '64',
}],
valid: [
'abababababababab',
'FFFFFFFFFFFFFFFF',
'01020304050607ab',
'01AB030405060708',
],
invalid: [
'abababababab',
'FFFFFFFFFFFF',
'0102030405ab',
'01AB03040506',
],
});
});
Expand Down