validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

fix(isHexColor): add `require_hashtag` option

Open Numbers0689 opened this issue 11 months ago • 4 comments

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!

Numbers0689 avatar Mar 20 '25 08:03 Numbers0689

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

WikiRik avatar Mar 20 '25 09:03 WikiRik

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 avatar Mar 20 '25 09:03 Numbers0689

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

Numbers0689 avatar Mar 20 '25 10:03 Numbers0689

@WikiRik done!

Numbers0689 avatar Mar 20 '25 12:03 Numbers0689