How do you validate against each string in a list using Fluent Validation?
Try to use:
public class AccountsValidator : AbstractValidator<AccountViewModel>
{
public AccountsValidator()
{
RuleForEach(x => x.Accounts).NotNull()
}
}
The following should work:
public class AccountsValidator : AbstractValidator<AccountViewModel>
{
public AccountsValidator()
{
RuleFor(x => x.Accounts).SetCollectionValidator(
new AccountValidator("Accounts")
);
}
}
public class AccountValidator : AbstractValidator<string>
{
public AccountValidator(string collectionName)
{
RuleFor(x => x)
.NotEmpty()
.OverridePropertyName(collectionName);
}
}
You could use RuleForEach
There is an easy class with a list of string
public class Request
{
public IEnumerable<string> UserIds { get; set; }
public string Body { get; set; }
}
I created the next validator
public class RequestValidator : AbstractValidator<Request>
{
public RequestValidator()
{
RuleForEach(x => x.UserIds).NotNull().NotEmpty();
RuleFor(x => x.Body).NotNull().NotEmpty();
}
}