From 89af5e8439712eb02ab534e84925a604b0dcaf07 Mon Sep 17 00:00:00 2001 From: yuna0831 Date: Tue, 27 Jan 2026 20:48:54 -0600 Subject: [PATCH 1/3] fix: ignore non-object options in isHexColor (#2660) --- src/lib/isHexColor.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib/isHexColor.js b/src/lib/isHexColor.js index b7a2e5a23..805c6192f 100644 --- a/src/lib/isHexColor.js +++ b/src/lib/isHexColor.js @@ -10,7 +10,13 @@ const default_is_hexcolor_options = { export default function isHexColor(str, options) { assertString(str); + + if (typeof options !== 'object' || options === null) { + options = {}; + } + options = merge(options, default_is_hexcolor_options); + const hexcolor_regex = options.require_hashtag ? hexcolor_with_prefix : hexcolor; From e5a82c25285e771f645dcb5d1c04c530cb91f6f1 Mon Sep 17 00:00:00 2001 From: yuna0831 Date: Wed, 28 Jan 2026 08:09:35 -0600 Subject: [PATCH 2/3] refactor: move type check to merge util and add filter test case --- src/lib/isHexColor.js | 5 ----- src/lib/util/merge.js | 3 +++ test/validators.test.js | 3 +++ 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lib/isHexColor.js b/src/lib/isHexColor.js index 805c6192f..34903f413 100644 --- a/src/lib/isHexColor.js +++ b/src/lib/isHexColor.js @@ -10,11 +10,6 @@ const default_is_hexcolor_options = { export default function isHexColor(str, options) { assertString(str); - - if (typeof options !== 'object' || options === null) { - options = {}; - } - options = merge(options, default_is_hexcolor_options); const hexcolor_regex = options.require_hashtag diff --git a/src/lib/util/merge.js b/src/lib/util/merge.js index 071477696..d50329b56 100644 --- a/src/lib/util/merge.js +++ b/src/lib/util/merge.js @@ -1,4 +1,7 @@ export default function merge(obj = { }, defaults) { + if (typeof obj !== 'object' || obj === null) { + obj = {}; + } for (const key in defaults) { if (typeof obj[key] === 'undefined') { obj[key] = defaults[key]; diff --git a/test/validators.test.js b/test/validators.test.js index 9bd00d6ec..970949dd8 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -2,6 +2,7 @@ import assert from 'assert'; import fs from 'fs'; import timezone_mock from 'timezone-mock'; import vm from 'vm'; +import validator from '../index'; import test from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); @@ -5041,6 +5042,8 @@ describe('Validators', () => { '', ], }); + const validColors = ['#ff0034', '#CCCCCC'].filter(validator.isHexColor); + assert.strictEqual(validColors.length, 2); }); it('should validate HSL color strings', () => { From a13b25bef93c59bbcb9b130cf20f9669564d9e5a Mon Sep 17 00:00:00 2001 From: yuna0831 Date: Wed, 28 Jan 2026 16:54:56 -0600 Subject: [PATCH 3/3] fix: handle null and non-object options in isHexColor with 100% coverage --- test/validators.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/validators.test.js b/test/validators.test.js index 970949dd8..b5a6be4c5 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -5042,6 +5042,18 @@ describe('Validators', () => { '', ], }); + test({ + validator: 'isHexColor', + args: [null], + valid: ['#fff', '#000000', '123'], + invalid: ['not-a-color'], + }); + test({ + validator: 'isHexColor', + args: [123], + valid: ['#fff', '#000000', '123', 'abc'], + invalid: ['gray', 'not-a-color'], + }); const validColors = ['#ff0034', '#CCCCCC'].filter(validator.isHexColor); assert.strictEqual(validColors.length, 2); });