antinutrino
antinutrino

Reputation: 179

FluentValidation: validate only the property that has changed

I want to only validate the property that has changed on my model, unfortunately fluent validation by default appears to validate every property that has a rule when calling Validator.Validate(instanceToValidate)

I've tried setting the PropertyChain to only include the property I want to validate and construct a new ValidationContext - It still validates all the rules.

Is there a way to achieve this using fluent validation?

Thanks

Upvotes: 4

Views: 2578

Answers (1)

antinutrino
antinutrino

Reputation: 179

so the fix is quite simple

var rule = _validator.CreateDescriptor();
var rules = rule.GetRulesForMember(e.PropertyName);
_validationResult = new ValidationResult(rules.SelectMany(x => x.Validate(new ValidationContext(_target))).ToList());

Find the rule(s) for the property that has changed and validate the target object against that ruleset.

Upvotes: 5

Related Questions