Files
Auto-GPT/frontend/lib/utils/uri_utility.dart
hunteraraujo 52c0929e3b Add UriUtility Class for URL Validation
This commit introduces the UriUtility class to the codebase. This utility class contains a static method, isURL, used for validating URLs. The method checks for common URL validation criteria, ensuring that the input string adheres to the standard URL format. It verifies that the URL is non-empty, does not contain invalid characters, is not a mailto link, can be parsed as a URI, and has both a scheme and a host. Additional validations include checks on user info and port numbers.

Changes made:
- Created UriUtility class with isURL static method for URL validation.
- Included validations for empty strings, invalid characters, mailto links, scheme, host, user info, and port numbers.
- The utility will be utilized in forms and other areas where URL validation is necessary.

This enhancement improves the robustness of URL validation across the application, ensuring that only valid URLs are processed and stored.
2023-09-27 15:10:14 -07:00

41 lines
1.0 KiB
Dart

class UriUtility {
static bool isURL(String url) {
// Validate if the URL string is empty, or contains spaces or invalid characters
if (url.isEmpty || RegExp(r'[\s<>]').hasMatch(url)) {
return false;
}
// Check for 'mailto:' at the start of the URL
if (url.startsWith('mailto:')) {
return false;
}
// Try to parse the URL, return false if parsing fails
Uri? uri;
try {
uri = Uri.parse(url);
} catch (e) {
return false;
}
// Validate the URL has a scheme (protocol) and host
if (uri.scheme.isEmpty || uri.host.isEmpty) {
return false;
}
// Check if the URI has user info, which is not a common case for a valid HTTP/HTTPS URL
if (uri.hasAuthority &&
(uri.userInfo.isEmpty ||
uri.userInfo.contains(':') && uri.userInfo.split(':').length > 2)) {
return false;
}
// Validate the port number if exists
if (uri.hasPort && (uri.port <= 0 || uri.port > 65535)) {
return false;
}
return true;
}
}