Skip to content
Open
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
12 changes: 5 additions & 7 deletions src/lib/isMACAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ 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);
}
const eui = options?.eui ? String(options.eui) : undefined;
/**
* @deprecated `no_colons` TODO: remove it in the next major
*/
if (options?.no_colons || options?.no_separators) {
if (options.eui === '48') {
if (eui === '48') {
return macAddress48NoSeparators.test(str);
}
if (options.eui === '64') {
if (eui === '64') {
return macAddress64NoSeparators.test(str);
}
return macAddress48NoSeparators.test(str) || macAddress64NoSeparators.test(str);
}
if (options?.eui === '48') {
if (eui === '48') {
return macAddress48.test(str) || macAddress48WithDots.test(str);
}
if (options?.eui === '64') {
if (eui === '64') {
return macAddress64.test(str) || macAddress64WithDots.test(str);
}
return isMACAddress(str, { eui: '48' }) || isMACAddress(str, { eui: '64' });
Expand Down
26 changes: 26 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,32 @@ describe('Validators', () => {
});
});

it('should validate MAC addresses with frozen EUI options', () => {
[48, '48', 64, '64'].forEach((eui) => {
const address = Number(eui) === 48 ? '01:23:45:67:89:ab' : '01:23:45:67:89:ab:cd:ef';
test({
validator: 'isMACAddress',
args: [Object.freeze({ eui })],
valid: [address, address.replace(/:/g, '-')],
invalid: ['invalid', Number(eui) === 48 ? '01:23:45:67:89:ab:cd:ef' : '01:23:45:67:89:ab'],
});
['no_separators', 'no_colons'].forEach((option) => {
test({
validator: 'isMACAddress',
args: [Object.freeze({ eui, [option]: true })],
valid: [address.replace(/:/g, '')],
invalid: [address, 'invalid'],
});
});
});
});

it('should not modify MAC address options', () => {
const options = { eui: 48 };
validator.isMACAddress('01:23:45:67:89:ab', options);
assert.strictEqual(options.eui, 48);
});

it('should validate MAC addresses without separator', () => {
test({
validator: 'isMACAddress',
Expand Down