This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/
validator.go
61 lines (51 loc) · 1.86 KB
/
validator.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
package endpoint
import (
"context"
"github.com/jmalloc/ax"
)
// SelfValidatingMessage is a message that can perform its own superficial
// validation.
type SelfValidatingMessage interface {
ax.Message
// Validate returns a non-nil error if the message is invalid. It is up to
// message implementation to check validity criteria.
//
// This method is invoked by SelfValidator that is one the default
// validators to verify the message.
Validate() error
}
// Validator is an interface for validating messages.
//
// Application-defined validators can be implemented to provide superficial and
// domain validation. Each endpoint has a set of validators that are used to
// validate outgoing messages. Additionally, the validation.InboundRejecter and
// validation.OutboundRejecter can be used to perform message validation at any
// point in a pipeline.
type Validator interface {
// Validate checks if m is valid.
//
// It returns a non-nil error if the message is invalid. The meaning of
// 'valid' in is implementation-defined.
Validate(ctx context.Context, m ax.Message) error
}
// DefaultValidators is the set of validators used to validate outgoing messages
// if no other set of validators is configured on the endpoint.
var DefaultValidators = []Validator{
&SelfValidator{},
}
// SelfValidator is one of the default message validators
// that validates the message if it implements SelfValidatingMessage interface.
type SelfValidator struct{}
// Validate validates m if it implements SelfValidatingMessage. It returns the
// error returned by m.Validate(). If m does not implement SelfValidatingMessage
// then no validation is performed and nil is returned.
func (SelfValidator) Validate(
ctx context.Context,
m ax.Message,
) error {
// check if message can perform self-validation
if s, ok := m.(SelfValidatingMessage); ok {
return s.Validate()
}
return nil
}