I am looking to validate both my Dto's and Domain models using FluentValidation. I already defined a Validator class to validate my Dto as seen below.
However if I want to add a Validator for my Domain model as well, a change in one of the Validator's will not reflect in the other. As in, if I change the rule for Password length from 6 to 7, I will have to change it on both places.
Is there some way of potentially inheriting the rules from the domain model or something similar, to achieve consistent rules across Dto's and Domain models?
Dto:
public class NewUserDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class NewUserDtoValidator : AbstractValidator<NewUserDto>
{
public NewUserDtoValidator()
{
RuleFor(x => x.FirstName).Length(2, 50);
RuleFor(x => x.LastName).Length(2, 50);
RuleFor(x => x.Email).EmailAddress();
RuleFor(x => x.Username).Length(4, 25);
RuleFor(x => x.Password).MinimumLength(6);
}
}
Domain Model:
public class User
{
public uint Id { get; private set; }
public string Username { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Email { get; private set; }
public DateTime RegistrationDate { get; private set; }
public string Hash { get; private set; }
public string Salt { get; private set; }
}