-
Notifications
You must be signed in to change notification settings - Fork 224
/
required.go
74 lines (65 loc) · 2.12 KB
/
required.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package validation
var (
// ErrRequired is the error that returns when a value is required.
ErrRequired = NewError("validation_required", "cannot be blank")
// ErrNilOrNotEmpty is the error that returns when a value is not nil and is empty.
ErrNilOrNotEmpty = NewError("validation_nil_or_not_empty_required", "cannot be blank")
)
// Required is a validation rule that checks if a value is not empty.
// A value is considered not empty if
// - integer, float: not zero
// - bool: true
// - string, array, slice, map: len() > 0
// - interface, pointer: not nil and the referenced value is not empty
// - any other types
var Required = RequiredRule{skipNil: false, condition: true}
// NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty.
// NilOrNotEmpty differs from Required in that it treats a nil pointer as valid.
var NilOrNotEmpty = RequiredRule{skipNil: true, condition: true}
// RequiredRule is a rule that checks if a value is not empty.
type RequiredRule struct {
condition bool
skipNil bool
err Error
}
// Validate checks if the given value is valid or not.
func (r RequiredRule) Validate(value interface{}) error {
if r.condition {
value, isNil := Indirect(value)
if r.skipNil && !isNil && IsEmpty(value) || !r.skipNil && (isNil || IsEmpty(value)) {
if r.err != nil {
return r.err
}
if r.skipNil {
return ErrNilOrNotEmpty
}
return ErrRequired
}
}
return nil
}
// When sets the condition that determines if the validation should be performed.
func (r RequiredRule) When(condition bool) RequiredRule {
r.condition = condition
return r
}
// Error sets the error message for the rule.
func (r RequiredRule) Error(message string) RequiredRule {
if r.err == nil {
if r.skipNil {
r.err = ErrNilOrNotEmpty
} else {
r.err = ErrRequired
}
}
r.err = r.err.SetMessage(message)
return r
}
// ErrorObject sets the error struct for the rule.
func (r RequiredRule) ErrorObject(err Error) RequiredRule {
r.err = err
return r
}