-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.go
More file actions
39 lines (36 loc) · 1.24 KB
/
struct.go
File metadata and controls
39 lines (36 loc) · 1.24 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
package validation
import (
"context"
"github.com/containerd/containerd/log"
"github.com/go-playground/validator/v10"
"github.com/sirupsen/logrus"
)
// lifted from https://github.com/go-playground/validator/blob/master/_examples/struct-level/main.go#L68
func ValidateStruct(ctx context.Context, validate *validator.Validate, p interface{}) error {
logger := log.G(ctx)
if err := validate.Struct(p); err != nil {
// this check is only needed when your code could produce
// an invalid value for validation such as interface with nil
if err, ok := err.(*validator.InvalidValidationError); ok {
logger.WithError(err).Error("Invalid value validation error")
return err
}
for _, err := range err.(validator.ValidationErrors) {
logger.WithFields(logrus.Fields{
"namespace": err.Namespace(),
"field": err.Field(),
"structNamespace": err.StructNamespace(),
"structField": err.StructField(),
"tag": err.Tag(),
"actualTag": err.ActualTag(),
"kind": err.Kind(),
"type": err.Type(),
"value": err.Value(),
"param": err.Param(),
logrus.ErrorKey: err,
}).Error("Validation error details")
}
return err
}
return nil
}