public static IPropertyValidator <T, TProperty> NotEmpty <T, TProperty>(this IPropertyValidator <T, TProperty> @this) => @this.Add(v => { object value = v.Value; bool b; var s = value as string; if (s != null) { b = string.IsNullOrWhiteSpace(s); } else { var enumerable = value as IEnumerable; if (enumerable != null) { b = !enumerable.Cast <object>().Any(); } else { b = Equals(value, default(TProperty)); } } return(b ? v.CreateFailureData("'{PropertyName}' should not be empty.") : null); });
public static IPropertyValidator <T, TProperty> NotNull <T, TProperty>(this IPropertyValidator <T, TProperty> @this) => @this.Add(v => { object value = v.Value; return(value == null ? v.CreateFailureData("'{PropertyName}' must not be empty.") : null); });
public static IPropertyValidator <T, TProperty> NotEmpty <T, TProperty>(this IPropertyValidator <T, TProperty> @this, Func <string> message = null) => @this.Add(v => { object value = v.Value; bool b; var s = value as string; if (s != null) { b = string.IsNullOrWhiteSpace(s); } else { var enumerable = value as IEnumerable; if (enumerable != null) { b = !enumerable.Cast <object>().Any(); } else { b = Equals(value, default(TProperty)); } } return(b ? v.CreateErrorInfo(Message(message, () => Messages.notempty_error)) : null); });
public static IPropertyValidator <T, TProperty> NotNull <T, TProperty>(this IPropertyValidator <T, TProperty> @this, Func <string> message = null) => @this.Add(v => { object value = v.Value; return(value == null ? v.CreateErrorInfo(Message(message, () => Messages.notnull_error)) : null); });
public static IPropertyValidator <T, TProperty> ExclusiveBetween <T, TProperty>(this IPropertyValidator <T, TProperty> @this, TProperty from, TProperty to) where TProperty : IComparable <TProperty>, IComparable => @this.Add(v => from.CompareTo(@this.Value) >= 0 || to.CompareTo(@this.Value) <= 0 ? v.CreateFailureData("'{PropertyName}' must be between {From} and {To}. You entered {Value}.", text => text.ReplacePlaceholderWithValue( MessageFormatter.CreateTuple("From", from), MessageFormatter.CreateTuple("To", to), MessageFormatter.CreateTuple("Value", @this.Value))) : null);
public static IPropertyValidator <T, string> Length <T>(this IPropertyValidator <T, string> @this, int minLength, int maxLength) => @this.Add(v => { var length = @this.Value?.Length ?? 0; return(length < minLength || length > maxLength ? v.CreateFailureData("'{PropertyName}' must be between {MinLength} and {MaxLength} characters. You entered {TotalLength} characters.", text => text.ReplacePlaceholderWithValue( MessageFormatter.CreateTuple("MaxLength", maxLength), MessageFormatter.CreateTuple("MinLength", minLength), MessageFormatter.CreateTuple("TotalLength", length))) : null); });
public static IPropertyValidator <T, string> Length <T>(this IPropertyValidator <T, string> @this, int minLength, int maxLength, Func <string> message = null) => @this.Add(v => { var length = @this.Value?.Length ?? 0; return(length < minLength || length > maxLength ? v.CreateErrorInfo(Message(message, () => Messages.length_error), text => text.ReplacePlaceholderWithValue( CreateTuple("MaxLength", maxLength), CreateTuple("MinLength", minLength), CreateTuple("TotalLength", length))) : null); });
public static IPropertyValidator <T, TProperty> NotEqual <T, TProperty>(this IPropertyValidator <T, TProperty> @this, TProperty comparisonValue) => @this.Add(v => Equals(v.Value, comparisonValue) ? v.CreateFailureData("'{PropertyName}' should not be equal to '{ComparisonValue}'.", text => text.ReplacePlaceholderWithValue(MessageFormatter.CreateTuple("ComparisonValue", comparisonValue))) : null);
public static IPropertyValidator <T, TProperty> NotEqual <T, TProperty>(this IPropertyValidator <T, TProperty> @this, TProperty comparisonValue, Func <string> message = null) => @this.Add(v => Equals(v.Value, comparisonValue) ? v.CreateErrorInfo(Message(message, () => Messages.notequal_error), text => text.ReplacePlaceholderWithValue(CreateTuple("ComparisonValue", comparisonValue))) : null);