Skip to content

Instantly share code, notes, and snippets.

@sunilkumarmedium
Created November 22, 2020 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilkumarmedium/6183a0ede703f9acc5b049ae9b2257c6 to your computer and use it in GitHub Desktop.
Save sunilkumarmedium/6183a0ede703f9acc5b049ae9b2257c6 to your computer and use it in GitHub Desktop.
CreateUserCommandValidator.cs
namespace CleanArchitectureApp.Application.Features.Users.Commands
{
public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand>
{
private readonly IUserRepositoryAsync userRepository;
public CreateUserCommandValidator(IUserRepositoryAsync userRepository)
{
this.userRepository = userRepository;
RuleFor(p => p.UserName)
.NotEmpty().WithMessage("{PropertyName} is required.")
.NotNull()
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters.")
.MustAsync(IsUniqueUserName).WithMessage("{PropertyName} already exists.");
RuleFor(p => p.Password)
.NotEmpty().WithMessage("{PropertyName} is required.")
.NotNull()
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters.");
RuleFor(p => p.FirstName)
.NotEmpty().WithMessage("{PropertyName} is required.")
.NotNull()
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters.");
RuleFor(p => p.UserEmail)
.NotEmpty().WithMessage("{PropertyName} is required.")
.NotNull()
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters.");
RuleFor(p => p.CreatedBy)
.NotEmpty().WithMessage("{PropertyName} is required.")
.NotNull()
.WithMessage("{PropertyName} must not exceed 50 characters.");
}
private async Task<bool> IsUniqueUserName(string username, CancellationToken cancellationToken)
{
var userObject = (await userRepository.FindByCondition(x => x.UserName.ToLower() == username.ToLower()).ConfigureAwait(false)).AsQueryable().FirstOrDefault();
if (userObject != null)
{
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment