-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.go
More file actions
45 lines (40 loc) · 780 Bytes
/
rule.go
File metadata and controls
45 lines (40 loc) · 780 Bytes
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
package vd
import (
"strings"
)
type Rule struct {
Fail bool
Message string
Path []string
Format Formatter
error error
}
func (r *Rule) Error(err error) {
if err != nil {
r.error = err
}
}
func (r *Rule) Break(message string, path string) {
r.Fail = true
r.Message = message
r.Path = append(r.Path, path)
}
func (r *Rule) Validator(v interface {
Validator(err ...error) error
}, failMessage string, path string) {
if r.Fail {
return
}
err := v.Validator()
if err != nil {
r.Break(failMessage, path)
}
}
func (r Rule) CreateMessage(message string, customMessage func() string) string {
message = strings.TrimPrefix(message, " ")
message = strings.TrimSuffix(message, " ")
if len(message) == 0 {
return customMessage()
}
return message
}