Beispiel #1
0
 public static Validation IsOfType <T>(this Validation validation, object theObject, string paramName)
 {
     if (theObject == null)
     {
         return(validation.AddException(ValidationException.IsRequired(paramName)));
     }
     if (theObject.GetType() != typeof(T))
     {
         return
             (validation.AddException(
                  new ValidationException($"{paramName} is not of type {typeof(T).Name}")));
     }
     return(validation);
 }
Beispiel #2
0
 public static Validation Contains <T>(this Validation validation, IEnumerable <T> items, Predicate <T> criteria,
                                       string message)
 {
     return(items.Any(x => criteria(x))
         ? validation
         : validation.AddException(new ValidationException(message)));
 }
Beispiel #3
0
 public static Validation IsWithinLength(this Validation validation, string theString, int maxLength,
                                         string paramName)
 {
     return(string.IsNullOrEmpty(theString) || theString.Length > maxLength
         ? validation.AddException(ValidationException.ExceedsMaximumLength(paramName))
         : validation);
 }
Beispiel #4
0
 public static Validation Contains <T>(this Validation validation, IEnumerable <T> items, T criteria,
                                       string message)
 {
     return(items.Any(x => Equals(x, criteria))
         ? validation
         : validation.AddException(new ValidationException(message)));
 }
Beispiel #5
0
 public static Validation IsTrue(this Validation validation, string theString,
                                 Func <string, bool> isValidCustomFunction, string exceptionMessage)
 {
     return(!isValidCustomFunction(theString)
         ? validation.AddException(ValidationException.IsNotTrue(exceptionMessage))
         : validation);
 }
Beispiel #6
0
 public static Validation IsNotEmpty <T>(this Validation validation, IEnumerable <T> theList, string paramName)
 {
     return(theList == null || !theList.Any()
         ? validation.AddException(
                ValidationException.FormattedError("{0} is not expected to be empty", paramName))
         : validation);
 }
Beispiel #7
0
 public static Validation LessThan <T>(this Validation validation, T theObject, T comparison, string paramName)
     where T : IComparable <T>
 {
     return(theObject.CompareTo(comparison) < 0
         ? validation
         : validation.AddException(new ValidationException(paramName)));
 }
Beispiel #8
0
 public static Validation GreaterThanOrEqual <T>(this Validation validation, T theObject, T comparison,
                                                 string paramName) where T : IComparable
 {
     return(theObject.CompareTo(comparison) >= 0
         ? validation
         : validation.AddException(new ValidationException(paramName)));
 }
Beispiel #9
0
 public static Validation IsMinimumLength(this Validation validation, string theValue, int minimumLength,
                                          string message)
 {
     return(string.IsNullOrEmpty(theValue) || theValue.Length < minimumLength
         ? validation.AddException(ValidationException.MustBePhoneNumber(message))
         : validation);
 }
Beispiel #10
0
        public static Validation IsLengthOf(this Validation validation, string theString, int expectedLength,
                                            string paramName)
        {
            var adds = expectedLength > 1 ? "s" : "";

            return(string.IsNullOrEmpty(theString) || theString.Length != expectedLength
                ? validation.AddException(
                       ValidationException.IsNotLengthOf(
                           $"{paramName} needs to be exactly {expectedLength} character{adds} long."))
                : validation);
        }
Beispiel #11
0
 public static Validation GreaterThan(this Validation validation, DateTime?theObject, DateTime?comparison,
                                      string paramName)
 {
     if (!theObject.HasValue || !comparison.HasValue)
     {
         return(validation);
     }
     return(theObject.Value.CompareTo(comparison.Value) > 0
         ? validation
         : validation.AddException(new ValidationException(paramName)));
 }
Beispiel #12
0
        public static Validation IsDate(this Validation validation, string date, string paramName, string format = null)
        {
            var success = format == null
                ? DateTime.TryParse(date, out _)
                : DateTime.TryParseExact(date, format, DateTimeFormatInfo.CurrentInfo,
                                         DateTimeStyles.AssumeLocal, out _);

            if (!success)
            {
                validation.AddException(new ValidationException($"{paramName} is not a valid Date"));
            }

            return(validation);
        }
Beispiel #13
0
 public static Validation IsPhoneNumber(this Validation validation, string theValue, string paramName)
 {
     return(string.IsNullOrEmpty(theValue) || !PhoneNumberRegex.IsMatch(theValue)
         ? validation.AddException(ValidationException.MustBePhoneNumber(paramName))
         : validation);
 }
Beispiel #14
0
 public static Validation IsEmail(this Validation validation, string theValue, string paramName)
 {
     return(string.IsNullOrEmpty(theValue) || !EmailRegex.IsMatch(theValue)
         ? validation.AddException(ValidationException.MustBeEmail(paramName))
         : validation);
 }
Beispiel #15
0
 public static Validation IsEqual <T>(this Validation validation, T left, T right, string message)
 {
     return(Equals(left, right) ? validation : validation.AddException(new ValidationException(message)));
 }
Beispiel #16
0
 public static Validation IsEmpty <T>(this Validation validation, IEnumerable <T> theItems, string paramName)
 {
     return(theItems != null && theItems.Any()
         ? validation.AddException(ValidationException.FormattedError("{0} should be empty", paramName))
         : validation);
 }
Beispiel #17
0
 public static Validation ForEach <T>(this Validation validation, IEnumerable <T> items, Func <Validation, T, Validation> action)
 {
     return(items == null
         ? validation.AddException(ValidationException.ListIsNullOrEmpty("items"))
         : items.Aggregate(validation, action));
 }
Beispiel #18
0
 public static Validation IsNotEmpty(this Validation validation, string theString, string paramName)
 {
     return(string.IsNullOrEmpty(theString)
         ? validation.AddException(ValidationException.IsRequired(paramName))
         : validation);
 }
Beispiel #19
0
 public static Validation IsNotEqual <T>(this Validation validation, T theObject, T comparison, string paramName)
 {
     return(Equals(theObject, comparison)
         ? validation.AddException(ValidationException.IsRequired(paramName))
         : validation);
 }
Beispiel #20
0
 public static Validation IsTrue(this Validation validation, bool val, string message)
 {
     return(val ? validation : validation.AddException(new ValidationException(message)));
 }
Beispiel #21
0
 public static Validation HasLength(this Validation validation, string theValue, int length, string message)
 {
     return((theValue ?? "").Length != length
         ? validation.AddException(ValidationException.MustBeOfLength(message))
         : validation);
 }
Beispiel #22
0
 public static Validation IsEmpty(this Validation validation, string theString, string paramName)
 {
     return(!string.IsNullOrEmpty(theString)
         ? validation.AddException(ValidationException.FormattedError("{0} should be empty", paramName))
         : validation);
 }
Beispiel #23
0
 public static Validation Throw(this Validation validation, string message)
 {
     return(validation.AddException(new ValidationException(message)).Check());
 }
Beispiel #24
0
 public static Validation IsNotNull(this Validation validation, DateTime theObject, string paramName)
 {
     return(Equals(theObject, DateTime.MinValue)
         ? validation.AddException(ValidationException.IsRequired(paramName))
         : validation);
 }
Beispiel #25
0
 public static Validation IsFalse(this Validation validation, bool?val, string message)
 {
     return(val.HasValue && val.Value == false? validation : validation.AddException(new ValidationException(message)));
 }
Beispiel #26
0
 public static Validation IsNumeric(this Validation validation, object theValue, string paramName)
 {
     return(string.IsNullOrEmpty(theValue?.ToString()) || !NumericRegex.IsMatch(theValue.ToString())
         ? validation.AddException(ValidationException.MustBeNumeric(paramName))
         : validation);
 }
Beispiel #27
0
 public static Validation IsNotNull(this Validation validation, decimal theObject, string paramName)
 {
     return(theObject == 0 ? validation.AddException(ValidationException.IsRequired(paramName)) : validation);
 }
Beispiel #28
0
 public static Validation HasItems <T>(this Validation validation, IEnumerable <T> items, int count, string message)
 {
     return((items != null && items.Count() == count)
             ? validation
             : validation.AddException(new ValidationException(message)));
 }
Beispiel #29
0
 public static Validation IsNotNull <T>(this Validation validation, T?theObject, string paramName)
     where T : struct
 {
     return(theObject == null?validation.AddException(ValidationException.IsRequired(paramName)) : validation);
 }
Beispiel #30
0
 public static Validation HasExactly <T>(this Validation validation, IEnumerable <T> items, int expected, string message)
 {
     return((items != null && items.Count() == expected)
         ? validation
         : validation.AddException(new ValidationException(message)));
 }