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

fix(isMacAddress): improve regexes and options #1616

Merged
merged 6 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -134,7 +134,7 @@ Validator | Description
**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`)
**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_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.<br/><br/>`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'.
fedeci marked this conversation as resolved.
Show resolved Hide resolved
**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
17 changes: 8 additions & 9 deletions src/lib/isMACAddress.js
Original file line number Diff line number Diff line change
@@ -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})$/;
fedeci marked this conversation as resolved.
Show resolved Hide resolved
const macAddressNoSeparators = /^([0-9a-fA-F]){12}$/;
fedeci marked this conversation as resolved.
Show resolved Hide resolved
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
fedeci marked this conversation as resolved.
Show resolved Hide resolved
*/
if (options && (options.no_colons || options.noSeparators)) {
fedeci marked this conversation as resolved.
Show resolved Hide resolved
return macAddressNoSeparators.test(str);
}

return macAddress.test(str)
|| macAddressWithHyphen.test(str)
|| macAddressWithSpaces.test(str)
|| macAddressWithDots.test(str);
}
6 changes: 4 additions & 2 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
fedeci marked this conversation as resolved.
Show resolved Hide resolved
}],
valid: [
'abababababab',
Expand Down