Created
September 30, 2019 16:34
-
-
Save phuctm97/95d266d43090d18d448db6dc3091aac3 to your computer and use it in GitHub Desktop.
Go RESTful Series
This file contains hidden or 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
package users | |
// ValidateUserUnique validates whether user's username or email is valid and returns errors or nil. | |
func ValidateUserUnique(userRepo UserRepository, user *User) error { | |
errs := make([]error, 0) | |
var exists bool | |
var err error | |
// Validate username. | |
exists, err = userRepo.ExistsByUsername(user.Username) | |
if err != nil { | |
return err | |
} | |
if exists { | |
errs = append(errs, ErrUsernameWasUsed) | |
} | |
// Validate email. | |
exists, err = userRepo.ExistsByEmail(user.Email) | |
if err != nil { | |
return err | |
} | |
if exists { | |
errs = append(errs, ErrEmailWasUsed) | |
} | |
// Return errors or nil. | |
if len(errs) == 0 { | |
return nil | |
} | |
return common.Errors(errs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment