Skip to content

Commit

Permalink
request model validation check
Browse files Browse the repository at this point in the history
  • Loading branch information
alnck committed Feb 13, 2022
1 parent 3f510a5 commit d84d68e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/services/blog-api/handler/blogHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions src/services/blog-api/handler/loginHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions src/services/blog-api/models/loginRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions src/services/blog-api/models/storyRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit d84d68e

Please sign in to comment.