-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
193 lines (160 loc) · 5.14 KB
/
main.go
File metadata and controls
193 lines (160 loc) · 5.14 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Package main demonstrates validation usage.
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
"github.com/yigithankarabulut/wirekit"
"github.com/yigithankarabulut/wirekit/validation"
)
// CreateUserRequest represents a user creation request.
type CreateUserRequest struct {
Username string `json:"username" validate:"required,min=3,max=20,alphanum"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=8,strong_password"`
Phone string `json:"phone" validate:"omitempty,phone"`
Role string `json:"role" validate:"required,oneof=admin user guest"`
Age int `json:"age" validate:"gte=18,lte=120"`
Website string `json:"website" validate:"omitempty,url"`
}
// UpdateProfileRequest represents a profile update request.
type UpdateProfileRequest struct {
DisplayName string `json:"display_name" validate:"omitempty,min=2,max=50"`
Bio string `json:"bio" validate:"omitempty,max=500"`
Slug string `json:"slug" validate:"omitempty,slug"`
}
var echoValidator *validation.EchoValidator
// SetupValidator creates and configures the validator.
func SetupValidator() {
fmt.Println("--- Setup Validator ---")
echoValidator = wirekit.Validator(wirekit.Echo).
WithRule("strong_password", validation.StrongPassword).
WithRule("phone", validation.Phone).
WithRule("slug", validation.Slug).
WithRule("alphanum", validation.AlphaNumeric).
WithRule("no_spaces", validation.NoSpaces).
Build()
fmt.Println("Validator configured with custom rules")
}
// ValidRequestExample demonstrates a valid request.
func ValidRequestExample() {
fmt.Println("\n--- Valid Request ---")
v := echoValidator.GetValidator()
validRequest := CreateUserRequest{
Username: "johndoe",
Email: "john@example.com",
Password: "SecurePass123!",
Phone: "+1234567890",
Role: "user",
Age: 25,
Website: "https://example.com",
}
if err := v.Struct(validRequest); err != nil {
fmt.Println("Validation failed:", err)
} else {
fmt.Println("✓ Valid request passed validation")
}
}
// InvalidRequestExample demonstrates validation errors.
func InvalidRequestExample() {
fmt.Println("\n--- Invalid Request ---")
v := echoValidator.GetValidator()
invalidRequest := CreateUserRequest{
Username: "jo", // too short
Email: "invalid-email", // not valid email
Password: "weak", // not strong enough
Phone: "123", // invalid phone
Role: "superadmin", // not in allowed list
Age: 15, // below minimum
Website: "not-a-url", // invalid URL
}
if err := v.Struct(invalidRequest); err != nil {
fmt.Println("✗ Invalid request errors:")
if validationErrors, ok := err.(validator.ValidationErrors); ok {
for _, e := range validationErrors {
fmt.Printf(" - %s: %s\n", e.Field(), e.Tag())
}
}
}
}
// EchoValidatorInterface demonstrates using EchoValidator.Validate().
func EchoValidatorInterface() {
fmt.Println("\n--- Echo Validator Interface ---")
invalidRequest := CreateUserRequest{
Username: "jo",
Email: "invalid",
Password: "weak",
Role: "unknown",
Age: 10,
}
if err := echoValidator.Validate(invalidRequest); err != nil {
fmt.Println("Validation error:", err)
}
}
// CustomValidationRules demonstrates custom validation rules.
func CustomValidationRules() {
fmt.Println("\n--- Custom Validation Rules ---")
v := echoValidator.GetValidator()
// Slug validation
profileWithValidSlug := UpdateProfileRequest{
DisplayName: "John Doe",
Bio: "Software developer",
Slug: "john-doe-123",
}
if err := v.Struct(profileWithValidSlug); err != nil {
fmt.Println("Slug validation failed:", err)
} else {
fmt.Println("✓ Valid slug: john-doe-123")
}
profileWithInvalidSlug := UpdateProfileRequest{
Slug: "invalid slug with spaces!",
}
if err := v.Struct(profileWithInvalidSlug); err != nil {
fmt.Println("✗ Invalid slug detected")
}
}
// PasswordStrengthValidation demonstrates password strength checking.
func PasswordStrengthValidation() {
fmt.Println("\n--- Password Strength Validation ---")
passwords := []string{
"weak",
"NoNumbers!",
"nonumbers123",
"SecurePass123!",
}
for _, pwd := range passwords {
isStrong := wirekit.Crypto.IsStrongPassword(pwd)
status := "✗"
if isStrong {
status = "✓"
}
fmt.Printf(" %s %s\n", status, pwd)
}
}
// EmailValidation demonstrates email validation.
func EmailValidation() {
fmt.Println("\n--- Email Validation ---")
emails := []string{
"valid@example.com",
"invalid-email",
"user@tempmail.com",
"work@company.org",
}
for _, email := range emails {
valid := wirekit.Email.IsValid(email)
disposable := wirekit.Email.IsDisposable(email)
corporate := wirekit.Email.IsCorporate(email)
fmt.Printf(" %s: valid=%v, disposable=%v, corporate=%v\n",
email, valid, disposable, corporate)
}
}
func main() {
wirekit.Log(wirekit.Zap).WithLevel("info").Init()
SetupValidator()
ValidRequestExample()
InvalidRequestExample()
EchoValidatorInterface()
CustomValidationRules()
PasswordStrengthValidation()
EmailValidation()
wirekit.Info("validation example completed")
}