Skip to content

Commit

Permalink
fix error response
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Mar 5, 2024
1 parent 071ef0c commit 0beefd5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
1 change: 0 additions & 1 deletion backend/src/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func (a *AuthService) UpdatePassword(id string, passwordBody models.UpdatePasswo
return idErr
}

// TODO: Validate password
if err := a.Validate.Struct(passwordBody); err != nil {
return &errors.FailedToValidateUser
}
Expand Down
6 changes: 3 additions & 3 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func (c *ClubService) UpdateClub(id string, clubBody models.UpdateClubRequestBod
return nil, idErr
}

if err := c.Validate.Struct(clubBody); err != nil {
if utilities.AtLeastOne(clubBody, models.UpdateClubRequestBody{}) {
return nil, &errors.FailedToValidateClub
}

if err := utilities.AtLeastOne(clubBody, models.UpdateClubRequestBody{}); err != nil {
return nil, err
if err := c.Validate.Struct(clubBody); err != nil {
return nil, &errors.FailedToValidateClub
}

club, err := utilities.MapRequestToModel(clubBody, &models.Club{})
Expand Down
8 changes: 4 additions & 4 deletions backend/src/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (e *EventService) UpdateEvent(id string, eventBody models.UpdateEventReques
return nil, idErr
}

if err := e.Validate.Struct(eventBody); err != nil {
return nil, &errors.FailedToValidateEvent
if utilities.AtLeastOne(eventBody, models.UpdateEventRequestBody{}) {
return nil, &errors.FailedToValidateTag
}

if err := utilities.AtLeastOne(eventBody, models.UpdateEventRequestBody{}); err != nil {
return nil, err
if err := e.Validate.Struct(eventBody); err != nil {
return nil, &errors.FailedToValidateEvent
}

event, err := utilities.MapRequestToModel(eventBody, &models.UpdateEventRequestBody{})
Expand Down
6 changes: 3 additions & 3 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func (t *TagService) UpdateTag(tagID string, tagBody models.UpdateTagRequestBody
return nil, idErr
}

if err := t.Validate.Struct(tagBody); err != nil {
if utilities.AtLeastOne(tagBody, models.UpdateTagRequestBody{}) {
return nil, &errors.FailedToValidateTag
}

if err := utilities.AtLeastOne(tagBody, models.UpdateTagRequestBody{}); err != nil {
return nil, err
if err := t.Validate.Struct(tagBody); err != nil {
return nil, &errors.FailedToValidateTag
}

tag, err := utilities.MapRequestToModel(tagBody, &models.Tag{})
Expand Down
6 changes: 3 additions & 3 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func (u *UserService) UpdateUser(id string, userBody models.UpdateUserRequestBod
return nil, idErr
}

if err := u.Validate.Struct(userBody); err != nil {
if utilities.AtLeastOne(userBody, models.UpdateUserRequestBody{}) {
return nil, &errors.FailedToValidateUser
}

if err := utilities.AtLeastOne(userBody, models.UpdateUserRequestBody{}); err != nil {
return nil, err
if err := u.Validate.Struct(userBody); err != nil {
return nil, &errors.FailedToValidateUser
}

user, err := utilities.MapRequestToModel(userBody, &models.User{})
Expand Down
8 changes: 2 additions & 6 deletions backend/src/utilities/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ 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 AtLeastOne[Model any](body Model, model Model) bool {
return reflect.DeepEqual(body, model)
}

func validateEmail(fl validator.FieldLevel) bool {
Expand Down

0 comments on commit 0beefd5

Please sign in to comment.