diff --git a/src/lib/isHexColor.js b/src/lib/isHexColor.js index b7a2e5a23..34903f413 100644 --- a/src/lib/isHexColor.js +++ b/src/lib/isHexColor.js @@ -11,6 +11,7 @@ const default_is_hexcolor_options = { export default function isHexColor(str, options) { assertString(str); options = merge(options, default_is_hexcolor_options); + const hexcolor_regex = options.require_hashtag ? hexcolor_with_prefix : hexcolor; 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..b5a6be4c5 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,20 @@ 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); }); it('should validate HSL color strings', () => {