Test Failed
Pull Request — main (#36)
by Rushan
02:31
created

contraint.go   A

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
cc 8
eloc 53
dl 0
loc 108
ccs 2
cts 2
cp 1
crap 8
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 6 2
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 1
71
func When(condition bool) ConditionalConstraint {
72
	return ConditionalConstraint{
73
		condition: condition,
74 1
	}
75
}
76
77
func (c ConditionalConstraint) Then(constraints ...Constraint) ConditionalConstraint {
78
	c.thenConstraints = constraints
79
	return c
80
}
81
82
func (c ConditionalConstraint) Else(constraints ...Constraint) ConditionalConstraint {
83
	c.elseConstraints = constraints
84
	return c
85
}
86
87
func (c ConditionalConstraint) SetUp() error {
88
	if len(c.thenConstraints) == 0 {
89
		return errThenBranchNotSet
90
	}
91
92
	return nil
93
}
94
95
func (c ConditionalConstraint) Name() string {
96
	return "ConditionalConstraint"
97
}
98
99
type notFoundConstraint struct {
100
	key string
101
}
102
103
func (c notFoundConstraint) SetUp() error {
104
	return ConstraintNotFoundError{Key: c.key}
105
}
106
107
func (c notFoundConstraint) Name() string {
108
	return "notFoundConstraint"
109
}
110