Skip to content

fix(isRgbColor): accept alpha values with more than 2 decimal digits#2793

Open
JSap0914 wants to merge 1 commit into
validatorjs:masterfrom
JSap0914:fix/2792-isrgbcolor-alpha-precision
Open

fix(isRgbColor): accept alpha values with more than 2 decimal digits#2793
JSap0914 wants to merge 1 commit into
validatorjs:masterfrom
JSap0914:fix/2792-isrgbcolor-alpha-precision

Conversation

@JSap0914

Copy link
Copy Markdown
Contributor

Summary

isRgbColor incorrectly rejected valid CSS rgba() colors where the alpha value had more than 2 decimal digits, or where alpha was 1.00 (trailing zeros on the boundary value 1).

Closes #2792

Bug

The old alpha sub-pattern (0?\.\d\d?|1(\.0)?|0(\.0)?) limited the decimal part to at most 2 digits and only allowed exactly one trailing zero on the boundary values:

isRgbColor('rgba(255,255,255,.123)') // false — wrong, should be true
isRgbColor('rgba(255,255,255,1.00)') // false — wrong, should be true
isRgbColor('rgba(0,0,0,0.123)')      // false — wrong, should be true
isRgbColor('rgba(5%,5%,5%,.321)')    // false — wrong, should be true

The CSS specification defines <alpha-value> as a CSS <number>, which imposes no limit on decimal precision.

Fix

Changed the alpha sub-pattern in both rgbaColor and rgbaColorPercent from:

(0?\.\d\d?|1(\.0)?|0(\.0)?)

to:

(0?\.\d+|1(\.0+)?|0(\.0+)?)

This allows any number of decimal digits after the dot and properly handles trailing zeros on the opaque (1) and transparent (0) boundary values.

Verification

  • Before fix (RED): npm test → 317 passing, 1 failing (validator.isRgbColor("rgba(255,255,255,.123)") failed but should have passed)
  • After fix (GREEN): npm test → 318 passing

All existing tests continue to pass. Values outside the 0–1 range (rgba(0,0,0,2), rgba(0,0,0,1.5)) are still correctly rejected.

The alpha regex (0?\.\d\d?|1(\.0)?|0(\.0)?) limited alpha to at
most 2 decimal digits and did not allow trailing zeros on the boundary
values 1 and 0, causing valid CSS rgba() colors to be incorrectly
rejected:

  isRgbColor('rgba(255,255,255,.123)') // false — should be true
  isRgbColor('rgba(255,255,255,1.00)') // false — should be true
  isRgbColor('rgba(0,0,0,0.123)')      // false — should be true

The CSS specification defines <alpha-value> as a CSS <number>
(https://www.w3.org/TR/css-color-4/#typedef-alpha-value), which
imposes no limit on decimal precision.

Fix: widen the alpha sub-pattern to:
  (0?\.\d+|1(\.0+)?|0(\.0+)?)
so that any number of decimal digits after the point is accepted, and
both 1.0 and 1.00 (and similarly for 0) are valid.

Closes validatorjs#2792
Copilot AI review requested due to automatic review settings June 30, 2026 03:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a38f15b) to head (f46b338).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2793   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2587      2587           
  Branches       656       656           
=========================================
  Hits          2587      2587           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

isRgbColor: alpha values with more than 2 decimal digits or trailing zeros on '1' are incorrectly rejected

2 participants