public ISet <T> HasItems <T>([ValidatedNotNull] ISet <T> value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

            if (value.Count < 1)
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Collections_HasItemsFailed,
                          paramName,
                          optsFn);
            }

            return(value);
        }
        public IEnumerable <T> HasAny <T>([ValidatedNotNull, InstantHandle] IEnumerable <T> value, [NotNull] Func <T, bool> predicate, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

            if (!value.Any(predicate))
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Collections_Any_Failed,
                          paramName,
                          optsFn);
            }

            return(value);
        }
Exemple #3
0
        public T IsClass <T>([ValidatedNotNull] T param, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(param);
            }

            if (param == null)
            {
                throw ExceptionFactory.ArgumentNullException(
                          ExceptionMessages.Types_IsClass_Failed_Null,
                          paramName,
                          optsFn);
            }

            IsClass(param.GetType(), paramName, optsFn);

            return(param);
        }
        public IDictionary <TKey, TValue> ContainsKey <TKey, TValue>([ValidatedNotNull] IDictionary <TKey, TValue> value, TKey expectedKey, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

            if (!value.ContainsKey(expectedKey))
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Collections_ContainsKey_Failed.Inject(expectedKey),
                          paramName,
                          optsFn);
            }

            return(value);
        }
        public IDictionary <TKey, TValue> SizeIs <TKey, TValue>([ValidatedNotNull] IDictionary <TKey, TValue> value, long expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

            if (value.Count != expected)
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Count),
                          paramName,
                          optsFn);
            }

            return(value);
        }
        public T[] SizeIs <T>([ValidatedNotNull] T[] value, int expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

            if (value.Length != expected)
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, value.Length),
                          paramName,
                          optsFn);
            }

            return(value);
        }
        public static Param <T> IsInRange <T>(this Param <T> param, T min, T max) where T : struct, IComparable <T>
        {
            if (!Ensure.IsActive)
            {
                return(param);
            }

            if (param.Value.IsLt(min))
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.Comp_IsNotInRange_ToLow.Inject(param.Value, min));
            }

            if (param.Value.IsGt(max))
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.Comp_IsNotInRange_ToHigh.Inject(param.Value, max));
            }

            return(param);
        }
Exemple #8
0
        public static void IsClass(this Param <Type> param)
        {
            if (!Ensure.IsActive)
            {
                return;
            }

            if (param.Value == null)
            {
                throw ExceptionFactory.CreateForParamNullValidation(param,
                                                                    ExceptionMessages.Types_IsClass_Failed_Null);
            }

            if (param.Value == null ||
                !param.Value.GetTypeInfo().IsClass)
            {
                throw ExceptionFactory.CreateForParamValidation(param,
                                                                ExceptionMessages.Types_IsClass_Failed.Inject(param.Value.FullName));
            }
        }
Exemple #9
0
        public Type IsNotOfType([ValidatedNotNull] Type param, [NotNull] Type nonExpectedType, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(param);
            }

            Ensure.Any.IsNotNull(param, paramName, optsFn);
            Ensure.Any.IsNotNull(nonExpectedType, nameof(nonExpectedType));

            if (param == nonExpectedType)
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Types_IsNotOfType_Failed.Inject(nonExpectedType.FullName),
                          paramName,
                          optsFn);
            }

            return(param);
        }
        public static Param <string> HasLengthBetween(this Param <string> param, int minLength, int maxLength)
        {
            if (string.IsNullOrEmpty(param.Value))
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.EnsureExtensions_IsNotNullOrEmpty);
            }

            var length = param.Value.Length;

            if (length < minLength)
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.EnsureExtensions_IsNotInRange_ToShort.Inject(minLength, maxLength, length));
            }

            if (length > maxLength)
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.EnsureExtensions_IsNotInRange_ToLong.Inject(minLength, maxLength, length));
            }

            return(param);
        }
        public static Param <string> HasLengthBetween(this Param <string> param, int minLength, int maxLength)
        {
            if (param.Value == null)
            {
                throw ExceptionFactory.CreateForParamNullValidation(param, ExceptionMessages.Common_IsNotNull_Failed);
            }

            var length = param.Value.Length;

            if (length < minLength)
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.Strings_HasLengthBetween_Failed_ToShort.Inject(minLength, maxLength, length));
            }

            if (length > maxLength)
            {
                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.Strings_HasLengthBetween_Failed_ToLong.Inject(minLength, maxLength, length));
            }

            return(param);
        }
        public IEnumerable <T> SizeIs <T>([ValidatedNotNull, InstantHandle] IEnumerable <T> value, int expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

            var count = value.Count();

            if (count != expected)
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Collections_SizeIs_Failed.Inject(expected, count),
                          paramName,
                          optsFn);
            }

            return(value);
        }
Exemple #13
0
        public string HasLengthBetween([ValidatedNotNull] string value, int minLength, int maxLength, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName, optsFn);

            var length = value.Length;

            if (length < minLength)
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_HasLengthBetween_Failed_ToShort.Inject(minLength, maxLength, length), paramName, optsFn);
            }

            if (length > maxLength)
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_HasLengthBetween_Failed_ToLong.Inject(minLength, maxLength, length), paramName, optsFn);
            }

            return(value);
        }
Exemple #14
0
 private Throws()
 {
     InvalidOperationException = param => ExceptionFactory.CreateForInvalidOperation(param, ExceptionMessages.EnsureExtensions_InvalidOperationException);
 }