UrlValidator class

UrlValidator extends TranslatedValidator to validate if a string represents a valid URL.

This validator checks if the URL matches the specified regex pattern and adheres to various URL validation rules.

Parameters:

  • protocols The list of allowed protocols (e.g., http, https, ftp). Defaults to 'http', 'https', 'ftp'.
  • requireTld Whether a top-level domain (TLD) is required. Defaults to true.
  • requireProtocol Whether the protocol is required. Defaults to false.
  • allowUnderscore Whether underscores are allowed in the URL. Defaults to false.
  • hostWhitelist A list of valid hostnames that are explicitly allowed.
  • hostBlacklist A list of invalid hostnames that are explicitly disallowed.
  • regex The regular expression used to validate the URL format. If provided, overrides the default URL regex.
  • errorText The error message returned if the validation fails.
  • checkNullOrEmpty Whether to check if the value is null or empty.
Inheritance

Constructors

UrlValidator.new({List<String> protocols = const <String>['http', 'https', 'ftp'], bool requireTld = true, bool requireProtocol = false, bool allowUnderscore = false, List<String> hostWhitelist = const <String>[], List<String> hostBlacklist = const <String>[], RegExp? regex, String? errorText, bool checkNullOrEmpty = true})
Constructor for the URL validator.

Properties

allowUnderscore bool
Whether underscores are allowed in the URL.
final
checkNullOrEmpty bool
Whether to check if the value is null or empty.
finalinherited
errorText String
The error message returned if the value is invalid.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
hostBlacklist List<String>
A list of invalid hostnames that are explicitly disallowed.
final
hostWhitelist List<String>
A list of valid hostnames that are explicitly allowed.
final
protocols List<String>
The list of allowed protocols.
final
regex RegExp?
The regular expression used to validate the URL format.
final
requireProtocol bool
Whether the protocol is required.
final
requireTld bool
Whether a top-level domain (TLD) is required.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
translatedErrorText String
The translated error message returned if the value is invalid.
no setteroverride

Methods

isFQDN(String str, {bool requireTld = true, bool allowUnderscores = false}) bool
Check if the string str is a fully qualified domain name (e.g., domain.com).
isNullOrEmpty(String? valueCandidate) bool
Checks if the value is null or empty. Returns true if the value is null or empty, otherwise false. The value is considered empty if it is a String, Iterable, or Map and it is empty or contains only whitespace characters. If the value is not a String, Iterable, or Map, it is considered empty if it is null.
inherited
isURL(String? value, {List<String?> protocols = const <String?>['http', 'https', 'ftp'], bool requireTld = true, bool requireProtocol = false, bool allowUnderscore = false, List<String> hostWhitelist = const <String>[], List<String> hostBlacklist = const <String>[]}) bool
Check if the string value is a URL.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
shift<T>(List<T> l) → T
Remove and return the first element from a list.
toString() String
A string representation of this object.
inherited
validate(String? valueCandidate) String?
Validates the value and checks if it is null or empty.
inherited
validateValue(String valueCandidate) String?
Validates the value. Returns null if the value is valid, otherwise an error message. Call validate() instead of this method when using the validator.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited