-
Notifications
You must be signed in to change notification settings - Fork 3
/
ip.go
94 lines (74 loc) · 1.53 KB
/
ip.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
package validator
import (
"context"
"net"
)
type IP struct {
message string
ipv4NotAllowedMessage string
ipv6NotAllowedMessage string
allowV4 bool
allowV6 bool
whenFunc WhenFunc
skipEmpty bool
skipError bool
}
func NewIP() *IP {
return &IP{
message: "Must be a valid IP address.",
ipv4NotAllowedMessage: "Must not be an IPv4 address.",
ipv6NotAllowedMessage: "Must not be an IPv6 address.",
allowV4: true,
allowV6: true,
}
}
func (r *IP) WithMessage(v string) *IP {
rc := *r
rc.message = v
return &rc
}
func (r *IP) When(v WhenFunc) *IP {
rc := *r
rc.whenFunc = v
return &rc
}
func (r *IP) when() WhenFunc {
return r.whenFunc
}
func (r *IP) setWhen(v WhenFunc) {
r.whenFunc = v
}
func (r *IP) SkipOnEmpty() *IP {
rc := *r
rc.skipEmpty = true
return &rc
}
func (r *IP) skipOnEmpty() bool {
return r.skipEmpty
}
func (r *IP) setSkipOnEmpty(v bool) {
r.skipEmpty = v
}
func (r *IP) SkipOnError() *IP {
rs := *r
rs.skipError = true
return &rs
}
func (r *IP) shouldSkipOnError() bool {
return r.skipError
}
func (r *IP) setSkipOnError(v bool) {
r.skipError = v
}
func (r *IP) ValidateValue(_ context.Context, value any) error {
v, ok := toString(value)
if !ok {
return NewResult().WithError(NewValidationError(r.message))
}
ip := net.ParseIP(v)
if ip == nil {
return NewResult().WithError(NewValidationError(r.message))
}
// TODO: implement ipv4 and ipv4 validations
return nil
}