ensureValidHandle function

void ensureValidHandle(
  1. String handle
)

Implementation

void ensureValidHandle(final String handle) {
  // check that all chars are boring ASCII
  if (!_handleAllowedCharsRegExp.hasMatch(handle)) {
    throw InvalidHandleError(
      'Disallowed characters in handle '
      '(ASCII letters, digits, dashes, periods only)',
    );
  }

  if (handle.length > 253) {
    throw InvalidHandleError('Handle is too long (253 chars max)');
  }

  final labels = handle.split('.');
  if (labels.length < 2) {
    throw InvalidHandleError('Handle domain needs at least two parts');
  }

  for (int i = 0; i < labels.length; i++) {
    final label = labels[i];

    if (label.isEmpty) {
      throw InvalidHandleError('Handle parts can not be empty');
    }

    if (label.length > 63) {
      throw InvalidHandleError('Handle part too long (max 63 chars)');
    }

    if (label.endsWith('-') || label.startsWith('-')) {
      throw InvalidHandleError(
        'Handle parts can not start or end with hyphens',
      );
    }

    if (i + 1 == labels.length && !_handleTldStartRegExp.hasMatch(label)) {
      throw InvalidHandleError(
        'Handle final component (TLD) must start with ASCII letter',
      );
    }
  }
}