From 389b5ef06b573d0ff5e613c0e2fc769e9d459f9b Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Wed, 24 Feb 2021 11:48:43 +0100 Subject: [PATCH 1/6] fix(isMacAddress): improve Regexes --- src/lib/isMACAddress.js | 17 ++++++++--------- test/validators.js | 6 ++++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/lib/isMACAddress.js b/src/lib/isMACAddress.js index 458c72c64..748ea4ca7 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -1,19 +1,18 @@ 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}$/; -const macAddressWithHyphen = /^([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])$/; -const macAddressWithSpaces = /^([0-9a-fA-F][0-9a-fA-F]\s){5}([0-9a-fA-F][0-9a-fA-F])$/; -const macAddressWithDots = /^([0-9a-fA-F]{4}).([0-9a-fA-F]{4}).([0-9a-fA-F]{4})$/; +const macAddress = /^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$/; +const macAddressNoSeparator = /^([0-9a-fA-F]){12}$/; +const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; export default function isMACAddress(str, options) { assertString(str); - if (options && options.no_colons) { - return macAddressNoColons.test(str); + /** + * @deprecated `no_colons` deprecated + */ + if (options && (options.no_colons || options.noSeparator)) { + return macAddressNoSeparator.test(str); } return macAddress.test(str) - || macAddressWithHyphen.test(str) - || macAddressWithSpaces.test(str) || macAddressWithDots.test(str); } diff --git a/test/validators.js b/test/validators.js index b9899b626..d07458a05 100644 --- a/test/validators.js +++ b/test/validators.js @@ -719,21 +719,23 @@ describe('Validators', () => { invalid: [ 'abc', '01:02:03:04:05', + '01:02:03:04:05:z0', '01:02:03:04::ab', '1:2:3:4:5:6', 'AB:CD:EF:GH:01:02', 'A9C5 D4 9F EB D3', '01-02 03:04 05 ab', '0102.03:04.05ab', + '900f/dffs/sdea', ], }); }); - it('should validate MAC addresses without colons', () => { + it('should validate MAC addresses without separator', () => { test({ validator: 'isMACAddress', args: [{ - no_colons: true, + noSeparator: true, }], valid: [ 'abababababab', From 91817a826f90fc7490e502ecd56764975a6ba600 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Wed, 24 Feb 2021 12:46:47 +0100 Subject: [PATCH 2/6] docs: update docs --- README.md | 2 +- src/lib/isMACAddress.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b2896b73f..a8e2a4aa7 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Validator | Description **isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL']` 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_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'. +**isMACAddress(str)** | check if the string is a MAC address.

`options` is an object which defaults to `{noSeparators: false}`. If `noSeparators` 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'. **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 748ea4ca7..0ee0a2dde 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -1,7 +1,7 @@ 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 macAddressNoSeparator = /^([0-9a-fA-F]){12}$/; +const macAddressNoSeparators = /^([0-9a-fA-F]){12}$/; const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; export default function isMACAddress(str, options) { @@ -9,8 +9,8 @@ export default function isMACAddress(str, options) { /** * @deprecated `no_colons` deprecated */ - if (options && (options.no_colons || options.noSeparator)) { - return macAddressNoSeparator.test(str); + if (options && (options.no_colons || options.noSeparators)) { + return macAddressNoSeparators.test(str); } return macAddress.test(str) From 7beee7301abed0e38f363af2f8a480bbca72db20 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Wed, 24 Feb 2021 13:22:41 +0100 Subject: [PATCH 3/6] Update src/lib/isMACAddress.js --- src/lib/isMACAddress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isMACAddress.js b/src/lib/isMACAddress.js index 0ee0a2dde..5ac8675f6 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -9,7 +9,7 @@ export default function isMACAddress(str, options) { /** * @deprecated `no_colons` deprecated */ - if (options && (options.no_colons || options.noSeparators)) { + if (options && (options.no_colons || options.no_separators)) { return macAddressNoSeparators.test(str); } From c1b57ae522f5b52dee1e450cbbe691199dbaee42 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Wed, 24 Feb 2021 13:22:49 +0100 Subject: [PATCH 4/6] Update src/lib/isMACAddress.js --- src/lib/isMACAddress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isMACAddress.js b/src/lib/isMACAddress.js index 5ac8675f6..8ffc8c254 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -7,7 +7,7 @@ const macAddressWithDots = /^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$/; export default function isMACAddress(str, options) { assertString(str); /** - * @deprecated `no_colons` deprecated + * @deprecated `no_colons` TODO: remove it in the next major */ if (options && (options.no_colons || options.no_separators)) { return macAddressNoSeparators.test(str); From 006dd10edaa1154d704efa452f19c781f71c3fa3 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Wed, 24 Feb 2021 13:22:58 +0100 Subject: [PATCH 5/6] Update test/validators.js --- test/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/validators.js b/test/validators.js index d07458a05..97be2e9f8 100644 --- a/test/validators.js +++ b/test/validators.js @@ -735,7 +735,7 @@ describe('Validators', () => { test({ validator: 'isMACAddress', args: [{ - noSeparator: true, + no_separators: true, }], valid: [ 'abababababab', From 889284ef384f37ed02f762ac7ea320bcf109f3cf Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Wed, 24 Feb 2021 13:23:09 +0100 Subject: [PATCH 6/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a8e2a4aa7..72f859dd6 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Validator | Description **isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL']` 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 `{noSeparators: false}`. If `noSeparators` 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)** | 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'. **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