From 7416c6b27699d3ffd3d94b9d1f8018a07516dd79 Mon Sep 17 00:00:00 2001 From: Numbers0689 Date: Thu, 20 Mar 2025 12:53:42 +0530 Subject: [PATCH 1/3] Enforce # prefix for Hexcolor validation --- README.md | 2 +- src/lib/isHexColor.js | 2 +- test/validators.test.js | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0889a5ae0..a3b132a5a 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Validator | Description **isHalfWidth(str)** | check if the string contains any half-width chars. **isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`. **isHexadecimal(str)** | check if the string is a hexadecimal number. -**isHexColor(str)** | check if the string is a hexadecimal color. +**isHexColor(str)** | check if the string is a hexadecimal color with # prefix. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification][CSS Colors Level 4 Specification].

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). **isIBAN(str, [, options])** | check if the string is an IBAN (International Bank Account Number).

`options` is an object which accepts two attributes: `whitelist`: where you can restrict IBAN codes you want to receive data from and `blacklist`: where you can remove some of the countries from the current list. For both you can use an array with the following values `['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','IS','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK']`. **isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK', 'PK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.

Defaults to 'any'. diff --git a/src/lib/isHexColor.js b/src/lib/isHexColor.js index 21a037504..ee0fba39e 100644 --- a/src/lib/isHexColor.js +++ b/src/lib/isHexColor.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -const hexcolor = /^#?([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; +const hexcolor = /^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; export default function isHexColor(str) { assertString(str); diff --git a/test/validators.test.js b/test/validators.test.js index e85d041b6..723e37270 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4696,21 +4696,23 @@ describe('Validators', () => { }); }); - it('should validate hexadecimal color strings', () => { + it('should validate hexadecimal color strings with # prefix', () => { test({ validator: 'isHexColor', valid: [ '#ff0000ff', '#ff0034', '#CCCCCC', - '0f38', - 'fff', + '#0f38', + '#fff', '#f00', ], invalid: [ '#ff', 'fff0a', '#ff12FG', + '0f38', + 'fff', ], }); }); From 97556b76f2fe205e99c1b7c1155c31e57878b08e Mon Sep 17 00:00:00 2001 From: Numbers0689 Date: Thu, 20 Mar 2025 15:49:24 +0530 Subject: [PATCH 2/3] Add require_hashtag option to isHexColor --- README.md | 2 +- src/lib/isHexColor.js | 16 +++++++++++++--- test/validators.test.js | 23 ++++++++++++++++++++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a3b132a5a..d97ae7a84 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Validator | Description **isHalfWidth(str)** | check if the string contains any half-width chars. **isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`. **isHexadecimal(str)** | check if the string is a hexadecimal number. -**isHexColor(str)** | check if the string is a hexadecimal color with # prefix. +**isHexColor(str [, options])** | check if the string is a hexadecimal color.

`options` is an object that defaults to `{ require_hashtag: false }`.
Options:
`require_hashtag`: Enforce # prefix, default false. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification][CSS Colors Level 4 Specification].

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). **isIBAN(str, [, options])** | check if the string is an IBAN (International Bank Account Number).

`options` is an object which accepts two attributes: `whitelist`: where you can restrict IBAN codes you want to receive data from and `blacklist`: where you can remove some of the countries from the current list. For both you can use an array with the following values `['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','IS','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK']`. **isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK', 'PK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.

Defaults to 'any'. diff --git a/src/lib/isHexColor.js b/src/lib/isHexColor.js index ee0fba39e..b7a2e5a23 100644 --- a/src/lib/isHexColor.js +++ b/src/lib/isHexColor.js @@ -1,8 +1,18 @@ import assertString from './util/assertString'; +import merge from './util/merge'; -const hexcolor = /^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; +const hexcolor = /^#?([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; +const hexcolor_with_prefix = /^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; -export default function isHexColor(str) { +const default_is_hexcolor_options = { + require_hashtag: false, +}; + +export default function isHexColor(str, options) { assertString(str); - return hexcolor.test(str); + options = merge(options, default_is_hexcolor_options); + const hexcolor_regex = options.require_hashtag + ? hexcolor_with_prefix + : hexcolor; + return hexcolor_regex.test(str); } diff --git a/test/validators.test.js b/test/validators.test.js index 723e37270..1bba1cc3b 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4696,9 +4696,28 @@ describe('Validators', () => { }); }); - it('should validate hexadecimal color strings with # prefix', () => { + it('should validate hexadecimal color strings', () => { test({ validator: 'isHexColor', + valid: [ + '#ff0000ff', + '#ff0034', + '#CCCCCC', + '0f38', + 'fff', + '#f00', + ], + invalid: [ + '#ff', + 'fff0a', + '#ff12FG', + '#######', + '', + ], + }); + test({ + validator: 'isHexColor', + args: [{ require_hashtag: true }], valid: [ '#ff0000ff', '#ff0034', @@ -4713,6 +4732,8 @@ describe('Validators', () => { '#ff12FG', '0f38', 'fff', + '#######', + '', ], }); }); From 122bf984e0b34ce8c8302796e28801112b519ac8 Mon Sep 17 00:00:00 2001 From: Numbers0689 Date: Thu, 20 Mar 2025 17:55:51 +0530 Subject: [PATCH 3/3] Add testcase with default option for ishexcolor --- test/validators.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/validators.test.js b/test/validators.test.js index 1bba1cc3b..b5b8f32db 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4715,6 +4715,25 @@ describe('Validators', () => { '', ], }); + test({ + validator: 'isHexColor', + args: [{ require_hashtag: false }], + valid: [ + '#ff0000ff', + '#ff0034', + '#CCCCCC', + '0f38', + 'fff', + '#f00', + ], + invalid: [ + '#ff', + 'fff0a', + '#ff12FG', + '#######', + '', + ], + }); test({ validator: 'isHexColor', args: [{ require_hashtag: true }],