-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.go
99 lines (82 loc) · 1.99 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"encoding/json"
"errors"
"net/http"
"slices"
"strings"
)
// validate_chirp checks if the received message has the length of 140 or below.
func validate_chirp(r *http.Request) (string, error) {
var bad_words = []string{
"kerfuffle",
"sharbert",
"fornax",
}
type message struct {
Body string `json:"body"`
}
// json receive part
decoder := json.NewDecoder(r.Body)
msg := message{}
err := decoder.Decode(&msg)
if err != nil {
return "", err
}
// validate message
l := len(msg.Body)
if l > 0 && l <= 140 {
resp := Sanitize(msg.Body, bad_words)
return resp, nil
} else if l > 140 {
return "", errors.New("message too long: message > 140 characters")
} else {
return "", errors.New("something went wrong during validation")
}
}
func Sanitize(s string, words []string) string {
var return_text []string
toSlice := strings.Split(s, " ")
for _, word := range toSlice {
if slices.Contains(words, strings.ToLower(word)) {
return_text = append(return_text, "****")
} else {
return_text = append(return_text, word)
}
}
return strings.Join(return_text, " ")
}
// func validate_chirp(w http.ResponseWriter, r *http.Request) {
// var bad_words = []string{
// "kerfuffle",
// "sharbert",
// "fornax",
// }
// type message struct {
// Body string `json:"body"`
// }
// type cleaned_body struct {
// Cleaned_body string `json:"cleaned_body"`
// }
// // json receive part
// decoder := json.NewDecoder(r.Body)
// msg := message{}
// err := decoder.Decode(&msg)
// if err != nil {
// log.Printf("Error decoding message body: %s", err)
// w.WriteHeader(500)
// return
// }
// // validate message
// l := len(msg.Body)
// if l > 0 && l <= 140 {
// resp := cleaned_body{Cleaned_body: Sanitize(msg.Body, bad_words)}
// respondWithJSON(w, 200, resp)
// } else if l > 140 {
// msg := "Chirp is too long"
// respondWithError(w, 400, msg)
// } else {
// msg := "Something went wrong"
// respondWithError(w, 400, msg)
// }
// }