From f055c111d8720dd5fad720f218f1399ce357e81a Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Wed, 17 Nov 2021 08:25:45 +0100 Subject: [PATCH] feat(isMACAddress): add EUI-64 validation (#1865) * feat(isMACAddress): add EUI-64 validation * Update src/lib/isMACAddress.js Co-authored-by: Sarhan Aissi * Add possible values of eui to README.md Co-authored-by: Rik Smale Co-authored-by: Sarhan Aissi --- README.md | 2 +- src/lib/isMACAddress.js | 32 ++++++++--- test/validators.js | 123 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0ad4539b3..0d2ed2bce 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ Validator | Description **isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(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.

`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.

`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.

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 diff --git a/src/lib/isMACAddress.js b/src/lib/isMACAddress.js index 8ffc8c254..d87cd4aa7 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -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' }); } diff --git a/test/validators.js b/test/validators.js index cdcf21f70..76ec3d95a 100644 --- a/test/validators.js +++ b/test/validators.js @@ -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', @@ -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', ], }); }); @@ -837,6 +906,10 @@ describe('Validators', () => { 'FFFFFFFFFFFF', '0102030405ab', '01AB03040506', + 'abababababababab', + 'FFFFFFFFFFFFFFFF', + '01020304050607ab', + '01AB030405060708', ], invalid: [ 'abc', @@ -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', ], }); });