Skip to content

Commit

Permalink
172 stricter editing constraints on user model fields (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye authored Mar 5, 2024
1 parent 5dc6027 commit 3f4a802
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 9 deletions.
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
12 changes: 6 additions & 6 deletions backend/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CheckServerRunning(host string, port uint16) error {
return nil
}

func ExitIfError(format string, a ...interface{}) {
func Exit(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(0)
}
Expand All @@ -37,17 +37,17 @@ func main() {

config, err := config.GetConfiguration(*configPath, *useDevDotEnv)
if err != nil {
ExitIfError("Error getting configuration: %s", err.Error())
Exit("Error getting configuration: %s", err.Error())
}

err = CheckServerRunning(config.Application.Host, config.Application.Port)
if err == nil {
ExitIfError("A server is already running on %s:%d.\n", config.Application.Host, config.Application.Port)
Exit("A server is already running on %s:%d.\n", config.Application.Host, config.Application.Port)
}

db, err := database.ConfigureDB(*config)
if err != nil {
ExitIfError("Error migrating database: %s", err.Error())
Exit("Error migrating database: %s", err.Error())
}

if *onlyMigrate {
Expand All @@ -56,13 +56,13 @@ func main() {

err = database.ConnPooling(db)
if err != nil {
ExitIfError("Error with connection pooling: %s", err.Error())
Exit("Error with connection pooling: %s", err.Error())
}

app := server.Init(db, *config)

err = app.Listen(fmt.Sprintf("%s:%d", config.Application.Host, config.Application.Port))
if err != nil {
ExitIfError("Error starting server: %s", err.Error())
Exit("Error starting server: %s", err.Error())
}
}
2 changes: 0 additions & 2 deletions backend/src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ type CreateUserRequestBody struct {
}

type UpdateUserRequestBody struct {
NUID string `json:"nuid" validate:"omitempty,numeric,len=9"`
FirstName string `json:"first_name" validate:"omitempty,max=255"`
LastName string `json:"last_name" validate:"omitempty,max=255"`
Email string `json:"email" validate:"omitempty,email,neu_email,max=255"`
College College `json:"college" validate:"omitempty,oneof=CAMD DMSB KCCS CE BCHS SL CPS CS CSSH"`
Year Year `json:"year" validate:"omitempty,min=1,max=6"`
}
Expand Down
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
4 changes: 4 additions & 0 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (c *ClubService) UpdateClub(id string, clubBody models.UpdateClubRequestBod
return nil, idErr
}

if utilities.AtLeastOne(clubBody, models.UpdateClubRequestBody{}) {
return nil, &errors.FailedToValidateClub
}

if err := c.Validate.Struct(clubBody); err != nil {
return nil, &errors.FailedToValidateClub
}
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 @@ -123,6 +123,10 @@ func (e *EventService) UpdateEvent(id string, eventBody models.UpdateEventReques
return nil, idErr
}

if utilities.AtLeastOne(eventBody, models.UpdateEventRequestBody{}) {
return nil, &errors.FailedToValidateTag
}

if err := e.Validate.Struct(eventBody); err != nil {
return nil, &errors.FailedToValidateEvent
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (t *TagService) UpdateTag(tagID string, tagBody models.UpdateTagRequestBody
return nil, idErr
}

if utilities.AtLeastOne(tagBody, models.UpdateTagRequestBody{}) {
return nil, &errors.FailedToValidateTag
}

if err := t.Validate.Struct(tagBody); err != nil {
return nil, &errors.FailedToValidateTag
}
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 @@ -82,6 +82,10 @@ func (u *UserService) UpdateUser(id string, userBody models.UpdateUserRequestBod
return nil, idErr
}

if utilities.AtLeastOne(userBody, models.UpdateUserRequestBody{}) {
return nil, &errors.FailedToValidateUser
}

if err := u.Validate.Struct(userBody); err != nil {
return nil, &errors.FailedToValidateUser
}
Expand Down
5 changes: 5 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,10 @@ func RegisterCustomValidators() (*validator.Validate, error) {
return validate, nil
}

func AtLeastOne[Model any](body Model, model Model) bool {
return reflect.DeepEqual(body, model)
}

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

0 comments on commit 3f4a802

Please sign in to comment.