Created
October 5, 2019 15:29
-
-
Save beniusij/dd9506bb402aa32628013080eb009afa to your computer and use it in GitHub Desktop.
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
package users | |
import ( | |
"fmt" | |
"gopkg.in/go-playground/validator.v9" | |
) | |
type UserModelValidator struct { | |
user struct{ | |
Email string `validate:"required,email,unique"` | |
FirstName string `validate:"required,min=2,max=255"` | |
LastName string `validate:"required,min=2,max=255"` | |
Password string `validate:"required,min=8,max=255"` | |
PasswordConfirm string `validate:"required,eqfield=Password"` | |
} | |
userModel User | |
} | |
var validate *validator.Validate | |
func NewUserValidator() UserModelValidator { | |
userValidator := UserModelValidator{} | |
return userValidator | |
} | |
func (u *UserModelValidator) validateUserForm(p map[string]interface{}) error { | |
validate = validator.New() | |
fmt.Println("Created validator instance") | |
// Unmarshal params from graphql.ResolveParams and put in struct for validation | |
u.user.Email = p["email"].(string) | |
u.user.FirstName = p["first_name"].(string) | |
u.user.LastName = p["last_name"].(string) | |
u.user.Password = p["password"].(string) | |
u.user.PasswordConfirm = p["password_confirm"].(string) | |
fmt.Println("Finished unmarshal values") | |
err := validate.Struct(&u.user) | |
if err != nil { | |
fmt.Println(fmt.Sprintf("Error validating form: %v", err)) | |
return err | |
} | |
fmt.Println("Finished validating") | |
// After validation re-assign those values to User and set password hash | |
u.userModel.Email = u.user.Email | |
u.userModel.FirstName = u.user.FirstName | |
u.userModel.LastName = u.user.LastName | |
err = u.userModel.SetPassword(u.user.Password) | |
if err != nil { | |
fmt.Println(fmt.Sprintf("Error setting password: %v", err)) | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment