-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_http_handler_test.go
91 lines (77 loc) · 2.87 KB
/
example_http_handler_test.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
package validation_test
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"github.com/muonsoft/language"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/message/translations/russian"
)
type Book struct {
Title string `json:"title"`
Author string `json:"author"`
Keywords []string `json:"keywords"`
}
func (b Book) Validate(ctx context.Context, validator *validation.Validator) error {
return validator.Validate(
ctx,
validation.StringProperty("title", b.Title, it.IsNotBlank()),
validation.StringProperty("author", b.Author, it.IsNotBlank()),
validation.CountableProperty("keywords", len(b.Keywords), it.HasCountBetween(1, 10)),
validation.EachStringProperty("keywords", b.Keywords, it.IsNotBlank()),
)
}
func HandleBooks(writer http.ResponseWriter, request *http.Request) {
var book Book
err := json.NewDecoder(request.Body).Decode(&book)
if err != nil {
http.Error(writer, "invalid request", http.StatusBadRequest)
return
}
// setting up validator
validator, err := validation.NewValidator(validation.Translations(russian.Messages))
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
err = validator.Validate(request.Context(), validation.Valid(book))
if err != nil {
violations, ok := validation.UnwrapViolationList(err)
if ok {
response, err := json.Marshal(violations)
if err != nil {
log.Fatal(err)
}
writer.WriteHeader(http.StatusUnprocessableEntity)
writer.Header().Set("Content-Type", "application/json")
writer.Write(response)
return
}
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
// handle valid book
writer.WriteHeader(http.StatusCreated)
writer.Write([]byte("ok"))
}
func ExampleValidator_Validate_httpHandler() {
var handler http.Handler
handler = http.HandlerFunc(HandleBooks)
// middleware set up: we need to set supported languages
// detected language will be passed via request context
handler = language.NewMiddleware(handler, language.SupportedLanguages(language.English, language.Russian))
// creating request with the language-specific header
request := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(`{}`))
request.Header.Set("Accept-Language", "ru")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
// recorded response should contain array of violations
fmt.Println(recorder.Body.String())
// Output:
// [{"error":"is blank","message":"Значение не должно быть пустым.","propertyPath":"title"},{"error":"is blank","message":"Значение не должно быть пустым.","propertyPath":"author"},{"error":"too few elements","message":"Эта коллекция должна содержать 1 элемент или больше.","propertyPath":"keywords"}]
}