Last active
April 6, 2020 05:43
-
-
Save DavidRogersDev/d4e1ed9a380af3ea877b4cb3118d2029 to your computer and use it in GitHub Desktop.
Gits for Medium Article - RegisterNewUserValidator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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