Пример #1
0
 /// <summary>
 /// Checks if the property's value ends with the given string value.
 /// <para>Supported Data Types: Strings.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="token">The string value that the property's value should ends with.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator EndsWith(this Validator validator, string token, string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (!value.ToString().EndsWith(token))
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.EndsWith(token));
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.EndsWith(token));
     }
     return(validator);
 }
Пример #2
0
 /// <summary>
 /// Validates the length of the property's value using the given minimum length.
 /// <para>Supported Data Types: Strings.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="minLength">The minimum length.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator MinLength(this Validator validator, long minLength, string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (value.ToString().Length < minLength)
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.MinLength(minLength));
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.MinLength(minLength));
     }
     return(validator);
 }
Пример #3
0
 /// <summary>
 /// Checks if the property's value is not more than the given maximum value.
 /// <para>Supported Data Types: Doubles, Decimals, Floats, Singles,
 /// Longs, Integers, Shorts and Bytes.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="maxValue">The maximum value.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator Max(this Validator validator, double maxValue, string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (double.Parse(value.ToString()) > maxValue)
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.Max(maxValue));
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.Max(maxValue));
     }
     return(validator);
 }
Пример #4
0
 /// <summary>
 /// Checks if the property's value matches the given pattern.
 /// <para>Supported Data Types: Strings.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="pattern">The pattern that the property's value should match.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator Matches(this Validator validator, string pattern, string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (!new Regex(pattern).IsMatch(value.ToString()))
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.Matches(pattern));
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.Matches(pattern));
     }
     return(validator);
 }
Пример #5
0
 /// <summary>
 /// Checks if the property's value is a valid ip address.
 /// <para>Supported Data Types: Strings.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator IsIP(this Validator validator, string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (!IPAddress.TryParse(value.ToString(), out var iPAddress))
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.IsIP());
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.IsIP());
     }
     return(validator);
 }
Пример #6
0
 /// <summary>
 /// Checks if the property's value is not lower than the given minimum value.
 /// <para>Supported Data Types: TimeSpans.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="minTime">The minimum time.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator Min(this Validator validator, TimeSpan minTime,
                             string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (TimeSpan.Parse(value.ToString()) < minTime)
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.Min(minTime));
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.Min(minTime));
     }
     return(validator);
 }
Пример #7
0
        /// <summary>
        /// Checks if the property's value contains at least one of the given string values.
        /// <para>Supported Data Types: Strings.</para>
        /// </summary>

        /// <param name="validator">The validator to add more validations or sanitization.</param>
        /// <param name="errorMessage">An optional validation error message.</param>
        /// <param name="tokens">The values that the property's value should contains one at least.</param>
        /// <returns>A Validator.</returns>
        public static Validator Contains(this Validator validator, string errorMessage = null
                                         , params string[] tokens)
        {
            try
            {
                var value = validator.GetValue();
                if (!validator.IsOptional || value != null)
                {
                    if (!tokens.Any(token => value.ToString().Contains(token)))
                    {
                        validator.AddError(errorMessage ?? DefaultErrorMessages.Contains(tokens));
                    }
                }
            }
            catch (Exception)
            {
                validator.AddError(errorMessage ?? DefaultErrorMessages.Contains(tokens));
            }
            return(validator);
        }
Пример #8
0
        /// <summary>
        /// Checks if the property's value is a valid ip address for the given ip version.
        /// <para>Supported Data Types: Strings.</para>
        /// </summary>
        /// <param name="validator">The validator to add more validations or sanitization.</param>
        /// <param name="ipVersion">The version of the ip address.</param>
        /// <param name="errorMessage">An optional validation error message.</param>
        /// <returns>A Validator.</returns>
        public static Validator IsIP(this Validator validator, IPVersion ipVersion, string errorMessage = null)
        {
            var version = ipVersion == IPVersion.IPv4 ? "v4" : "v6";

            try
            {
                var value = validator.GetValue();
                if (!validator.IsOptional || value != null)
                {
                    if (
                        // is a valid ip address
                        !IPAddress.TryParse(value.ToString(), out var iPAddress)
                        ||
                        // is a valid ipv4
                        (
                            ipVersion == IPVersion.IPv4 &&
                            !Regex.IsMatch(value.ToString(),
                                           @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}" +
                                           @"([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
                                           , RegexOptions.IgnoreCase
                                           )
                        )
                        ||
                        // is a valid ipv6
                        (
                            ipVersion == IPVersion.IPv6 && iPAddress.AddressFamily != AddressFamily.InterNetworkV6
                        )
                        )
                    {
                        validator.AddError(errorMessage ?? DefaultErrorMessages.IsIP(version));
                    }
                }
            }
            catch (Exception)
            {
                validator.AddError(errorMessage ?? DefaultErrorMessages.IsIP(version));
            }
            return(validator);
        }
Пример #9
0
 /// <summary>
 /// Checks if the property's value is in a given range
 /// <para>Supported Data Types: DateTimes.</para>
 /// </summary>
 /// <param name="validator">The validator to add more validations or sanitization.</param>
 /// <param name="minTime">The minimum dateTime.</param>
 /// <param name="maxTime">The maximum dateTime.</param>
 /// <param name="errorMessage">An optional validation error message.</param>
 /// <returns>A Validator.</returns>
 public static Validator IsInRange(this Validator validator, DateTime minDateTime,
                                   DateTime maxDateTime, string errorMessage = null)
 {
     try
     {
         var value = validator.GetValue();
         if (!validator.IsOptional || value != null)
         {
             if (Convert.ToDateTime(value.ToString()) < minDateTime ||
                 Convert.ToDateTime(value.ToString()) > maxDateTime
                 )
             {
                 validator.AddError(errorMessage ?? DefaultErrorMessages.IsInRange(minDateTime, maxDateTime));
             }
         }
     }
     catch (Exception)
     {
         validator.AddError(errorMessage ?? DefaultErrorMessages.IsInRange(minDateTime, maxDateTime));
     }
     return(validator);
 }