fix(isHexColor): add `require_hashtag` option
isHexColor: Enforce # prefix for Hexcolor validation
Description:
This PR updates the isHexColor function in validator.js to enforce the presence of a # prefix for hex color validation. Previously, the function accepted both formats with and without #, leading to inconsistent behavior. Hex color codes are commonly expected to start with a #, so this change aligns the implementation with industry standards.
Changes Made:
- Updated the regex to require a # prefix.
- Adjusted the test cases to ensure validation only passes for hex colors with a #.
- Invalidated hex colors without # in the test suite.
- Ensures stricter validation for hex color inputs.
- Aligns with CSS and other standard hex color formats.
- Prevents unexpected behavior in downstream applications.
- ref: mdn
Checklist
- [ X] PR contains only changes related; no stray files, etc.
- [ X] README updated (where applicable)
- [ X] Tests written (where applicable)
- [ X] References provided in PR (where applicable)
Please let me know if further adjustments or additional tests are needed!
See my comment in https://github.com/validatorjs/validator.js/issues/2533#issuecomment-2739678804 I don't see this becoming the default behaviour, but we could add an option to enforce the use of # prefix
thank you for the feedback! that makes sense. I'll implement the requireHashtag option as suggested to avoid any breaking changes. I'll update the pr soon.
@WikiRik
I've updated the implementation to add a require_hashtag option, updated the tests, and made corresponding changes to the readme.
@WikiRik done!