Skip to content

Commit f39bb3b

Browse files
yuna0831rubiin
andauthored
fix: ignore non-object options in isHexColor (#2660) (#2661)
Co-authored-by: Rubin Bhandari <roobin.bhandari@gmail.com>
1 parent 88e0d3d commit f39bb3b

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/lib/isHexColor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const default_is_hexcolor_options = {
1111
export default function isHexColor(str, options) {
1212
assertString(str);
1313
options = merge(options, default_is_hexcolor_options);
14+
1415
const hexcolor_regex = options.require_hashtag
1516
? hexcolor_with_prefix
1617
: hexcolor;

src/lib/util/merge.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export default function merge(obj = { }, defaults) {
2+
if (typeof obj !== 'object' || obj === null) {
3+
obj = {};
4+
}
25
for (const key in defaults) {
36
if (typeof obj[key] === 'undefined') {
47
obj[key] = defaults[key];

test/validators.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'assert';
22
import fs from 'fs';
33
import timezone_mock from 'timezone-mock';
44
import vm from 'vm';
5+
import validator from '../index';
56
import test from './testFunctions';
67

78
let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString();
@@ -5041,6 +5042,20 @@ describe('Validators', () => {
50415042
'',
50425043
],
50435044
});
5045+
test({
5046+
validator: 'isHexColor',
5047+
args: [null],
5048+
valid: ['#fff', '#000000', '123'],
5049+
invalid: ['not-a-color'],
5050+
});
5051+
test({
5052+
validator: 'isHexColor',
5053+
args: [123],
5054+
valid: ['#fff', '#000000', '123', 'abc'],
5055+
invalid: ['gray', 'not-a-color'],
5056+
});
5057+
const validColors = ['#ff0034', '#CCCCCC'].filter(validator.isHexColor);
5058+
assert.strictEqual(validColors.length, 2);
50445059
});
50455060

50465061
it('should validate HSL color strings', () => {

0 commit comments

Comments
 (0)