Skip to content

Commit b8aac94

Browse files
committed
feat: add postDTO model validation middleware
1 parent 4bd5017 commit b8aac94

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

internal/middleware/validation.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package middleware
2+
3+
import (
4+
"Blogging-Platform-API/internal/model"
5+
"context"
6+
"encoding/json"
7+
"io"
8+
"log"
9+
"net/http"
10+
)
11+
12+
const PostKey = "post"
13+
14+
func ValidatePostDTO(handler http.Handler) http.Handler {
15+
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
16+
requestBodyBytes, err := io.ReadAll(request.Body)
17+
if err != nil || len(requestBodyBytes) == 0 {
18+
log.Print("Error: Request Body cannot be empty")
19+
http.Error(writer, "Request Body cannot be empty", http.StatusBadRequest)
20+
return
21+
}
22+
var post model.PostDTO
23+
err = json.Unmarshal(requestBodyBytes, &post)
24+
if err != nil {
25+
log.Print("Error: Unmarshalling request Body")
26+
http.Error(writer, "Invalid request Body", http.StatusBadRequest)
27+
return
28+
}
29+
if post.Title == "" {
30+
log.Print("Error: Title field cannot be empty")
31+
http.Error(writer, "Title field cannot be empty", http.StatusUnprocessableEntity)
32+
return
33+
}
34+
if len(post.Title) > 255 {
35+
log.Print("Error: Invalid title length ", len(post.Title))
36+
http.Error(writer, "Title field is too big", http.StatusUnprocessableEntity)
37+
return
38+
}
39+
if post.Content == "" {
40+
log.Print("Error: Content field is empty")
41+
http.Error(writer, "Content field is empty", http.StatusUnprocessableEntity)
42+
return
43+
}
44+
if len(post.Category) >= 30 {
45+
log.Print("Error: Category field is too big ", len(post.Category))
46+
http.Error(writer, "Error: Category field is too big ", http.StatusUnprocessableEntity)
47+
return
48+
}
49+
ctx := context.WithValue(request.Context(), PostKey, post)
50+
handler.ServeHTTP(writer, request.WithContext(ctx))
51+
})
52+
}

internal/model/post.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ type Post struct {
1313
CreatedAt time.Time `json:"created_at"`
1414
UpdatedAt time.Time `json:"updated_at"`
1515
}
16+
17+
type PostDTO struct {
18+
// length until 255
19+
Title string `json:"title"`
20+
Content string `json:"content"`
21+
// length until 30
22+
Category string `json:"category"`
23+
Tags []string `json:"tags"`
24+
CreatedAt time.Time `json:"created_at"`
25+
UpdatedAt time.Time `json:"updated_at"`
26+
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88
)
99

1010
func main() {
11-
http.Handle("/posts", middleware.RequestIDGeneratorMiddleware(middleware.LoggingMiddleware(http.HandlerFunc(handler.TaskCreationHandler))))
11+
http.Handle("POST /posts", middleware.RequestIDGeneratorMiddleware(middleware.LoggingMiddleware(middleware.ValidatePostDTO(http.HandlerFunc(handler.TaskCreationHandler)))))
1212
log.Fatal(http.ListenAndServe(":8080", nil))
1313
}

0 commit comments

Comments
 (0)