Passed
Push — main ( 815f74...226e09 )
by Rushan
02:04 queued 12s
created

it.IsEmail   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 0
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
// IsEmail is used for simplified validation of an email address. It allows all values
11
// with an "@" symbol in, and a "." in the second host part of the email address.
12
func IsEmail() validation.CustomStringConstraint {
13 1
	return validation.NewCustomStringConstraint(
14
		is.Email,
15
		"EmailConstraint",
16
		code.InvalidEmail,
17
		message.InvalidEmail,
18
	)
19
}
20
21
// IsHTML5Email is used for validation of an email address based on pattern for HTML5
22
// (see https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address).
23
func IsHTML5Email() validation.CustomStringConstraint {
24 1
	return validation.NewCustomStringConstraint(
25
		is.HTML5Email,
26
		"HTML5EmailConstraint",
27
		code.InvalidEmail,
28
		message.InvalidEmail,
29
	)
30
}
31
32
// URLConstraint is used to validate URL string. This constraint doesn’t check that the host of the
33
// given URL really exists, because the information of the DNS records is not reliable.
34
//
35
// This constraint doesn't check the length of the URL. Use LengthConstraint to check the length of the given value.
36
type URLConstraint struct {
37
	isIgnored              bool
38
	supportsRelativeSchema bool
39
	schemas                []string
40
	messageTemplate        string
41
}
42
43
// IsURL creates a URLConstraint to validate an URL. By default, constraint checks
44
// only for the http:// and https:// schemas. Use the WithSchemas method to configure
45
// the list of expected schemas. Also, you can use WithRelativeSchema to enable support
46
// of the relative schema (without schema, e.g. "//example.com").
47
func IsURL() URLConstraint {
48 1
	return URLConstraint{
49
		schemas:         []string{"http", "https"},
50
		messageTemplate: message.InvalidURL,
51
	}
52
}
53
54
// SetUp will return an error if the list of schemas is empty.
55
func (c URLConstraint) SetUp() error {
56 1
	if len(c.schemas) == 0 {
57 1
		return errEmptySchemas
58
	}
59
60 1
	return nil
61
}
62
63
// Name is the constraint name.
64
func (c URLConstraint) Name() string {
65 1
	return "URLConstraint"
66
}
67
68
// WithRelativeSchema enables support of relative URL schema, which means that URL value
69
// may be treated as relative (without schema, e.g. "//example.com").
70
func (c URLConstraint) WithRelativeSchema() URLConstraint {
71 1
	c.supportsRelativeSchema = true
72 1
	return c
73
}
74
75
// WithSchemas is used to set up a list of accepted schemas. For example, if you also consider the ftp:// type URLs
76
// to be valid, redefine the schemas list, listing http, https, and also ftp.
77
// If the list is empty, then an error will be returned by the SetUp method.
78
func (c URLConstraint) WithSchemas(schemas ...string) URLConstraint {
79 1
	c.schemas = schemas
80 1
	return c
81
}
82
83
// Message sets the violation message template. You can use template parameters
84
// for injecting its values into the final message:
85
//
86
//	{{ value }} - the current (invalid) value.
87
func (c URLConstraint) Message(message string) URLConstraint {
88 1
	c.messageTemplate = message
89 1
	return c
90
}
91
92
// When enables conditional validation of this constraint. If the expression evaluates to false,
93
// then the constraint will be ignored.
94
func (c URLConstraint) When(condition bool) URLConstraint {
95 1
	c.isIgnored = !condition
96 1
	return c
97
}
98
99
func (c URLConstraint) ValidateString(value *string, scope validation.Scope) error {
100 1
	if c.isIgnored || value == nil || *value == "" {
101 1
		return nil
102
	}
103
104 1
	schemas := c.schemas
105 1
	if c.supportsRelativeSchema {
106 1
		schemas = append(schemas, "")
107
	}
108 1
	if is.URL(*value, schemas...) {
109 1
		return nil
110
	}
111
112 1
	return scope.BuildViolation(code.InvalidURL, c.messageTemplate).
113
		AddParameter("{{ value }}", *value).
114
		CreateViolation()
115
}
116