Skip to content

fix(isHexColor): add require_hashtag option #2535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Numbers0689
Copy link

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!

@WikiRik
Copy link
Member

WikiRik commented Mar 20, 2025

See my comment in #2533 (comment)
I don't see this becoming the default behaviour, but we could add an option to enforce the use of # prefix

@Numbers0689
Copy link
Author

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.

@Numbers0689 Numbers0689 changed the title Enforce # prefix for Hexcolor validation in isHexColor Add require_hashtag option to isHexColor Mar 20, 2025
@Numbers0689
Copy link
Author

@WikiRik
I've updated the implementation to add a require_hashtag option, updated the tests, and made corresponding changes to the readme.

Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you copy the 'default' test set to be checked against args: [{ require_hashtag: false }], as well? Just for completeness

@Numbers0689
Copy link
Author

@WikiRik done!

@Numbers0689 Numbers0689 requested a review from WikiRik March 20, 2025 13:36
@WikiRik WikiRik requested a review from rubiin March 20, 2025 13:37
@WikiRik WikiRik changed the title Add require_hashtag option to isHexColor fix(isHexColor): add require_hashtag option Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants