From 569a2a2f888665a4ca0aacce7ff5ead38f954c70 Mon Sep 17 00:00:00 2001 From: xxiaoxiong Date: Fri, 29 May 2026 15:55:47 +0800 Subject: [PATCH] fix(isFQDN): skip numeric TLD check when require_tld is false 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 #2425 --- src/lib/isFQDN.js | 4 ++-- test/validators/isFQDN.test.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index eb6928fda..39b0b63dc 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -43,8 +43,8 @@ export default function isFQDN(str, options) { } } - // reject numeric TLDs - if (!options.allow_numeric_tld && /^\d+$/.test(tld)) { + // reject numeric TLDs (only when a TLD is actually required) + if (options.require_tld && !options.allow_numeric_tld && /^\d+$/.test(tld)) { return false; } diff --git a/test/validators/isFQDN.test.js b/test/validators/isFQDN.test.js index 134bab005..819081a6f 100644 --- a/test/validators/isFQDN.test.js +++ b/test/validators/isFQDN.test.js @@ -22,5 +22,18 @@ describe('isFQDN', () => { invalid: [ ], }); + test({ + validator: 'isFQDN', + args: [{ require_tld: false }], + valid: [ + 'google.com', + 'localhost', + '192', + '10', + ], + invalid: [ + '', + ], + }); }); });