-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_custom_violation_test.go
144 lines (125 loc) · 4.47 KB
/
example_custom_violation_test.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package validation_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"github.com/muonsoft/validation"
"github.com/muonsoft/validation/it"
"github.com/muonsoft/validation/message/translations"
"golang.org/x/text/language"
)
// DomainError is the container that will pass the ID to DomainViolation.
type DomainError struct {
ID string // this ID will be passed into DomainViolation by DomainViolationFactory
Message string
}
func (err *DomainError) Error() string {
return err.Message
}
var ErrIsEmpty = &DomainError{ID: "IsEmpty", Message: "Value is empty."}
// DomainViolation is custom implementation of validation.Violation interface with domain
// data and custom marshaling to JSON.
type DomainViolation struct {
id string // id passed from DomainError
// required fields for implementing validation.Violation
err error
message string
messageTemplate string
parameters []validation.TemplateParameter
propertyPath *validation.PropertyPath
}
func (v *DomainViolation) Unwrap() error { return v.err }
func (v *DomainViolation) Is(target error) bool { return errors.Is(v.err, target) }
func (v *DomainViolation) Error() string { return v.err.Error() }
func (v *DomainViolation) Message() string { return v.message }
func (v *DomainViolation) MessageTemplate() string { return v.messageTemplate }
func (v *DomainViolation) Parameters() []validation.TemplateParameter { return v.parameters }
func (v *DomainViolation) PropertyPath() *validation.PropertyPath { return v.propertyPath }
// pathAsJSONPointer formats property path according to a JSON Pointer Syntax https://tools.ietf.org/html/rfc6901
func (v *DomainViolation) pathAsJSONPointer() string {
var s strings.Builder
for _, element := range v.propertyPath.Elements() {
s.WriteRune('/')
s.WriteString(element.String())
}
return s.String()
}
// MarshalJSON marshals violation data with id, message and path fields. Path is formatted
// according to JSON Pointer Syntax.
func (v *DomainViolation) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
ID string `json:"id"`
Message string `json:"message"`
PropertyPath string `json:"path"`
}{
ID: v.id,
Message: v.message,
PropertyPath: v.pathAsJSONPointer(),
})
}
// DomainViolationFactory is custom implementation for validation.ViolationFactory.
type DomainViolationFactory struct {
// reuse translations and templating from BuiltinViolationFactory
factory *validation.BuiltinViolationFactory
}
func NewDomainViolationFactory() (*DomainViolationFactory, error) {
translator, err := translations.NewTranslator()
if err != nil {
return nil, err
}
return &DomainViolationFactory{factory: validation.NewViolationFactory(translator)}, nil
}
func (factory *DomainViolationFactory) CreateViolation(err error, messageTemplate string, pluralCount int, parameters []validation.TemplateParameter, propertyPath *validation.PropertyPath, lang language.Tag) validation.Violation {
// extracting error ID from err if it implements DomainError
id := ""
var domainErr *DomainError
if errors.As(err, &domainErr) {
id = domainErr.ID
}
violation := factory.factory.CreateViolation(err, messageTemplate, pluralCount, parameters, propertyPath, lang)
return &DomainViolation{
id: id,
err: err,
message: violation.Message(),
messageTemplate: violation.MessageTemplate(),
parameters: violation.Parameters(),
propertyPath: violation.PropertyPath(),
}
}
func ExampleSetViolationFactory() {
violationFactory, err := NewDomainViolationFactory()
if err != nil {
log.Fatalln(err)
}
validator, err := validation.NewValidator(validation.SetViolationFactory(violationFactory))
if err != nil {
log.Fatalln(err)
}
err = validator.At(
// property path will be formatted according to JSON Pointer Syntax
validation.PropertyName("properties"),
validation.ArrayIndex(1),
validation.PropertyName("key"),
).ValidateString(
context.Background(),
"",
// passing DomainError implementation via a constraint method
it.IsNotBlank().WithError(ErrIsEmpty).WithMessage(ErrIsEmpty.Message),
)
marshaled, err := json.MarshalIndent(err, "", "\t")
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(marshaled))
// Output:
// [
// {
// "id": "IsEmpty",
// "message": "Value is empty.",
// "path": "/properties/1/key"
// }
// ]
}