-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
80 lines (69 loc) · 1.74 KB
/
examples_test.go
File metadata and controls
80 lines (69 loc) · 1.74 KB
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
package examples
import (
"testing"
"time"
"github.com/9ssi7/rapidval"
)
func TestBusinessValidation(t *testing.T) {
now := time.Now()
business := &Business{
Title: "A",
Description: "Too short",
FoundAt: now.Add(-24 * time.Hour),
}
v := rapidval.New()
err := v.Validate(business)
if err == nil {
t.Error("validation should fail")
}
verr, ok := err.(rapidval.ValidationErrors)
if !ok {
t.Error("error should be ValidationErrors type")
}
if len(verr) != 3 {
t.Errorf("expected 3 validation errors, got %d", len(verr))
}
// Hataları detaylı kontrol et
for _, err := range verr {
switch err.Field {
case "Title":
if err.MessageKey != rapidval.MsgMinLength {
t.Errorf("unexpected Title error key: %v", err.MessageKey)
}
if err.MessageParams["Min"] != 3 {
t.Errorf("unexpected min length param: %v", err.MessageParams["min"])
}
case "Description":
if err.MessageKey != rapidval.MsgMinLength {
t.Errorf("unexpected Description error key: %v", err.MessageKey)
}
case "FoundAt":
if err.MessageKey != rapidval.MsgDateGreaterThan {
t.Errorf("unexpected FoundAt error key: %v", err.MessageKey)
}
default:
t.Errorf("unexpected error field: %v", err)
}
}
}
func TestUserValidation(t *testing.T) {
user := &User{
FirstName: "J",
LastName: "D",
Email: "invalid-email",
Password: "123",
Age: 15,
}
v := rapidval.New()
err := v.Validate(user)
if err == nil {
t.Error("validation should fail")
}
verr, ok := err.(rapidval.ValidationErrors)
if !ok {
t.Error("error should be ValidationErrors type")
}
if len(verr) != 5 { // FirstName min, LastName min, Email invalid, Password min, Age between
t.Errorf("expected 5 validation errors, got %d", len(verr))
}
}