isValid static method

bool isValid(
  1. String value
)

Returns true if value is a valid IDN hostname (best-effort check).

Both ASCII labels and Unicode labels are accepted. The total hostname length must be ≤ 253 characters. Each label must be ≤ 63 characters. Labels must not start or end with a hyphen.

Implementation

static bool isValid(String value) {
  if (value.isEmpty) return false;

  // Trailing dots are rejected (RFC 1123 host names, not DNS zone-file FQDNs).
  if (value.endsWith('.')) return false;

  // Total length must not exceed 253 characters.
  if (value.length > 253) return false;

  final labels = value.split('.');

  for (final label in labels) {
    if (label.isEmpty) return false;
    if (label.length > 63) return false;

    // Try the ASCII label pattern first.
    if (_asciiLabel.hasMatch(label)) continue;

    // For labels containing non-ASCII characters, apply structural rules:
    // must not start or end with a hyphen, and every character must be a
    // Unicode letter (L), Unicode digit (N), ASCII alphanumeric, or hyphen.
    // NOTE: Dart's RegExp does not support \p{L}/\p{N} Unicode properties
    // natively. We check by rejecting controls and known-invalid characters
    // rather than by allow-listing, then enforce the structural hyphen rule.
    if (!_isValidUnicodeLabel(label)) return false;
  }

  return true;
}