isValid method

  1. @override
bool isValid(
  1. dynamic value
)
override

Implementation

@override
bool isValid(dynamic value) {
  if (value is! String || value.isEmpty) return false;
  if (value.length > 253) return false;
  if (value.contains('/')) return false;
  if (value.contains(':')) return false;
  if (_allowList.isEmpty) return true;
  for (final allowedHostname in _allowList) {
    if (value == allowedHostname || allowedHostname == '*') return true;
    if (allowedHostname.contains('*')) {
      final allowedSections = allowedHostname.split('.');
      final valueSections = value.split('.');

      if (allowedSections.length == valueSections.length) {
        var matchesAmount = 0;
        for (var sectionIndex = 0;
            sectionIndex < allowedSections.length;
            sectionIndex++) {
          final allowedsection = allowedSections[sectionIndex];
          if (allowedsection == '*' ||
              allowedsection == valueSections[sectionIndex]) {
            matchesAmount++;
          } else {
            break;
          }
        }

        if (matchesAmount == allowedSections.length) {
          return true;
        }
      }
    }
  }
  return false;
}