Skip to content

Instantly share code, notes, and snippets.

@DavidRogersDev
Last active April 6, 2020 05:43
Show Gist options
  • Save DavidRogersDev/d4e1ed9a380af3ea877b4cb3118d2029 to your computer and use it in GitHub Desktop.
Save DavidRogersDev/d4e1ed9a380af3ea877b4cb3118d2029 to your computer and use it in GitHub Desktop.
Gits for Medium Article - RegisterNewUserValidator
public class RegisterNewUserValidator : AbstractValidator<RegisterNewUserCommand>
{
public RegisterNewUserValidator()
{
RuleFor(u => u.Dto.UserName)
.MustAsync(NotContainUserAlready)
.WithMessage((u,userName) => $"{userName} {Constants.Validation.PartMessages.UserAlreadyExists}");
}
async Task<bool> NotContainUserAlready(RegisterNewUserCommand dto, string userName, CancellationToken cancellation = new CancellationToken())
{
// Normally we would hit a database and retrieve the User here.
// For demo purposes, I will enforce an assumption that a user with UserName "DaveRogers" already exists.
return await Task.FromResult(!userName.Equals("DaveRogers"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment