Skip to content

Instantly share code, notes, and snippets.

@phuctm97
Created September 24, 2019 16:09
Show Gist options
  • Save phuctm97/f262757697d199d1a08054e2dbdbba43 to your computer and use it in GitHub Desktop.
Save phuctm97/f262757697d199d1a08054e2dbdbba43 to your computer and use it in GitHub Desktop.
Go RESTful Series
package users
import (
"errors"
"regexp"
)
// Regular expressions.
var (
regexContainsOnlyDigitsUnderscoreDashesDotsAlpha = regexp.MustCompile(`^[0-9a-zA-Z_\.\-]*$`)
regexBeginsWithUnderscoreOrAlpha = regexp.MustCompile(`^[a-zA-Z_].*$`)
regexIsEmail = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
)
// ValidateUser validates a user data and returns validation errors.
func ValidateUser(user *User) []error {
errs := make([]error, 0)
// Validate username.
if len(user.Username) < 1 {
errs = append(errs, errors.New("username must be at least 1-character length"))
}
if len(user.Username) > 32 {
errs = append(errs, errors.New("username must be at most 32-character length"))
}
if !regexContainsOnlyDigitsUnderscoreDashesDotsAlpha.MatchString(user.Username) {
errs = append(errs, errors.New("username must contain only digits, underscores, dashes, dots and alphabetical letters"))
}
if !regexBeginsWithUnderscoreOrAlpha.MatchString(user.Username) {
errs = append(errs, errors.New("username must begin with either underscore or an alphabetical letter"))
}
// Validate email.
if !regexIsEmail.MatchString(user.Email) {
errs = append(errs, errors.New("email is invalid"))
}
if len(user.Email) > 128 {
errs = append(errs, errors.New("email must be at most 128-character length"))
}
// Validate full name.
if len(user.FullName) < 1 {
errs = append(errs, errors.New("full name must be at least 1-character length"))
}
if len(user.FullName) > 128 {
errs = append(errs, errors.New("full name must be at most 128-character length"))
}
// Validate bio.
if len(user.Bio) > 256 {
errs = append(errs, errors.New("bio must be at most 128-character length"))
}
return errs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment