-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.go
117 lines (97 loc) · 3.17 KB
/
validate.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
package validate
import (
"reflect"
"github.com/metafates/schema/internal/reflectwalk"
)
type (
// Validator is an interface that validators must implement.
// It's a special empty (struct{}) type that is invoked in a form of (*new(V)).Validate(...).
// Therefore it should not depend on inner state (fields).
Validator[T any] interface {
Validate(value T) error
}
// TypeValidateable is an interface for types that can validate their types.
// This is used by required and optional fields so that they can validate if contained values
// satisfy the schema enforced by [Validator] backed type.
//
// TL;DR: do not implement nor use this method directly (codegen is exception).
//
// See [Validateable] interface if you want to implement custom validation
TypeValidateable interface {
TypeValidate() error
}
// Validateable is an interface for types that can perform validation logic after
// type validation (by [TypeValidateable]) has been called without errors.
//
// Primary usecase is custom cross-field validation. E.g. if X is true then Y cannot be empty
Validateable interface {
Validate() error
}
)
// Validate checks if the provided value can be validated and reports any validation errors.
//
// The validation process follows these steps:
// 1. If v implements both [TypeValidateable] and [Validateable] interfaces, its [TypeValidateable.TypeValidate]
// method is called, followed by its [Validateable.Validate] method.
// 2. If v only implements the [TypeValidateable] interface, its [TypeValidateable.TypeValidate] method is called.
// 3. Otherwise, Validate traverses all fields in the struct pointed to by v, applying the same validation
// logic to each field.
//
// A [ValidationError] is returned if any validation fails during any step.
//
// If v is nil or not a pointer, Validate returns an [InvalidValidateError].
func Validate(v any) error {
switch v := v.(type) {
case interface {
TypeValidateable
Validateable
}:
if err := v.TypeValidate(); err != nil {
return ValidationError{Inner: err}
}
if err := v.Validate(); err != nil {
return ValidationError{Inner: err}
}
return nil
case TypeValidateable:
if err := v.TypeValidate(); err != nil {
return ValidationError{Inner: err}
}
return nil
}
// same thing [json.Unmarshal] does
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Pointer || rv.IsNil() {
return &InvalidValidateError{Type: reflect.TypeOf(v)}
}
var postValidate []func() error
err := reflectwalk.WalkFields(v, func(path string, reflectValue reflect.Value) error {
if reflectValue.CanAddr() {
reflectValue = reflectValue.Addr()
}
value := reflectValue.Interface()
if value, ok := value.(TypeValidateable); ok {
if err := value.TypeValidate(); err != nil {
return ValidationError{Inner: err, path: path}
}
}
if value, ok := value.(Validateable); ok {
postValidate = append(postValidate, func() error {
if err := value.Validate(); err != nil {
return ValidationError{Inner: err, path: path}
}
return nil
})
}
return nil
})
if err != nil {
return err
}
for _, f := range postValidate {
if err := f(); err != nil {
return err
}
}
return nil
}