-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
34 lines (27 loc) · 831 Bytes
/
validate.go
File metadata and controls
34 lines (27 loc) · 831 Bytes
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
package structconf
import (
"bytes"
"errors"
"fmt"
"github.com/go-playground/validator/v10"
)
func validate(configPointer any) error {
configValidator := validator.New(validator.WithRequiredStructEnabled())
err := configValidator.Struct(configPointer)
if err != nil {
var validationErrors validator.ValidationErrors
if errors.As(err, &validationErrors) {
errorMessage := &bytes.Buffer{}
for _, fieldError := range validationErrors {
validationTag := fieldError.Tag()
if validationTag == "required" {
fmt.Fprintf(errorMessage, "Missing required configuration: %s\n", fieldError.Namespace())
} else {
fmt.Fprintf(errorMessage, "Configuration error: %s - %s\n", fieldError.StructField(), fieldError.ActualTag())
}
}
return errors.New(errorMessage.String())
}
}
return nil
}