public static IValidationRule AddRequiredRule([NotNull] this ValidationHelper validator, [NotNull] Expression <Func <object> > propertyExpression, [NotNull] string errorMessage) { Guard.NotNull(validator, nameof(validator)); Guard.NotNull(propertyExpression, nameof(propertyExpression)); Guard.NotNullOrEmpty(errorMessage, nameof(errorMessage)); Func <object> propertyGetter = propertyExpression.Compile(); return(validator.AddRule(PropertyName.For(propertyExpression, false), () => { object propertyValue = propertyGetter(); var stringPropertyValue = propertyValue as string; if (propertyValue == null || (stringPropertyValue != null && string.IsNullOrEmpty(stringPropertyValue))) { return RuleResult.Invalid(errorMessage); } return RuleResult.Valid(); })); }
public static IValidationRule AddRequiredRule([NotNull] this ValidationHelper validator, [NotNull] Expression <Func <object> > propertyExpression, [NotNull] string errorMessage) { Contract.Requires(validator != null); Contract.Requires(propertyExpression != null); Contract.Requires(!string.IsNullOrEmpty(errorMessage)); Contract.Ensures(Contract.Result <IValidationRule>() != null); Func <object> propertyGetter = propertyExpression.Compile(); return(validator.AddRule(propertyExpression, () => { object propertyValue = propertyGetter(); var stringPropertyValue = propertyValue as string; if (propertyValue == null || (stringPropertyValue != null && string.IsNullOrEmpty(stringPropertyValue))) { return RuleResult.Invalid(errorMessage); } return RuleResult.Valid(); })); }