Passed
Pull Request — main (#36)
by Rushan
02:12 queued 31s
created

contraint.go   A

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
cc 9
eloc 55
dl 0
loc 111
ccs 10
cts 13
cp 0.7692
crap 9.9957
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validation.ConditionalConstraint.Name 0 2 1
A validation.ConditionalConstraint.Else 0 3 1
A validation.notFoundConstraint.Name 0 2 1
A validation.notFoundConstraint.SetUp 0 2 1
A validation.When 0 3 1
A validation.ConditionalConstraint.Then 0 3 1
A validation.ConditionalConstraint.SetUp 0 9 3
1
package validation
2
3
import (
4
	"github.com/muonsoft/validation/generic"
5
6
	"time"
7
)
8
9
// Constraint is the base interface to build validation constraints.
10
type Constraint interface {
11
	Option
12
	// Name is a constraint name that can be used in internal errors.
13
	Name() string
14
}
15
16
// NilConstraint is used for constraints that need to check value for nil. In common case
17
// you do not need to implement it in your constraints because nil values should be ignored.
18
type NilConstraint interface {
19
	Constraint
20
	ValidateNil(scope Scope) error
21
}
22
23
// BoolConstraint is used to build constraints for boolean values validation.
24
type BoolConstraint interface {
25
	Constraint
26
	ValidateBool(value *bool, scope Scope) error
27
}
28
29
// NumberConstraint is used to build constraints for numeric values validation.
30
//
31
// At this moment working with numbers is based on reflection.
32
// Be aware. This constraint is subject to be changed after generics implementation in Go.
33
type NumberConstraint interface {
34
	Constraint
35
	ValidateNumber(value generic.Number, scope Scope) error
36
}
37
38
// StringConstraint is used to build constraints for string values validation.
39
type StringConstraint interface {
40
	Constraint
41
	ValidateString(value *string, scope Scope) error
42
}
43
44
// IterableConstraint is used to build constraints for validation of iterables (arrays, slices, or maps).
45
//
46
// At this moment working with numbers is based on reflection.
47
// Be aware. This constraint is subject to be changed after generics implementation in Go.
48
type IterableConstraint interface {
49
	Constraint
50
	ValidateIterable(value generic.Iterable, scope Scope) error
51
}
52
53
// CountableConstraint is used to build constraints for simpler validation of iterable elements count.
54
type CountableConstraint interface {
55
	Constraint
56
	ValidateCountable(count int, scope Scope) error
57
}
58
59
// TimeConstraint is used to build constraints for date/time validation.
60
type TimeConstraint interface {
61
	Constraint
62
	ValidateTime(value *time.Time, scope Scope) error
63
}
64
65
type ConditionalConstraint struct {
66
	condition       bool
67
	thenConstraints []Constraint
68
	elseConstraints []Constraint
69
}
70
71
func When(condition bool) ConditionalConstraint {
72 1
	return ConditionalConstraint{
73
		condition: condition,
74
	}
75
}
76
77
func (c ConditionalConstraint) Then(constraints ...Constraint) ConditionalConstraint {
78 1
	c.thenConstraints = constraints
79 1
	return c
80
}
81
82
func (c ConditionalConstraint) Else(constraints ...Constraint) ConditionalConstraint {
83 1
	c.elseConstraints = constraints
84 1
	return c
85
}
86
87
func (c ConditionalConstraint) SetUp() error {
88 1
	if len(c.thenConstraints) == 0 {
89
		return errThenBranchNotSet
90
	}
91 1
	if len(c.elseConstraints) == 0 {
92
		return errElseBranchNotSet
93
	}
94
95 1
	return nil
96
}
97
98
func (c ConditionalConstraint) Name() string {
99
	return "ConditionalConstraint"
100
}
101
102
type notFoundConstraint struct {
103
	key string
104
}
105
106
func (c notFoundConstraint) SetUp() error {
107 1
	return ConstraintNotFoundError{Key: c.key}
108
}
109
110
func (c notFoundConstraint) Name() string {
111 1
	return "notFoundConstraint"
112
}
113