0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-29 18:21:08 +00:00
CleanArchitecture/CleanArchitecture.Domain/Commands/Users/CreateUser/CreateUserCommandValidation.cs
2023-11-25 16:09:15 +01:00

74 lines
2.4 KiB
C#

using CleanArchitecture.Domain.Constants;
using CleanArchitecture.Domain.Errors;
using CleanArchitecture.Domain.Extensions.Validation;
using FluentValidation;
namespace CleanArchitecture.Domain.Commands.Users.CreateUser;
public sealed class CreateUserCommandValidation : AbstractValidator<CreateUserCommand>
{
public CreateUserCommandValidation()
{
AddRuleForId();
AddRuleForTenantId();
AddRuleForEmail();
AddRuleForFirstName();
AddRuleForLastName();
AddRuleForPassword();
}
private void AddRuleForId()
{
RuleFor(cmd => cmd.UserId)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.User.EmptyId)
.WithMessage("User id may not be empty");
}
private void AddRuleForTenantId()
{
RuleFor(cmd => cmd.TenantId)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.Tenant.EmptyId)
.WithMessage("Tenant id may not be empty");
}
private void AddRuleForEmail()
{
RuleFor(cmd => cmd.Email)
.EmailAddress()
.WithErrorCode(DomainErrorCodes.User.InvalidEmail)
.WithMessage("Email is not a valid email address")
.MaximumLength(MaxLengths.User.Email)
.WithErrorCode(DomainErrorCodes.User.EmailExceedsMaxLength)
.WithMessage($"Email may not be longer than {MaxLengths.User.Email} characters");
}
private void AddRuleForFirstName()
{
RuleFor(cmd => cmd.FirstName)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.User.EmptyFirstName)
.WithMessage("FirstName may not be empty")
.MaximumLength(MaxLengths.User.FirstName)
.WithErrorCode(DomainErrorCodes.User.FirstNameExceedsMaxLength)
.WithMessage($"FirstName may not be longer than {MaxLengths.User.FirstName} characters");
}
private void AddRuleForLastName()
{
RuleFor(cmd => cmd.LastName)
.NotEmpty()
.WithErrorCode(DomainErrorCodes.User.EmptyLastName)
.WithMessage("LastName may not be empty")
.MaximumLength(MaxLengths.User.LastName)
.WithErrorCode(DomainErrorCodes.User.LastNameExceedsMaxLength)
.WithMessage($"LastName may not be longer than {MaxLengths.User.LastName} characters");
}
private void AddRuleForPassword()
{
RuleFor(cmd => cmd.Password)
.Password();
}
}