public class PersonValidator : AbstractValidator<Person>
{
private readonly IPersonRepository _personRepository;
private readonly List<Person> _peopleFromExcelFile;
public PersonValidator(IPersonRepository personRepository, List<Person> peopleFromExcelFile)
{
_personRepository = personRepository;
_peopleFromExcelFile = peopleFromExcelFile;
RuleFor(x => x.FullName)
.NotEmpty()
.WithMessage("Full name is empty!");
RuleFor(x => x.Email)
.NotEmpty()
.WithMessage("Email is empty");
RuleFor(x => x.Email)
.Must(PersonMailDoNotExistInRepository)
.WithMessage("Email already exists in database");
RuleFor(x => x.Email)
.Must(EmailIsNotDuplicateInExcelFile)
.WithMessage("Email is duplicated in excel file");
}
private bool PersonMailDoNotExistInRepository(string email)
{
return _personRepository.GetByEmail(email) == null;
}
private bool EmailIsNotDuplicateInExcelFile(string email)
{
return _peopleFromExcelFile.Count(x => x.Email == email) == 1;
}
}
public class PersonValidator : AbstractValidator<Person>
{
private readonly IPersonRepository _personRepository;
private readonly List<Person> _peopleFromExcelFile;
public PersonValidator(IPersonRepository personRepository, List<Person> peopleFromExcelFile)
{
_personRepository = personRepository;
_peopleFromExcelFile = peopleFromExcelFile;
RuleFor(x => x.FullName)
.NotEmpty()
.WithMessage("Full name is empty!");
RuleFor(x => x.Email)
.NotEmpty()
.WithMessage("Email is empty");
RuleFor(x => x.Email)
.Must(PersonMailDoNotExistInRepository)
.WithMessage("Email already exists in database");
RuleFor(x => x.Email)
.Must(EmailIsNotDuplicateInExcelFile)
.WithMessage("Email is duplicated in excel file");
}
private bool PersonMailDoNotExistInRepository(string email)
{
return _personRepository.GetByEmail(email) == null;
}
private bool EmailIsNotDuplicateInExcelFile(string email)
{
return _peopleFromExcelFile.Count(x => x.Email == email) == 1;
}
}