Skip to content

fix(isFQDN): skip numeric-label check when require_tld is false#2726

Open
xxiaoxiong wants to merge 1 commit into
validatorjs:masterfrom
xxiaoxiong:fix/isFQDN-numeric-tld-require-tld-false
Open

fix(isFQDN): skip numeric-label check when require_tld is false#2726
xxiaoxiong wants to merge 1 commit into
validatorjs:masterfrom
xxiaoxiong:fix/isFQDN-numeric-tld-require-tld-false

Conversation

@xxiaoxiong
Copy link
Copy Markdown

Problem

When require_tld: false, single-label names that are all-numeric (e.g. 192, 10) are incorrectly rejected by isFQDN.

isFQDN('192', { require_tld: false })  // returns false  ❌ (should be true)
isFQDN('10',  { require_tld: false })  // returns false  ❌ (should be true)

Root cause: The check at line 47 (outside the require_tld block) was unconditionally rejecting any label whose last dot-segment consists solely of digits:

// before
if (!options.allow_numeric_tld && /^\d+$/.test(tld)) {
  return false;
}

When require_tld: true this is fine — TLDs like .123 are always disallowed. But when require_tld: false there is no TLD at all; the last segment is just the only label, and a purely numeric label should be permitted.

The same constraint is already enforced inside the require_tld block (line 36 rejects non-alphabetic TLDs), so the outer check is redundant for the require_tld: true case and harmful for the require_tld: false case.

Fix

Guard the check with options.require_tld:

// after
if (options.require_tld && !options.allow_numeric_tld && /^\d+$/.test(tld)) {
  return false;
}

Tests

Added test cases for { require_tld: false } in test/validators/isFQDN.test.js covering:

  • Standard multi-label FQDNs (google.com) still pass
  • Non-numeric single labels (localhost) pass
  • Numeric single labels (192, 10) now correctly pass

Fixes #2425

When require_tld is false, single-label names like '192' or '10' should
be valid since there is no TLD to evaluate. The existing check at line
47 (outside the require_tld block) was unconditionally rejecting any
string whose last dot-segment is all digits, even when no TLD is
required at all.

The same constraint is already enforced inside the require_tld block
(line 36 rejects non-alphabetic TLDs), so wrapping the redundant check
in the same require_tld condition has no effect when require_tld is true
and correctly allows numeric single-labels when require_tld is false.

Fixes validatorjs#2425
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.

isFQDN function continues executing with disabled require_tld option

1 participant