First of all, I've already tried to make the steps of this question.
This class Post uses a fluent validation
public class Post
{
//...
public int? LampType { get; set; }
public int? LampModel { get; set; }
//...
}
And his fluent validation. It works fine.
//...
RuleFor(x => x.LampType).NotNull().WithMessage(x => "LampType can't be null");
RuleFor(x => x).Must(p => LampType.Open.ID == p.LampType )
.When(p => LampModel.NoCover.ID == p.LampModel ).WithMessage(x => "A closed Lamp needs a cover");
RuleFor(x => x).Must(p => LampType.Closed.ID == p.LampType )
.When(p => LampModel.NoCover.ID != p.LampModel ).WithMessage(x => "A open Lamp can't have a cover");
//...
But i need to make a update for this and now my Post can have multiples Lamps, so i created this:
public class newPost
{
//...
public IEnumerable<LampPostModel> Lamps { get; set; }
//...
}
public class LampPostModel
{
public int? LampType { get; set; }
public int? LampModel { get; set; }
}
And I've tried this on the PostValidation
{
//...
RuleFor(x => x.Lamps).SetCollectionValidator(new LampValidator());
//...
}
And this is the new LampValidator
{
RuleFor(x => x.LampType).NotNull().WithMessage(x => "LampType can't be null");
RuleFor(x => x).Must(p => LampType.Open.ID == p.LampType )
.When(p => LampModel.NoCover.ID == p.LampModel ).WithMessage(x => "A closed Lamp needs a cover");
RuleFor(x => x).Must(p => LampType.Closed.ID == p.LampType )
.When(p => LampModel.NoCover.ID != p.LampModel ).WithMessage(x => "A open Lamp can't have a cover");
}
But after this change, this doesn't work anymore. It rejects at the second rule, even when shouldn't. It seems that the validation is trying to test all the objects at once, not one by one
The request has been made with something like this
Lamps[
{open, nocover},
{closed, glass}
];