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); }
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); }
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)); }
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); } }