diff --git a/src/lib/isMACAddress.js b/src/lib/isMACAddress.js index d87cd4aa7..7c348ee05 100644 --- a/src/lib/isMACAddress.js +++ b/src/lib/isMACAddress.js @@ -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' }); diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..e326df3e5 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -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',