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

contraint.go   A

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 15
eloc 71
dl 0
loc 146
ccs 19
cts 19
cp 1
crap 15
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A validation.NewCustomStringConstraint 0 19 4
A validation.CustomStringConstraint.SetUp 0 2 1
A validation.notFoundConstraint.Name 0 2 1
A validation.notFoundConstraint.SetUp 0 2 1
A validation.CustomStringConstraint.Message 0 3 1
A validation.CustomStringConstraint.When 0 3 1
A validation.CustomStringConstraint.ValidateString 0 8 5
A validation.CustomStringConstraint.Name 0 2 1
1
package validation
2
3
import (
4
	"github.com/muonsoft/validation/code"
5
	"github.com/muonsoft/validation/generic"
6
	"github.com/muonsoft/validation/message"
7
8
	"time"
9
)
10
11
// Constraint is the base interface to build validation constraints.
12
type Constraint interface {
13
	Option
14
	// Name is a constraint name that can be used in internal errors.
15
	Name() string
16
}
17
18
// NilConstraint is used for constraints that need to check value for nil. In common case
19
// you do not need to implement it in your constraints because nil values should be ignored.
20
type NilConstraint interface {
21
	Constraint
22
	ValidateNil(scope Scope) error
23
}
24
25
// BoolConstraint is used to build constraints for boolean values validation.
26
type BoolConstraint interface {
27
	Constraint
28
	ValidateBool(value *bool, scope Scope) error
29
}
30
31
// NumberConstraint is used to build constraints for numeric values validation.
32
//
33
// At this moment working with numbers is based on reflection.
34
// Be aware. This constraint is subject to be changed after generics implementation in Go.
35
type NumberConstraint interface {
36
	Constraint
37
	ValidateNumber(value generic.Number, scope Scope) error
38
}
39
40
// StringConstraint is used to build constraints for string values validation.
41
type StringConstraint interface {
42
	Constraint
43
	ValidateString(value *string, scope Scope) error
44
}
45
46
// IterableConstraint is used to build constraints for validation of iterables (arrays, slices, or maps).
47
//
48
// At this moment working with numbers is based on reflection.
49
// Be aware. This constraint is subject to be changed after generics implementation in Go.
50
type IterableConstraint interface {
51
	Constraint
52
	ValidateIterable(value generic.Iterable, scope Scope) error
53
}
54
55
// CountableConstraint is used to build constraints for simpler validation of iterable elements count.
56
type CountableConstraint interface {
57
	Constraint
58
	ValidateCountable(count int, scope Scope) error
59
}
60
61
// TimeConstraint is used to build constraints for date/time validation.
62
type TimeConstraint interface {
63
	Constraint
64
	ValidateTime(value *time.Time, scope Scope) error
65
}
66
67
// CustomStringConstraint can be used to create custom constraints for validating string values
68
// based on function with signature func(string) bool.
69
type CustomStringConstraint struct {
70
	isIgnored       bool
71
	isValid         func(string) bool
72
	name            string
73
	code            string
74
	messageTemplate string
75
}
76
77
// NewCustomStringConstraint creates a new string constraint from a function with signature func(string) bool.
78
// Optional parameters can be used to set up constraint name (first parameter), violation code (second),
79
// message template (third). All other parameters are ignored.
80
func NewCustomStringConstraint(isValid func(string) bool, parameters ...string) CustomStringConstraint {
81 1
	constraint := CustomStringConstraint{
82
		isValid:         isValid,
83
		name:            "CustomStringConstraint",
84
		code:            code.NotValid,
85
		messageTemplate: message.NotValid,
86
	}
87
88 1
	if len(parameters) > 0 {
89 1
		constraint.name = parameters[0]
90
	}
91 1
	if len(parameters) > 1 {
92 1
		constraint.code = parameters[1]
93
	}
94 1
	if len(parameters) > 2 {
95 1
		constraint.messageTemplate = parameters[2]
96
	}
97
98 1
	return constraint
99
}
100
101
// SetUp always returns no error.
102
func (c CustomStringConstraint) SetUp() error {
103 1
	return nil
104
}
105
106
// Name is the constraint name. It can be set via first parameter of function NewCustomStringConstraint.
107
func (c CustomStringConstraint) Name() string {
108 1
	return c.name
109
}
110
111
// Message sets the violation message template. You can use template parameters
112
// for injecting its values into the final message:
113
//
114
//	{{ value }} - the current (invalid) value.
115
func (c CustomStringConstraint) Message(message string) CustomStringConstraint {
116 1
	c.messageTemplate = message
117 1
	return c
118
}
119
120
// When enables conditional validation of this constraint. If the expression evaluates to false,
121
// then the constraint will be ignored.
122
func (c CustomStringConstraint) When(condition bool) CustomStringConstraint {
123 1
	c.isIgnored = !condition
124 1
	return c
125
}
126
127
func (c CustomStringConstraint) ValidateString(value *string, scope Scope) error {
128 1
	if c.isIgnored || value == nil || *value == "" || c.isValid(*value) {
129 1
		return nil
130
	}
131
132 1
	return scope.BuildViolation(c.code, c.messageTemplate).
133
		AddParameter("{{ value }}", *value).
134
		CreateViolation()
135
}
136
137
type notFoundConstraint struct {
138
	key string
139
}
140
141
func (c notFoundConstraint) SetUp() error {
142 1
	return ConstraintNotFoundError{Key: c.key}
143
}
144
145
func (c notFoundConstraint) Name() string {
146 1
	return "notFoundConstraint"
147
}
148