-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidate.go
More file actions
61 lines (51 loc) · 1.83 KB
/
Copy pathvalidate.go
File metadata and controls
61 lines (51 loc) · 1.83 KB
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
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"context"
"errors"
"reflect"
)
// Validate performs a set of validations on a message. If no validators are
// provided, the default set of standard WRP validators is used. If the
// NoStandardValidation() processor is provided, no standard validation is
// performed. After standard validation (if applicable) is performed, any
// additional validators are executed in the order they are provided. If any
// validator returns an error excluding ErrNotHandled, the iteration stops and
// the error is returned. If a validator return ErrNotHandled, then the
// validation is considered successful. Any combination of nil errors and
// ErrNotHandled is considered a successful validation. All other errors are
// considered validation failures and the first encountered error is returned.
func Validate(msg Union, validators ...Processor) error {
if msg == nil || reflect.ValueOf(msg).IsNil() {
return ErrMessageIsInvalid
}
if m, ok := msg.(*Message); ok {
return validate(m, validators...)
}
return msg.To(&Message{}, validators...)
}
// SkipStandardValidation returns true if the provided processors contain
// the NoStandardValidation() provided processor.
func SkipStandardValidation(p []Processor) bool {
for _, v := range p {
if _, ok := v.(noStandardValidation); ok {
return true
}
}
return false
}
func validate(msg *Message, validators ...Processor) error {
defaults := []Processor{ // nolint:prealloc
StandardValidator(),
}
if SkipStandardValidation(validators) {
defaults = nil
}
validators = append(defaults, validators...)
err := Processors(validators).ProcessWRP(context.Background(), *msg)
if err == nil || errors.Is(err, ErrNotHandled) {
return nil
}
return err
}