Skip to content

Commit

Permalink
enforce at least 1 field exists one update functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Mar 5, 2024
1 parent d25ecdd commit 8df53a8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions backend/src/errors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package errors
import "github.com/gofiber/fiber/v2"

var (
FailedToValidateAtLeastOneField = Error{
StatusCode: fiber.StatusBadRequest,
Message: "failed to validate at least one field",
}
FailedToParseRequestBody = Error{
StatusCode: fiber.StatusBadRequest,
Message: "failed to parse request body",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ type Category struct {

type CategoryRequestBody struct {
Name string `json:"name" validate:"required,max=255"`
}
}

Check failure on line 12 in backend/src/models/category.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)

Check failure on line 12 in backend/src/models/category.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
4 changes: 4 additions & 0 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (c *ClubService) UpdateClub(id string, clubBody models.UpdateClubRequestBod
return nil, &errors.FailedToValidateClub
}

if err := utilities.AtLeastOne(clubBody, models.UpdateClubRequestBody{}); err != nil {
return nil, err
}

club, err := utilities.MapRequestToModel(clubBody, &models.Club{})
if err != nil {
return nil, &errors.FailedToMapRequestToModel
Expand Down
4 changes: 4 additions & 0 deletions backend/src/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func (e *EventService) UpdateEvent(id string, eventBody models.UpdateEventReques
return nil, &errors.FailedToValidateEvent
}

if err := utilities.AtLeastOne(eventBody, models.UpdateEventRequestBody{}); err != nil {
return nil, err
}

event, err := utilities.MapRequestToModel(eventBody, &models.UpdateEventRequestBody{})
if err != nil {
return nil, &errors.FailedToMapRequestToModel
Expand Down
5 changes: 5 additions & 0 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (t *TagService) UpdateTag(tagID string, tagBody models.UpdateTagRequestBody
return nil, &errors.FailedToValidateTag
}

if err := utilities.AtLeastOne(tagBody, models.UpdateTagRequestBody{}); err != nil {
return nil, err
}


Check failure on line 83 in backend/src/services/tag.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)

Check failure on line 83 in backend/src/services/tag.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
tag, err := utilities.MapRequestToModel(tagBody, &models.Tag{})
if err != nil {
return nil, &errors.FailedToMapRequestToModel
Expand Down
4 changes: 4 additions & 0 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (u *UserService) UpdateUser(id string, userBody models.UpdateUserRequestBod
return nil, &errors.FailedToValidateUser
}

if err := utilities.AtLeastOne(userBody, models.UpdateUserRequestBody{}); err != nil {
return nil, err
}

user, err := utilities.MapRequestToModel(userBody, &models.User{})
if err != nil {
return nil, &errors.FailedToMapRequestToModel
Expand Down
9 changes: 9 additions & 0 deletions backend/src/utilities/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utilities

import (
"reflect"
"regexp"
"strconv"

Expand Down Expand Up @@ -41,6 +42,14 @@ func RegisterCustomValidators() (*validator.Validate, error) {
return validate, nil
}

func AtLeastOne[Model any](body Model, model Model) *errors.Error {
if reflect.DeepEqual(body, model) {
return &errors.FailedToValidateAtLeastOneField
}

return nil
}

func validateEmail(fl validator.FieldLevel) bool {
email, err := emailaddress.Parse(fl.Field().String())
if err != nil {
Expand Down

0 comments on commit 8df53a8

Please sign in to comment.