Example #1
0
        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);
        }
Example #2
0
        public static Param <Type> IsClass(this Param <Type> param)
        {
            if (param.Value == null)
            {
                throw ExceptionFactory.CreateForParamValidation(param,
                                                                ExceptionMessages.EnsureExtensions_IsNotClass_WasNull);
            }

#if vNext || PCL
            if (!param.Value.GetTypeInfo().IsClass)
            {
                throw ExceptionFactory.CreateForParamValidation(param,
                                                                ExceptionMessages.EnsureExtensions_IsNotClass.Inject(param.Value.FullName));
            }
#else
            if (!param.Value.IsClass)
            {
                throw ExceptionFactory.CreateForParamValidation(param,
                                                                ExceptionMessages.EnsureExtensions_IsNotClass.Inject(param.Value.FullName));
            }
#endif
            return(param);
        }
Example #3
0
        public static Param <T> IsInRange <T>(this Param <T> param, T min, T max, Throws <T> .ExceptionFnConfig exceptionFn = null) where T : struct, IComparable <T>
        {
            if (param.Value.IsLt(min))
            {
                if (exceptionFn != null)
                {
                    throw exceptionFn(Throws <T> .Instance)(param);
                }

                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.EnsureExtensions_IsNotInRange_ToLow.Inject(param.Value, min));
            }

            if (param.Value.IsGt(max))
            {
                if (exceptionFn != null)
                {
                    throw exceptionFn(Throws <T> .Instance)(param);
                }

                throw ExceptionFactory.CreateForParamValidation(param, ExceptionMessages.EnsureExtensions_IsNotInRange_ToHigh.Inject(param.Value, max));
            }

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