Skip to content

Instantly share code, notes, and snippets.

@phuctm97
Created September 30, 2019 16:34
Show Gist options
  • Save phuctm97/95d266d43090d18d448db6dc3091aac3 to your computer and use it in GitHub Desktop.
Save phuctm97/95d266d43090d18d448db6dc3091aac3 to your computer and use it in GitHub Desktop.
Go RESTful Series
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