-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfirm.go
51 lines (40 loc) · 1.22 KB
/
confirm.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
package vcomplement
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"reflect"
)
// ErrInvalidConfirmation is the error that returns in case of invalid confirmation value.
var ErrInvalidConfirmation = validation.NewError("validation_invalid_confirmation", "confirmation value is invalid")
func Confirm(value interface{}) ConfirmRule {
return ConfirmRule{
realValue: value,
err: ErrInvalidConfirmation,
}
}
// ConfirmRule is a validation rule that validates if a value can confirm another value.
type ConfirmRule struct {
realValue interface{}
err validation.Error
}
// Validate checks if the given value is valid or not.
func (r ConfirmRule) Validate(value interface{}) error {
value, isNil := validation.Indirect(value)
if isNil || validation.IsEmpty(value) {
return nil
}
realVal,isNil:=validation.Indirect(r.realValue)
if isNil || !reflect.DeepEqual(realVal, value) {
return r.err
}
return nil
}
// Error sets the error message for the rule.
func (r ConfirmRule) Error(message string) ConfirmRule {
r.err = r.err.SetMessage(message)
return r
}
// ErrorObject sets the error struct for the rule.
func (r ConfirmRule) ErrorObject(err validation.Error) ConfirmRule {
r.err = err
return r
}