fix: allow .tld for user_domains_text & user_domains

This commit is contained in:
divocat
2025-10-07 19:19:10 +03:00
parent ae4a3781e6
commit 72b2a34af9
4 changed files with 38 additions and 5 deletions

View File

@@ -29,6 +29,13 @@ export const invalidDomains = [
['Too long domain (>253 chars)', Array(40).fill('abcdef').join('.') + '.com'],
];
export const dotTLDTests = [
['Dot TLD allowed (.net)', '.net', true, true],
['Dot TLD not allowed (.net)', '.net', false, false],
['Invalid with double dot', '..net', true, false],
['Invalid single word TLD (net)', 'net', true, false],
];
describe('validateDomain', () => {
describe.each(validDomains)('Valid domain: %s', (_desc, domain) => {
it(`returns valid=true for "${domain}"`, () => {
@@ -43,4 +50,14 @@ describe('validateDomain', () => {
expect(res.valid).toBe(false);
});
});
describe.each(dotTLDTests)(
'Dot TLD toggle: %s',
(_desc, domain, allowDotTLD, expected) => {
it(`"${domain}" with allowDotTLD=${allowDotTLD} → valid=${expected}`, () => {
const res = validateDomain(domain, allowDotTLD);
expect(res.valid).toBe(expected);
});
},
);
});

View File

@@ -1,8 +1,18 @@
import { ValidationResult } from './types';
export function validateDomain(domain: string): ValidationResult {
export function validateDomain(
domain: string,
allowDotTLD = false
): ValidationResult {
const domainRegex =
/^(?=.{1,253}(?:\/|$))(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+(?:[a-zA-Z]{2,}|xn--[a-zA-Z0-9-]{1,59}[a-zA-Z0-9])(?:\/[^\s]*)?$/;
/^(?=.{1,253}(?:\/|$))(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+(?:[a-zA-Z]{2,}|xn--[a-zA-Z0-9-]{1,59}[a-zA-Z0-9])(?:\/[^\s]*)?$/;
if (allowDotTLD) {
const dotTLD = /^\.[a-zA-Z]{2,}$/;
if (dotTLD.test(domain)) {
return { valid: true, message: _('Valid') };
}
}
if (!domainRegex.test(domain)) {
return { valid: false, message: _('Invalid domain address') };

View File

@@ -455,7 +455,7 @@ function createConfigSection(section) {
return true;
}
const validation = main.validateDomain(value);
const validation = main.validateDomain(value, true);
if (validation.valid) {
return true;
@@ -493,7 +493,7 @@ function createConfigSection(section) {
);
}
const { valid, results } = main.bulkValidate(domains, main.validateDomain);
const { valid, results } = main.bulkValidate(domains, row => main.validateDomain(row, true));
if (!valid) {
const errors = results

View File

@@ -14,8 +14,14 @@ function validateIPV4(ip) {
}
// src/validators/validateDomain.ts
function validateDomain(domain) {
function validateDomain(domain, allowDotTLD = false) {
const domainRegex = /^(?=.{1,253}(?:\/|$))(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+(?:[a-zA-Z]{2,}|xn--[a-zA-Z0-9-]{1,59}[a-zA-Z0-9])(?:\/[^\s]*)?$/;
if (allowDotTLD) {
const dotTLD = /^\.[a-zA-Z]{2,}$/;
if (dotTLD.test(domain)) {
return { valid: true, message: _("Valid") };
}
}
if (!domainRegex.test(domain)) {
return { valid: false, message: _("Invalid domain address") };
}