Skip to content

fix(isCreditCard): anchor Mastercard regex alternation#2718

Open
leno23 wants to merge 1 commit into
validatorjs:masterfrom
leno23:fix/mastercard-regex-anchoring-2717
Open

fix(isCreditCard): anchor Mastercard regex alternation#2718
leno23 wants to merge 1 commit into
validatorjs:masterfrom
leno23:fix/mastercard-regex-anchoring-2717

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 25, 2026

Summary

  • Fix Mastercard regex so ^ and $ bind to the full pattern, not only one branch of the alternation
  • Reject partial strings like 5108 that previously matched ^5[1-5][0-9]{2}

Problem

Due to | precedence, the old pattern was parsed as (^5[1-5][0-9]{2}) | ((222...)[0-9]{12}$), allowing any string starting with 51XX55XX regardless of length.

Test plan

  • Added invalid cases: 5108, 5108foo
  • Added valid case: 5105105105105100
  • npm test — 318 passing

Fixes #2717

Made with Cursor

Wrap the Mastercard pattern branches in a non-capturing group so ^ and $
apply to the full card number, rejecting partial strings like "5108".

Fixes validatorjs#2717

Co-authored-by: Cursor <cursoragent@cursor.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7fdc788) to head (060e01a).

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

☔ View full report in Codecov by Sentry.
📢 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.

isCreditCard: Mastercard regex anchoring bug accepts 4-digit and partial strings

1 participant