Skip to content

Commit 786bada

Browse files
authored
Merge pull request #46 from Soham-Maha/master
Validation added(added error messages)
2 parents 574e377 + b1bba1c commit 786bada

File tree

10 files changed

+98
-14
lines changed

10 files changed

+98
-14
lines changed

cmd/api/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
99
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/queue"
1010
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/submission"
11+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
1112
"github.com/CodeChefVIT/cookoff-backend/internal/server"
1213
"github.com/CodeChefVIT/cookoff-backend/internal/worker"
1314
"github.com/hibiken/asynq"
@@ -20,6 +21,7 @@ func main() {
2021
database.InitCache()
2122
auth.InitJWT()
2223
submission.Init(database.RedisClient)
24+
validator.InitValidator()
2325

2426
taskServer, taskClient := queue.InitQueue("redis:6379", 2)
2527

go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22
55
require (
66
github.com/go-chi/chi/v5 v5.1.0
77
github.com/go-chi/jwtauth/v5 v5.3.1
8+
github.com/go-playground/validator v9.31.0+incompatible
89
github.com/golang-jwt/jwt/v4 v4.5.0
910
github.com/google/uuid v1.6.0
1011
github.com/jackc/pgx/v5 v5.6.0
@@ -14,11 +15,15 @@ require (
1415
)
1516

1617
require (
18+
github.com/go-playground/locales v0.14.1 // indirect
19+
github.com/go-playground/universal-translator v0.18.1 // indirect
1720
github.com/golang/protobuf v1.5.2 // indirect
21+
github.com/leodido/go-urn v1.4.0 // indirect
1822
github.com/robfig/cron/v3 v3.0.1 // indirect
1923
github.com/spf13/cast v1.3.1 // indirect
2024
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
2125
google.golang.org/protobuf v1.26.0 // indirect
26+
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
2227
)
2328

2429
require (
@@ -38,7 +43,7 @@ require (
3843
github.com/lestrrat-go/option v1.0.1 // indirect
3944
github.com/segmentio/asm v1.2.0 // indirect
4045
go.uber.org/multierr v1.10.0 // indirect
41-
golang.org/x/crypto v0.21.0 // indirect
46+
golang.org/x/crypto v0.21.0
4247
golang.org/x/sync v0.1.0 // indirect
4348
golang.org/x/sys v0.18.0 // indirect
4449
golang.org/x/text v0.14.0 // indirect

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
1717
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
1818
github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh5A=
1919
github.com/go-chi/jwtauth/v5 v5.3.1/go.mod h1:6Fl2RRmWXs3tJYE1IQGX81FsPoGqDwq9c15j52R5q80=
20+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
21+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
22+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
23+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
24+
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
25+
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
2026
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
2127
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
2228
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
@@ -45,6 +51,8 @@ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA
4551
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
4652
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4753
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
54+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
55+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
4856
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
4957
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
5058
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
@@ -128,6 +136,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
128136
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
129137
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
130138
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
139+
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
140+
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
131141
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
132142
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
133143
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/controllers/auth.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@ import (
1111
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
1212
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1313
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
14+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
1415
"github.com/google/uuid"
1516
"github.com/jackc/pgx/v5/pgtype"
1617
"golang.org/x/crypto/bcrypt"
1718
)
1819

1920
type LoginRequest struct {
20-
Email string `json:"email"`
21-
Password string `json:"password"`
21+
Email string `json:"email" validate:"required"`
22+
Password string `json:"password" validate:"required"`
2223
}
2324

2425
type SignupRequest struct {
25-
Email string `json:"email"`
26-
Name string `json:"name"`
27-
RegNo string `json:"reg_no"`
28-
Key string `json:"fuck_you"`
26+
Email string `json:"email" validate:"required"`
27+
Name string `json:"name" validate:"required"`
28+
RegNo string `json:"reg_no" validate:"required"`
29+
Key string `json:"fuck_you" validate:"required"`
2930
}
3031

3132
func SignUp(w http.ResponseWriter, r *http.Request) {
@@ -36,6 +37,11 @@ func SignUp(w http.ResponseWriter, r *http.Request) {
3637
return
3738
}
3839

40+
if err := validator.ValidatePayload(w, payload); err != nil {
41+
httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.")
42+
return
43+
}
44+
3945
if payload.Key != os.Getenv("SECRET_KEY_FUCKERS") {
4046
httphelpers.WriteError(w, http.StatusUnauthorized, "I WILL POP YOUR CHERRY BRO")
4147
return

internal/controllers/questions.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/auth"
99
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/database"
1010
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
11+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
1112
"github.com/go-chi/chi/v5"
1213
"github.com/google/uuid"
1314
"github.com/jackc/pgx/v5"
@@ -25,6 +26,17 @@ type Question struct {
2526
OutputFormat *string `json:"output_format"`
2627
}
2728

29+
type QuestionRequest struct {
30+
ID uuid.UUID `json:"id" validate:"required"`
31+
Description *string `json:"description"`
32+
Title *string `json:"title"`
33+
InputFormat *string `json:"input_format"`
34+
Points pgtype.Int4 `json:"points"`
35+
Round int32 `json:"round"`
36+
Constraints *string `json:"constraints"`
37+
OutputFormat *string `json:"output_format"`
38+
}
39+
2840
func GetAllQuestion(w http.ResponseWriter, r *http.Request) {
2941
ctx := r.Context()
3042
fetchedQuestions, err := database.Queries.GetQuestions(ctx)
@@ -79,6 +91,11 @@ func CreateQuestion(w http.ResponseWriter, r *http.Request) {
7991
return
8092
}
8193

94+
if err := validator.ValidatePayload(w, question); err != nil {
95+
httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.")
96+
return
97+
}
98+
8299
questions, err := database.Queries.CreateQuestion(ctx, db.CreateQuestionParams{
83100
ID: uuid.New(),
84101
Description: question.Description,
@@ -116,12 +133,17 @@ func DeleteQuestion(w http.ResponseWriter, r *http.Request) {
116133

117134
func UpdateQuestion(w http.ResponseWriter, r *http.Request) {
118135
ctx := r.Context()
119-
var updateQuestion Question
136+
var updateQuestion QuestionRequest
120137
if err := httphelpers.ParseJSON(r, &updateQuestion); err != nil {
121138
httphelpers.WriteError(w, http.StatusInternalServerError, err.Error())
122139
return
123140
}
124141

142+
if err := validator.ValidatePayload(w, updateQuestion); err != nil {
143+
httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.")
144+
return
145+
}
146+
125147
question, err := database.Queries.GetQuestion(ctx, updateQuestion.ID)
126148
if err != nil {
127149
if err == pgx.ErrNoRows {

internal/controllers/runcode.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1313
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
1414
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/submission"
15+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
1516
"github.com/google/uuid"
1617
)
1718

@@ -29,6 +30,11 @@ func RunCode(w http.ResponseWriter, r *http.Request) {
2930
return
3031
}
3132

33+
if err := validator.ValidatePayload(w, req); err != nil {
34+
httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.")
35+
return
36+
}
37+
3238
question_id, _ := uuid.Parse(req.QuestionID)
3339
userID, _ := auth.GetUserID(w, r)
3440

internal/controllers/submission.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import (
1313
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
1414
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
1515
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/submission"
16+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/validator"
1617
"github.com/google/uuid"
1718
)
1819

1920
type subreq struct {
20-
SourceCode string `json:"source_code"`
21-
LanguageID int `json:"language_id"`
22-
QuestionID string `json:"question_id"`
21+
SourceCode string `json:"source_code" validate:"required"`
22+
LanguageID int `json:"language_id" validate:"required"`
23+
QuestionID string `json:"question_id" validate:"required"`
2324
}
2425

2526
var JUDGE0_URI = os.Getenv("JUDGE0_URI")
@@ -34,6 +35,11 @@ func SubmitCode(w http.ResponseWriter, r *http.Request) {
3435
return
3536
}
3637

38+
if err := validator.ValidatePayload(w, req); err != nil {
39+
httphelpers.WriteError(w, http.StatusNotAcceptable, "Please provide values for all required fields.")
40+
return
41+
}
42+
3743
userID, _ := auth.GetUserID(w, r)
3844
nullUserID := uuid.NullUUID{UUID: userID, Valid: true}
3945

internal/controllers/verify_role.go renamed to internal/helpers/auth/verify_role.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package controllers
1+
package auth
22

33
import (
44
"fmt"
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package validator
2+
3+
import (
4+
"net/http"
5+
6+
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
7+
logger "github.com/CodeChefVIT/cookoff-backend/internal/helpers/logging"
8+
"github.com/go-playground/validator"
9+
)
10+
11+
var Validate *validator.Validate
12+
13+
func InitValidator() {
14+
Validate = validator.New()
15+
}
16+
17+
func ValidatePayload(w http.ResponseWriter, v any) error {
18+
if err := Validate.Struct(v); err != nil {
19+
logger.Errof("Please provide values for all required fields.")
20+
httphelpers.WriteError(w, http.StatusNotAcceptable, err.Error())
21+
return err
22+
}
23+
httphelpers.WriteJSON(w, http.StatusOK, map[string]string{"message": "Payload Validated"})
24+
return nil
25+
}

internal/middlewares/role_authorization.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package middlewares
33
import (
44
"net/http"
55

6-
"github.com/CodeChefVIT/cookoff-backend/internal/controllers"
6+
"github.com/CodeChefVIT/cookoff-backend/internal/helpers/auth"
7+
httphelpers "github.com/CodeChefVIT/cookoff-backend/internal/helpers/http"
78
)
89

910
func RoleAuthorizationMiddleware(requiredRole string) func(http.Handler) http.Handler {
1011
return func(next http.Handler) http.Handler {
1112
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12-
ok := controllers.RoleFromToken(w, r, requiredRole)
13+
ok := auth.RoleFromToken(w, r, requiredRole)
1314
if !ok {
15+
httphelpers.WriteError(w, http.StatusUnauthorized, "Not Authorized")
1416
return
1517
}
1618
next.ServeHTTP(w, r)

0 commit comments

Comments
 (0)