Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/utils/yaml/yaml_decode_wrapper.go
2070 views
1
package yaml
2
3
import (
4
"io"
5
"strings"
6
7
"github.com/go-playground/validator/v10"
8
"github.com/pkg/errors"
9
"gopkg.in/yaml.v2"
10
)
11
12
var validate *validator.Validate
13
14
// DecodeAndValidate is a wrapper for yaml Decode adding struct validation
15
func DecodeAndValidate(r io.Reader, v interface{}) error {
16
if err := yaml.NewDecoder(r).Decode(v); err != nil {
17
return err
18
}
19
if validate == nil {
20
validate = validator.New()
21
}
22
23
if err := validate.Struct(v); err != nil {
24
if _, ok := err.(*validator.InvalidValidationError); ok {
25
return err
26
}
27
errs := []string{}
28
for _, err := range err.(validator.ValidationErrors) {
29
errs = append(errs, err.Namespace()+": "+err.Tag())
30
}
31
return errors.Wrap(errors.New(strings.Join(errs, ", ")), "validation failed for these fields")
32
}
33
return nil
34
}
35
36