Skip to content

fix: isHexColor doesn't validate correctly #2566

@TrNgTien

Description

@TrNgTien

Description

By passing the string is 6633FF that the regex to verify hex color can bypass this validation.

I saw the problem that the validate can be pass with the string about from the regex from the source base of this repository validator.js

Minimal code-snippet showcasing the problem

This is the code from validator.js, that maybe the root cause of this validation.

https://github.com/validatorjs/validator.js/blob/master/src/lib/isHexColor.js

import assertString from './util/assertString';

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);
  return hexcolor.test(str);
}

I think the regex should be implement like this

const hexcolor = /^#{1}([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i;

Expected behavior

It should validate the string 6633FF is false
image
image

Actual behavior

It validate the string 6633FF is a hex color string
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions