Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Do validation only for a single property

$
0
0

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.


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>