0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-29 18:21:08 +00:00
CleanArchitecture/CleanArchitecture.Domain/Commands/Users/UpdateUser/UpdateUserCommandValidation.cs
2023-08-30 23:36:48 +02:00

75 lines
2.5 KiB
C#

using CleanArchitecture.Domain.Constants;
using CleanArchitecture.Domain.Errors;
using FluentValidation;
namespace CleanArchitecture.Domain.Commands.Users.UpdateUser;
public sealed class UpdateUserCommandValidation : AbstractValidator<UpdateUserCommand>
{
public UpdateUserCommandValidation()
{
AddRuleForId();
AddRuleForTenantId();
AddRuleForEmail();
AddRuleForFirstName();
AddRuleForLastName();
AddRuleForRole();
}
private void AddRuleForId()
{
RuleFor(cmd => cmd.UserId)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.User.UserEmptyId)
.WithMessage("User id may not be empty");
}
private void AddRuleForTenantId()
{
RuleFor(cmd => cmd.TenantId)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.Tenant.TenantEmptyId)
.WithMessage("Tenant id may not be empty");
}
private void AddRuleForEmail()
{
RuleFor(cmd => cmd.Email)
.EmailAddress()
.WithErrorCode(DomainErrorCodes.User.UserInvalidEmail)
.WithMessage("Email is not a valid email address")
.MaximumLength(MaxLengths.User.Email)
.WithErrorCode(DomainErrorCodes.User.UserEmailExceedsMaxLength)
.WithMessage($"Email may not be longer than {MaxLengths.User.Email} characters");
}
private void AddRuleForFirstName()
{
RuleFor(cmd => cmd.FirstName)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.User.UserEmptyFirstName)
.WithMessage("FirstName may not be empty")
.MaximumLength(MaxLengths.User.FirstName)
.WithErrorCode(DomainErrorCodes.User.UserFirstNameExceedsMaxLength)
.WithMessage($"FirstName may not be longer than {MaxLengths.User.FirstName} characters");
}
private void AddRuleForLastName()
{
RuleFor(cmd => cmd.LastName)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.User.UserEmptyLastName)
.WithMessage("LastName may not be empty")
.MaximumLength(MaxLengths.User.LastName)
.WithErrorCode(DomainErrorCodes.User.UserLastNameExceedsMaxLength)
.WithMessage($"LastName may not be longer than {MaxLengths.User.LastName} characters");
}
private void AddRuleForRole()
{
RuleFor(cmd => cmd.Role)
.IsInEnum()
.WithErrorCode(DomainErrorCodes.User.UserInvalidRole)
.WithMessage("Role is not a valid role");
}
}