Esempio n. 1
0
        public static bool IsMatchedWith(this ValidationFormat format, string value)
        {
            if (format.Expression.HasValue())
            {
                return(Regex.IsMatch(value, format.Expression));
            }

            if (format.Function != null)
            {
                return(format.Function(value));
            }

            return(false);
        }
Esempio n. 2
0
        private static bool IsInvalidLength <TValue>(ValidationFormat <TValue> format, TValue value)
        {
            if (format.MinLength.HasValue && value.ToString().Length < format.MinLength.Value)
            {
                return(true);
            }

            if (format.MaxLength.HasValue && value.ToString().Length > format.MaxLength.Value)
            {
                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        public static bool Matches <TValue>(this ValidationFormat <TValue> format, TValue value)
        {
            if (format.Function.Exists())
            {
                return(format.Function(value));
            }

            if (IsInvalidLength(format, value))
            {
                return(false);
            }

            return(Regex.IsMatch(value.ToString(), format.Expression));
        }
Esempio n. 4
0
        public static void GuardAgainstInvalid(this string value, ValidationFormat format, string parameterName,
                                               string errorMessage = null)
        {
            format.GuardAgainstNull(nameof(format));
            parameterName.GuardAgainstNullOrEmpty(nameof(parameterName));

            var isMatch = format.IsMatchedWith(value);

            if (!isMatch)
            {
                if (errorMessage.HasValue())
                {
                    throw new ArgumentOutOfRangeException(parameterName, errorMessage);
                }
                throw new ArgumentOutOfRangeException(parameterName);
            }
        }