-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grpc): add validator to grpc of create_user and login_user API
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package gapi | ||
|
||
import ( | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func fieldViolation(field string, err error) *errdetails.BadRequest_FieldViolation { | ||
return &errdetails.BadRequest_FieldViolation{ | ||
Field: field, | ||
Description: err.Error(), | ||
} | ||
} | ||
|
||
func invalidArgumentError(violations []*errdetails.BadRequest_FieldViolation) error { | ||
badRequest := &errdetails.BadRequest{FieldViolations: violations} | ||
statusInvalid := status.New(codes.InvalidArgument, "invalid parameters") | ||
|
||
statusDetails, err := statusInvalid.WithDetails(badRequest) | ||
if err != nil { | ||
return statusInvalid.Err() | ||
} | ||
|
||
return statusDetails.Err() | ||
} | ||
|
||
func unauthenticatedError(err error) error { | ||
return status.Errorf(codes.Unauthenticated, "unauthorized: %s", err) | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package val | ||
|
||
import ( | ||
"fmt" | ||
"net/mail" | ||
"regexp" | ||
) | ||
|
||
var ( | ||
isValidUsername = regexp.MustCompile(`^[a-z0-9_]+$`).MatchString | ||
isValidFullName = regexp.MustCompile(`^[a-zA-Z\s]+$`).MatchString | ||
) | ||
|
||
func ValidateString(value string, minLength int, maxLength int) error { | ||
n := len(value) | ||
if n < minLength || n > maxLength { | ||
return fmt.Errorf("must contain from %d-%d characters", minLength, maxLength) | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateUsername(value string) error { | ||
if err := ValidateString(value, 3, 100); err != nil { | ||
return err | ||
} | ||
if !isValidUsername(value) { | ||
return fmt.Errorf("must contain only lowercase letters, digits, or underscore") | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateFullName(value string) error { | ||
if err := ValidateString(value, 3, 100); err != nil { | ||
return err | ||
} | ||
if !isValidFullName(value) { | ||
return fmt.Errorf("must contain only letters or spaces") | ||
} | ||
return nil | ||
} | ||
|
||
func ValidatePassword(value string) error { | ||
return ValidateString(value, 6, 100) | ||
} | ||
|
||
func ValidateEmail(value string) error { | ||
if err := ValidateString(value, 3, 200); err != nil { | ||
return err | ||
} | ||
if _, err := mail.ParseAddress(value); err != nil { | ||
return fmt.Errorf("is not a valid email address") | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateEmailId(value int64) error { | ||
if value <= 0 { | ||
return fmt.Errorf("must be a positive integer") | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateSecretCode(value string) error { | ||
return ValidateString(value, 32, 128) | ||
} |