In addition to this question, I would like to do validation only based on property value.
Like:
var validator = new ProductValidator(); var result = validator.ValidateSingleProperty(p => p.Code, "MyCode");
where ProductValidator is defined as :
publicclass ProductValidator : AbstractValidator<Product>
and Product has a string property 'Code'.
Extension method would be something like this ?
publicstatic ValidationResult ValidateSingleProperty<T>(this IValidator<T> validator, params Expression<Func<T, object>>[] propertyExpressions, object value) where T : new() { var instance = new T(); // and set the propery as defined in propertyExpressions ???var context = new ValidationContext<T>(instance, new PropertyChain(), MemberNameValidatorSelector.FromExpressions(propertyExpressions)); return validator.Validate(context); }
Advice is needed.