mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-30 10:33:43 +00:00
65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
using CleanArchitecture.Domain.Errors;
|
|
using FluentValidation;
|
|
|
|
namespace CleanArchitecture.Domain.Commands.Users.UpdateUser;
|
|
|
|
public sealed class UpdateUserCommandValidation : AbstractValidator<UpdateUserCommand>
|
|
{
|
|
public UpdateUserCommandValidation()
|
|
{
|
|
AddRuleForId();
|
|
AddRuleForEmail();
|
|
AddRuleForSurname();
|
|
AddRuleForGivenName();
|
|
AddRuleForRole();
|
|
}
|
|
|
|
private void AddRuleForId()
|
|
{
|
|
RuleFor(cmd => cmd.UserId)
|
|
.NotEmpty()
|
|
.WithErrorCode(DomainErrorCodes.UserEmptyId)
|
|
.WithMessage("User id may not be empty");
|
|
}
|
|
|
|
private void AddRuleForEmail()
|
|
{
|
|
RuleFor(cmd => cmd.Email)
|
|
.EmailAddress()
|
|
.WithErrorCode(DomainErrorCodes.UserInvalidEmail)
|
|
.WithMessage("Email is not a valid email address")
|
|
.MaximumLength(320)
|
|
.WithErrorCode(DomainErrorCodes.UserEmailExceedsMaxLength)
|
|
.WithMessage("Email may not be longer than 320 characters");
|
|
}
|
|
|
|
private void AddRuleForSurname()
|
|
{
|
|
RuleFor(cmd => cmd.Surname)
|
|
.NotEmpty()
|
|
.WithErrorCode(DomainErrorCodes.UserEmptySurname)
|
|
.WithMessage("Surname may not be empty")
|
|
.MaximumLength(100)
|
|
.WithErrorCode(DomainErrorCodes.UserSurnameExceedsMaxLength)
|
|
.WithMessage("Surname may not be longer than 100 characters");
|
|
}
|
|
|
|
private void AddRuleForGivenName()
|
|
{
|
|
RuleFor(cmd => cmd.GivenName)
|
|
.NotEmpty()
|
|
.WithErrorCode(DomainErrorCodes.UserEmptyGivenName)
|
|
.WithMessage("Given name may not be empty")
|
|
.MaximumLength(100)
|
|
.WithErrorCode(DomainErrorCodes.UserGivenNameExceedsMaxLength)
|
|
.WithMessage("Given name may not be longer than 100 characters");
|
|
}
|
|
|
|
private void AddRuleForRole()
|
|
{
|
|
RuleFor(cmd => cmd.Role)
|
|
.IsInEnum()
|
|
.WithErrorCode(DomainErrorCodes.UserInvalidRole)
|
|
.WithMessage("Role is not a valid role");
|
|
}
|
|
} |