isUrl function

IValidator isUrl({
  1. bool strict = false,
  2. String? message,
})

Validates that the String is a valid URL. By it uses non-strict validation (like "example.com").

If you want to enforce strict validation (must include scheme), set strict to true or use isStrictUrl.

Usage Examples:

final urlValidator = isUrl();
urlValidator.validate("https://example.com");     // Valid
urlValidator.validate("example.com");             // Valid (non-strict)
urlValidator.validate("not-a-url");               // Invalid

// Strict validation (requires scheme)
final strictUrl = isUrl(strict: true);
strictUrl.validate("https://example.com");        // Valid
strictUrl.validate("example.com");                // Invalid

// Or use the convenience method
final strictUrl2 = isStrictUrl();

Implementation

IValidator isUrl({bool strict = false, String? message}) {
  return isString() &
      validator(
        (value) {
          return strict
              ? Uri.tryParse(value)?.isAbsolute ?? false
              : Uri.tryParse(value) != null;
        },
        (value) => Expectation(
          message: message ?? 'a valid URL',
          value: value,
          code: ExpectationCodes.valueFormatInvalid,
          data: {'format': 'url'},
        ),
      );
}