mirror of
https://github.com/itdoginfo/podkop.git
synced 2026-05-24 21:27:41 +03:00
22 lines
686 B
TypeScript
22 lines
686 B
TypeScript
import { ValidationResult } from './types';
|
|
|
|
export function validateDomain(domain: string): 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]*)?$/;
|
|
|
|
if (!domainRegex.test(domain)) {
|
|
return { valid: false, message: 'Invalid domain address' };
|
|
}
|
|
|
|
const hostname = domain.split('/')[0];
|
|
const parts = hostname.split('.');
|
|
|
|
const atLeastOneInvalidPart = parts.some((part) => part.length > 63);
|
|
|
|
if (atLeastOneInvalidPart) {
|
|
return { valid: false, message: 'Invalid domain address' };
|
|
}
|
|
|
|
return { valid: true, message: 'Valid' };
|
|
}
|