mobile_nebula/lib/validators/dnsValidator.dart

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
790 B
Dart
Raw Normal View History

2020-07-27 20:43:58 +00:00
// Inspired by https://github.com/suragch/string_validator/blob/master/lib/src/validator.dart
bool dnsValidator(str, {requireTld = true, allowUnderscore = false}) {
if (str == null) {
return false;
}
List parts = str.split('.');
if (requireTld) {
var tld = parts.removeLast();
if (parts.isEmpty || !RegExp(r'^[a-z]{2,}$').hasMatch(tld)) {
return false;
}
}
for (var part, i = 0; i < parts.length; i++) {
part = parts[i];
if (allowUnderscore) {
if (part.indexOf('__') >= 0) {
return false;
}
}
if (!RegExp(r'^[a-z\\u00a1-\\uffff0-9-]+$').hasMatch(part)) {
return false;
}
if (part[0] == '-' || part[part.length - 1] == '-' || part.indexOf('---') >= 0) {
return false;
}
}
return true;
}