forked from techschool/simplebank
-
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.
validate grpc request parameters (techschool#35)
Co-authored-by: phamlequang <phamlequang@gmail.com>
- Loading branch information
1 parent
9212f74
commit 5ead2d2
Showing
4 changed files
with
126 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,26 @@ | ||
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() | ||
} |
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,54 @@ | ||
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 | ||
} |