-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Milestone
Description
FluentValidation version
11.1.1
ASP.NET version
6.0
Summary
There is a hierarchy of classes. They use validators with Include to include base class validator.
I'm trying to validate only one field by using ValidationContext with MemberNameValidatorSelector.
For base class it works as expected, for inheritor validation returns errors for different fields, not included in MemberNameValidatorSelector.
Steps to Reproduce
You can try out a demo on dotnetfiddle.
A hierarchy of classes :
public class Foo
{
public string FooString { get; set; }
public List<string> FooList { get; set; } = new();
}
public class Bar : Foo
{
}
public class Baz : Bar
{
}Validators:
public class FooValidator : AbstractValidator<Foo>
{
public FooValidator()
{
RuleFor((it) => it.FooString).NotEmpty();
}
}
public class BarValidator : AbstractValidator<Bar>
{
public BarValidator()
{
Include(new FooValidator());
RuleFor((it) => it.FooList).NotEmpty();
}
}
public class BazValidator : AbstractValidator<Baz>
{
public BazValidator()
{
Include(new BarValidator());
}
}When calling validation for FooString property of Bar:
Bar bar = new Bar() { FooString = "NotEmpty" };
var properties = new[] { nameof(Bar.FooString) };
var context = new ValidationContext<Bar>(bar, new PropertyChain(), new MemberNameValidatorSelector(properties));
IValidator v = new BarValidator();
var result = v.Validate(context); // result.Errors is emptyresult has no errors as expected.
But with the same code for FooString property of Baz:
Baz baz = new Baz() { FooString = "NotEmpty" };
var properties = new[] { nameof(Baz.FooString) };
var context = new ValidationContext<Baz>(baz, new PropertyChain(), new MemberNameValidatorSelector(properties));
IValidator v = new BazValidator();
var result = v.Validate(context); // result.Errors has an error for FooListresult has an error for FooList, even thought it is not included in properties.
Metadata
Metadata
Assignees
Labels
No labels