Пример #1
0
        public Type IsClass([ValidatedNotNull] Type 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);
            }

            if (!param.GetTypeInfo().IsClass)
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Types_IsClass_Failed.Inject(param.FullName),
                          paramName,
                          optsFn);
            }

            return(param);
        }
Пример #2
0
        public IEnumerable <T> SizeIs <T>([ValidatedNotNull, InstantHandle] IEnumerable <T> value, long expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

#if NETSTANDARD1_1
            var count = value.LongCount();

            if (count != expected)
#else
            var count = value.LongCount();

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

            return(value);
        }
Пример #3
0
        public bool IsTrue(bool value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

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

            return(value);
        }
Пример #4
0
        public T IsNotDefault <T>(T value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null) where T : struct
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (default(T).Equals(value))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.ValueTypes_IsNotDefault_Failed, paramName, optsFn);
            }

            return(value);
        }
Пример #5
0
        public T Is <T>([NotNull] T value, T expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null) where T : IComparable <T>
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (!value.IsEq(expected))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Comp_Is_Failed.Inject(value, expected), paramName, optsFn);
            }

            return(value);
        }
Пример #6
0
        public T IsNot <T>(T value, T expected, [NotNull] IComparer <T> comparer, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (value.IsEq(expected, comparer))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Comp_IsNot_Failed.Inject(value, expected), paramName, optsFn);
            }

            return(value);
        }
Пример #7
0
        public string IsNotEqualTo(string value, string expected, StringComparison comparison, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (StringEquals(value, expected, comparison))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_IsNotEqualTo_Failed.Inject(value, expected), paramName, optsFn);
            }

            return(value);
        }
Пример #8
0
        public string Matches([NotNull] string value, [NotNull] Regex match, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (!match.IsMatch(value))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_Matches_Failed.Inject(value, match), paramName, optsFn);
            }

            return(value);
        }
Пример #9
0
        public string IsNotEmpty(string value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (value?.Length == 0)
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_IsNotEmpty_Failed, paramName, optsFn);
            }

            return(value);
        }
Пример #10
0
        public string IsGuid([ValidatedNotNull] string value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (!Guid.TryParse(value, out _))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_IsGuid_Failed.Inject(value), paramName, optsFn);
            }

            return(value);
        }
Пример #11
0
        public string SizeIs([ValidatedNotNull] string value, int expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

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

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

            return(value);
        }
Пример #12
0
        public string IsNotNullOrEmpty([ValidatedNotNull] string value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

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

            if (string.IsNullOrEmpty(value))
            {
                throw ExceptionFactory.ArgumentException(ExceptionMessages.Strings_IsNotNullOrEmpty_Failed, paramName, optsFn);
            }

            return(value);
        }
Пример #13
0
        public Guid IsNotEmpty(Guid value, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            if (value.Equals(Guid.Empty))
            {
                throw ExceptionFactory.ArgumentException(
                          ExceptionMessages.Guids_IsNotEmpty_Failed,
                          paramName,
                          optsFn);
            }

            return(value);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
        }
Пример #16
0
        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);
        }
Пример #17
0
        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);
        }
Пример #18
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);
        }
Пример #19
0
        public T[] SizeIs <T>([ValidatedNotNull] T[] value, long expected, [InvokerParameterName] string paramName = Param.DefaultName, OptsFn optsFn = null)
        {
            if (!Ensure.IsActive)
            {
                return(value);
            }

            Ensure.Any.IsNotNull(value, paramName);

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

            return(value);
        }
Пример #20
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);
        }