urlAsync<T> static method

Future<T> urlAsync<T>(
  1. String url, {
  2. bool allowRelative = false,
  3. List<String>? allowedSchemes,
  4. List<String>? allowedDomains,
  5. bool normalize = true,
  6. bool checkDomainExists = false,
})

Asynchronously validates URLs with comprehensive format and domain checking.

Provides advanced URL validation including format verification, scheme restrictions, domain whitelisting, and optional DNS lookup. Return type is generic:

  • String: Returns validation message
  • bool: Returns true/false for validity
  • UrlValidationResult: Returns complete result with parsed URL data

Parameters:

  • url: The URL string to validate
  • allowRelative: Whether to allow relative URLs (default: false)
  • allowedSchemes: List of allowed URL schemes
  • allowedDomains: List of allowed domains
  • normalize: Whether to normalize the URL (default: true)
  • checkDomainExists: Whether to perform DNS lookup (default: false, can be slow) Validates a URL with optional DNS verification and format checks.

Parameters:

  • url: The URL to validate
  • allowRelative: Whether to allow relative URLs (default: false)
  • allowedSchemes: List of allowed URL schemes (e.g. 'http', 'https')
  • allowedDomains: List of allowed domains (e.g. 'example.com')
  • normalize: Whether to normalize the URL (default: true)
  • checkDomainExists: Whether to verify domain exists via DNS (default: false)

Generic type T can be:

  • String: Returns error message or 'URL is valid'
  • bool: Returns true/false
  • UrlValidationResult: Returns detailed validation result object

Example:

// Simple boolean check
final isValid = await SahihValidator.urlAsync<bool>('https://example.com');

// Detailed result
final result = await SahihValidator.urlAsync<UrlValidationResult>(
  'https://example.com/path',
  allowedDomains: ['example.com']
);

Implementation

static Future<T> urlAsync<T>(
  String url, {
  bool allowRelative = false,
  List<String>? allowedSchemes,
  List<String>? allowedDomains,
  bool normalize = true,
  bool checkDomainExists = false,
}) async {
  return await isValidUrlAsync<T>(
    url,
    allowRelative: allowRelative,
    allowedSchemes: allowedSchemes,
    allowedDomains: allowedDomains,
    normalize: normalize,
    checkDomainExists: checkDomainExists,
  );
}