1
|
|
|
package it |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"github.com/muonsoft/validation" |
5
|
|
|
"github.com/muonsoft/validation/code" |
6
|
|
|
"github.com/muonsoft/validation/is" |
7
|
|
|
"github.com/muonsoft/validation/message" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// URLConstraint is used to validate URL string. This constraint doesn’t check that the host of the |
11
|
|
|
// given URL really exists, because the information of the DNS records is not reliable. |
12
|
|
|
// |
13
|
|
|
// This constraint doesn't check the length of the URL. Use LengthConstraint to check the length of the given value. |
14
|
|
|
type URLConstraint struct { |
15
|
|
|
isIgnored bool |
16
|
|
|
isValid func(value string, protocols ...string) bool |
17
|
|
|
protocols []string |
18
|
|
|
messageTemplate string |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// IsURL creates a URLConstraint to validate an absolute URL, which means a protocol (or scheme) is required. |
22
|
|
|
// By default, constraint checks only for the http:// and https:// protocols. Use the Protocols method to configure |
23
|
|
|
// the list of expected protocols. |
24
|
|
|
// |
25
|
|
|
// Example |
26
|
|
|
// v := "http://example.com" |
27
|
|
|
// err := validator.ValidateString(&v, it.IsURL()) |
28
|
|
|
func IsURL() URLConstraint { |
29
|
1 |
|
return URLConstraint{ |
30
|
|
|
isValid: is.URL, |
31
|
|
|
protocols: []string{"http", "https"}, |
32
|
|
|
messageTemplate: message.InvalidURL, |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// IsRelativeURL creates a URLConstraint to validate an absolute or relative URL. The protocol is considered |
37
|
|
|
// optional when validating the syntax of the given URL. This means that both http:// and https:// are valid |
38
|
|
|
// but also relative URLs that contain no protocol (e.g. //example.com). |
39
|
|
|
// By default, constraint checks only for the http:// and https:// protocols. Use the Protocols method to configure |
40
|
|
|
// the list of expected protocols. |
41
|
|
|
// |
42
|
|
|
// Example |
43
|
|
|
// v := "//example.com" |
44
|
|
|
// err := validator.ValidateString(&v, it.IsRelativeURL()) |
45
|
|
|
func IsRelativeURL() URLConstraint { |
46
|
1 |
|
return URLConstraint{ |
47
|
|
|
isValid: is.RelativeURL, |
48
|
|
|
protocols: []string{"http", "https"}, |
49
|
|
|
messageTemplate: message.InvalidURL, |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// SetUp will return an error if the list of protocols is empty. |
54
|
|
|
func (c URLConstraint) SetUp() error { |
55
|
1 |
|
if len(c.protocols) == 0 { |
56
|
1 |
|
return errEmptyProtocols |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return nil |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Name is the constraint name. |
63
|
|
|
func (c URLConstraint) Name() string { |
64
|
1 |
|
return "URLConstraint" |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Protocols is used to set up a list of accepted protocols. For example, if you also consider the ftp:// type URLs |
68
|
|
|
// to be valid, redefine the protocols list, listing http, https, and also ftp. |
69
|
|
|
// If the list is empty, then an error will be returned by the SetUp method. |
70
|
|
|
// |
71
|
|
|
// Example |
72
|
|
|
// v := "ftp://example.com" |
73
|
|
|
// err := validator.ValidateString(&v, it.IsURL().Protocols("http", "https", "ftp")) |
74
|
|
|
func (c URLConstraint) Protocols(protocols ...string) URLConstraint { |
75
|
1 |
|
c.protocols = protocols |
76
|
1 |
|
return c |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Message sets the violation message template. You can use template parameters |
80
|
|
|
// for injecting its values into the final message: |
81
|
|
|
// |
82
|
|
|
// {{ value }} - the current (invalid) value. |
83
|
|
|
func (c URLConstraint) Message(message string) URLConstraint { |
84
|
1 |
|
c.messageTemplate = message |
85
|
1 |
|
return c |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
89
|
|
|
// then the constraint will be ignored. |
90
|
|
|
func (c URLConstraint) When(condition bool) URLConstraint { |
91
|
1 |
|
c.isIgnored = !condition |
92
|
1 |
|
return c |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
func (c URLConstraint) ValidateString(value *string, scope validation.Scope) error { |
96
|
1 |
|
if c.isIgnored || value == nil || *value == "" || c.isValid(*value, c.protocols...) { |
97
|
1 |
|
return nil |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return scope.BuildViolation(code.InvalidURL, c.messageTemplate). |
101
|
|
|
AddParameter("{{ value }}", *value). |
102
|
|
|
CreateViolation() |
103
|
|
|
} |
104
|
|
|
|