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
|
|
|
supportsRelativeSchema bool |
17
|
|
|
schemas []string |
18
|
|
|
messageTemplate string |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// IsURL creates a URLConstraint to validate an URL. By default, constraint checks |
22
|
|
|
// only for the http:// and https:// schemas. Use the WithSchemas method to configure |
23
|
|
|
// the list of expected schemas. Also, you can use WithRelativeSchema to enable support |
24
|
|
|
// of the relative schema (without schema, e.g. "//example.com"). |
25
|
|
|
func IsURL() URLConstraint { |
26
|
1 |
|
return URLConstraint{ |
27
|
|
|
schemas: []string{"http", "https"}, |
28
|
|
|
messageTemplate: message.InvalidURL, |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
// SetUp will return an error if the list of schemas is empty. |
33
|
|
|
func (c URLConstraint) SetUp() error { |
34
|
1 |
|
if len(c.schemas) == 0 { |
35
|
1 |
|
return errEmptySchemas |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
return nil |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Name is the constraint name. |
42
|
|
|
func (c URLConstraint) Name() string { |
43
|
1 |
|
return "URLConstraint" |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// WithRelativeSchema enables support of relative URL schema, which means that URL value |
47
|
|
|
// may be treated as relative (without schema, e.g. "//example.com"). |
48
|
|
|
func (c URLConstraint) WithRelativeSchema() URLConstraint { |
49
|
1 |
|
c.supportsRelativeSchema = true |
50
|
1 |
|
return c |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// WithSchemas is used to set up a list of accepted schemas. For example, if you also consider the ftp:// type URLs |
54
|
|
|
// to be valid, redefine the schemas list, listing http, https, and also ftp. |
55
|
|
|
// If the list is empty, then an error will be returned by the SetUp method. |
56
|
|
|
func (c URLConstraint) WithSchemas(schemas ...string) URLConstraint { |
57
|
1 |
|
c.schemas = schemas |
58
|
1 |
|
return c |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Message sets the violation message template. You can use template parameters |
62
|
|
|
// for injecting its values into the final message: |
63
|
|
|
// |
64
|
|
|
// {{ value }} - the current (invalid) value. |
65
|
|
|
func (c URLConstraint) Message(message string) URLConstraint { |
66
|
1 |
|
c.messageTemplate = message |
67
|
1 |
|
return c |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
71
|
|
|
// then the constraint will be ignored. |
72
|
|
|
func (c URLConstraint) When(condition bool) URLConstraint { |
73
|
1 |
|
c.isIgnored = !condition |
74
|
1 |
|
return c |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func (c URLConstraint) ValidateString(value *string, scope validation.Scope) error { |
78
|
1 |
|
if c.isIgnored || value == nil || *value == "" { |
79
|
1 |
|
return nil |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
schemas := c.schemas |
83
|
1 |
|
if c.supportsRelativeSchema { |
84
|
1 |
|
schemas = append(schemas, "") |
85
|
|
|
} |
86
|
1 |
|
if is.URL(*value, schemas...) { |
87
|
1 |
|
return nil |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return scope.BuildViolation(code.InvalidURL, c.messageTemplate). |
91
|
|
|
AddParameter("{{ value }}", *value). |
92
|
|
|
CreateViolation() |
93
|
|
|
} |
94
|
|
|
|