public static void RequiredArgumentEmail <T>(this T @object, Expression <Func <string> > propertyLambda) where T : class { var value = (string)ExpressionsUtils.GetValue(propertyLambda); if (new EmailAddressAttribute().IsValid(value) == false) { ThrowArgumentException(propertyLambda, $"Not valid email = {value}", context: @object); } }
public static void RequiredArgumentNotDefaultValue <T, TProperty>(this T @object, Expression <Func <TProperty> > propertyLambda) where T : class where TProperty : struct { var value = ExpressionsUtils.GetValue(propertyLambda); if (value.Equals(default(TProperty))) { var typeParameterType = typeof(TProperty); ThrowArgumentException(propertyLambda: propertyLambda, message: $"{typeParameterType.Name} equals default type value = {value}", context: @object); } }
public static void RequiredArgumentDateIsActual <T>(this T @object, Expression <Func <DateTime> > propertyLambda, int yearDelta = 1) where T : class { var dateNow = DateTime.Now; var fromDate = dateNow.AddYears(-yearDelta); var toDate = dateNow.AddYears(yearDelta); var value = (DateTime)ExpressionsUtils.GetValue(propertyLambda); if (value < fromDate || value > toDate) { ThrowArgumentException(propertyLambda, $"DateTime is out of range values from = {fromDate}, to = {toDate}", context: @object); } }
public static void RequiredNotNull <T, TProperty>(this T @object, Expression <Func <TProperty> > propertyLambda) where T : class { var value = ExpressionsUtils.GetValue(propertyLambda); if (value == null) { ThrowInvalidOperationException(propertyLambda: propertyLambda, context: @object); } var typeParameterType = typeof(TProperty); if (typeParameterType == typeof(string) && string.IsNullOrEmpty((string)value)) { ThrowInvalidOperationException(propertyLambda: propertyLambda, context: @object); } }
public static void RequiredArgumentEnumIsDefinedValue <T, TProperty>(this T @object, Expression <Func <TProperty> > propertyLambda) where T : class where TProperty : struct, IConvertible { if (typeof(TProperty).IsEnum == false) { throw new InvalidOperationException("TProperty must be an enumerated type"); } var value = ExpressionsUtils.GetValue(propertyLambda); var propertyType = typeof(TProperty); if (Enum.IsDefined(propertyType, value) == false) { ThrowArgumentException(propertyLambda, $"Not defined {propertyType.Name} value = {value}", context: @object); } }
public static void RequiredListNotEmpty <T, TProperty>(this T @object, Expression <Func <IEnumerable <TProperty> > > propertyLambda) where T : class { RequiredNotNull(@object, propertyLambda); var value = ExpressionsUtils.GetValue(propertyLambda) as IEnumerable <TProperty>; if (value == null) { throw new InvalidOperationException($"value must be IEnumerable<{typeof(TProperty).Name}>"); } if (value.Any() == false) { var typeParameterType = typeof(TProperty); ThrowInvalidOperationException(propertyLambda: propertyLambda, message: $"IEnumerable<{typeParameterType.Name}> must not be empty", context: @object); } }