Url Validator

The UrlValidator is in Litium.Web namespace. The purpose of the validator is to validate the url path for pages, categories and products. This is to ensure that they contain only approved characters that can be used without encoding problems when linking to the content. 

  • UrlValidator 
    /// <summary>
    /// Url validator is used to validate that url has a known good format.
    /// </summary>
    [Service(ServiceType = typeof(UrlValidator))]
    public abstract class UrlValidator
        /// <summary>
        /// Validates the url.
        /// </summary>
        /// <param name="cultureInfo">The culture info for the url.</param>
        /// <param name="url">The url to validate.</param>
        /// <returns><see cref="UrlValidatorResult"/> with the result of the validation.</returns>
        public abstract UrlValidatorResult Validate([NotNull] CultureInfo cultureInfo, [NotNull] string url);
  • UrlValidatorImpl is the default implementation of the UrlValidator, which is based on UrlValidatorConfig to perfom the check for invalid characters and invalid words for a given url.
  • UrlValidatorConfig class has these three collections for invalid characters, words and extensions:
 public static ISet<char> DefaultInvalidCharacters { get; } = new ReadOnlyHashSet<char>(
            "/\\.,;: +*'§½\"<>|!?$&=@#%{}^~[]`´¨¤—–‘’“”\t\r\n".ToCharArray()
            .Concat(new[] { '\u2007', '\u00A0', '\u0020', '\u202F', '\u2060', '\uFEFF' }));

        /// <summary>
        /// Default invalid characters contains all the characters that is configured by the solution to be invalid for urls.
        /// </summary>
        public static ISet<string> DefaultInvalidWords { get; } = new ReadOnlyHashSet<string>(new[] {
        }, StringComparer.OrdinalIgnoreCase);

        /// <summary>
        /// Default file extensions that is invalid to have in url's.
        /// </summary>
        public static ISet<string> DefaultInvalidExtensions { get; } = new ReadOnlyHashSet<string>(new[] {
        }, StringComparer.OrdinalIgnoreCase);

There are three more collections to add new exclusions to the default collections above: InvalidCharactersInvalidWordsInvalidExtensions.

The UrlValidatior can be used anywhere to validate urls, currently in Litium it is used in url validation rules for Base Product, Category, Variant, DraftPage and Page.