using System; namespace EmbedIO.Utilities { /// /// Provides validation methods for method arguments. /// public static partial class Validate { /// /// Ensures that an argument is not . /// /// The type of the argument to validate. /// The name of the argument to validate. /// The value to validate. /// if not . /// is . public static T NotNull(string argumentName, T? value) where T : class => value ?? throw new ArgumentNullException(argumentName); /// /// Ensures that a argument is neither nor the empty string. /// /// The name of the argument to validate. /// The value to validate. /// if neither nor the empty string. /// is . /// is the empty string. public static string NotNullOrEmpty(string argumentName, string? value) { if (value == null) throw new ArgumentNullException(argumentName); if (value.Length == 0) throw new ArgumentException("String is empty.", argumentName); return value; } /// /// Ensures that a valid URL can be constructed from a argument. /// /// Name of the argument. /// The value. /// Specifies whether is a relative URL, absolute URL, or is indeterminate. /// Ensure that, if is an absolute URL, its scheme is either http or https. /// The string representation of the constructed URL. /// is . /// /// is not a valid URL. /// - or - /// is , is an absolute URL, /// and 's scheme is neither http nor https. /// /// public static string Url( string argumentName, string value, UriKind uriKind = UriKind.RelativeOrAbsolute, bool enforceHttp = false) { Uri uri; try { uri = new Uri(NotNull(argumentName, value), uriKind); } catch (UriFormatException e) { throw new ArgumentException("URL is not valid.", argumentName, e); } if (enforceHttp && uri.IsAbsoluteUri && uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) throw new ArgumentException("URL scheme is neither HTTP nor HTTPS.", argumentName); return uri.ToString(); } /// /// Ensures that a valid URL, either absolute or relative to the given , /// can be constructed from a argument and returns the absolute URL /// obtained by combining and . /// /// Name of the argument. /// The value. /// The base URI for relative URLs. /// Ensure that the resulting URL's scheme is either http or https. /// The string representation of the constructed URL. /// /// is . /// - or - /// is . /// /// /// is not an absolute URI. /// - or - /// is not a valid URL. /// - or - /// is , /// and the combination of and has a scheme /// that is neither http nor https. /// /// public static string Url(string argumentName, string value, Uri baseUri, bool enforceHttp = false) { if (!NotNull(nameof(baseUri), baseUri).IsAbsoluteUri) throw new ArgumentException("Base URI is not an absolute URI.", nameof(baseUri)); Uri uri; try { uri = new Uri(baseUri, new Uri(NotNull(argumentName, value), UriKind.RelativeOrAbsolute)); } catch (UriFormatException e) { throw new ArgumentException("URL is not valid.", argumentName, e); } if (enforceHttp && uri.IsAbsoluteUri && uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) throw new ArgumentException("URL scheme is neither HTTP nor HTTPS.", argumentName); return uri.ToString(); } } }