From d84d68eca72935d42e8238c9e56e040ffe2b1701 Mon Sep 17 00:00:00 2001 From: Eren Date: Sun, 13 Feb 2022 22:11:17 +0300 Subject: [PATCH] request model validation check --- src/services/blog-api/handler/blogHandler.go | 10 ++++++++++ src/services/blog-api/handler/loginHandler.go | 5 +++++ src/services/blog-api/models/loginRequest.go | 16 ++++++++++++++++ src/services/blog-api/models/storyRequest.go | 10 ++++++++++ 4 files changed, 41 insertions(+) diff --git a/src/services/blog-api/handler/blogHandler.go b/src/services/blog-api/handler/blogHandler.go index 9c900e4..90906f4 100644 --- a/src/services/blog-api/handler/blogHandler.go +++ b/src/services/blog-api/handler/blogHandler.go @@ -22,6 +22,11 @@ func CreateStory(context *gin.Context) { badRequest(context, http.StatusBadRequest, "invalid request", errors) } + if err := story.IsValid(); err != nil { + badRequest(context, http.StatusBadRequest, "invalid request", err) + return + } + cu := helper.GetCurrentUser(context) newStory := entities.NewStory(story.Title, story.Content, cu.ID) @@ -42,6 +47,11 @@ func UpdateStory(context *gin.Context) { badRequest(context, http.StatusBadRequest, "invalid request", errors) } + if err := story.IsValid(); err != nil { + badRequest(context, http.StatusBadRequest, "invalid request", err) + return + } + storyService := services.NewStoryService(context) storyService.UpdateStory(story) diff --git a/src/services/blog-api/handler/loginHandler.go b/src/services/blog-api/handler/loginHandler.go index 615eb1a..069ce92 100644 --- a/src/services/blog-api/handler/loginHandler.go +++ b/src/services/blog-api/handler/loginHandler.go @@ -23,6 +23,11 @@ func LoginHandler(context *gin.Context) { return } + if err := loginObj.IsValid(); err != nil { + badRequest(context, http.StatusBadRequest, "invalid request", err) + return + } + userService := services.NewUserService() genrateJWTToken(context, loginObj, userService) diff --git a/src/services/blog-api/models/loginRequest.go b/src/services/blog-api/models/loginRequest.go index d9ef1dd..7fb84d2 100644 --- a/src/services/blog-api/models/loginRequest.go +++ b/src/services/blog-api/models/loginRequest.go @@ -5,3 +5,19 @@ type LoginRequest struct { Password string `json:"Password" form:"Password" binding:"required"` RememberMe bool `json:"RememberMe" form:"RememberMe"` } + +func (lr LoginRequest) IsValid() (errs []ErrorDetail) { + if lr.UserName == "" { + errs = append(errs, ErrorDetail{ErrorType: ErrorTypeValidation, ErrorMessage: "The username is required!"}) + } + if len(lr.UserName) < 5 || len(lr.UserName) > 10 { + errs = append(errs, ErrorDetail{ErrorType: ErrorTypeValidation, ErrorMessage: "The username field must be between 5-10 chars!"}) + } + if lr.Password == "" { + errs = append(errs, ErrorDetail{ErrorType: ErrorTypeValidation, ErrorMessage: "The password is required!"}) + } + if len(lr.Password) < 5 || len(lr.Password) > 10 { + errs = append(errs, ErrorDetail{ErrorType: ErrorTypeValidation, ErrorMessage: "The password field must be between 5-10 chars!"}) + } + return errs +} diff --git a/src/services/blog-api/models/storyRequest.go b/src/services/blog-api/models/storyRequest.go index 4b7aaf8..5f8cfc6 100644 --- a/src/services/blog-api/models/storyRequest.go +++ b/src/services/blog-api/models/storyRequest.go @@ -4,3 +4,13 @@ type StoryRequest struct { Title string `json:"Title"` Content string `json:"Content"` } + +func (sr StoryRequest) IsValid() (errs []ErrorDetail) { + if sr.Title == "" { + errs = append(errs, ErrorDetail{ErrorType: ErrorTypeValidation, ErrorMessage: "The title is required!"}) + } + if sr.Content == "" { + errs = append(errs, ErrorDetail{ErrorType: ErrorTypeValidation, ErrorMessage: "The content is required!"}) + } + return errs +}