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 notFoundConstraint struct { |
66
|
|
|
key string |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
func (c notFoundConstraint) SetUp() error { |
70
|
1 |
|
return ConstraintNotFoundError{Key: c.key} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
func (c notFoundConstraint) Name() string { |
74
|
1 |
|
return "notFoundConstraint" |
75
|
|
|
} |
76
|
|
|
|