-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidators.go
58 lines (52 loc) · 1.63 KB
/
validators.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
package goconf
import (
"errors"
"github.com/go-playground/validator/v10"
)
// StructValidator validates a struct's fields against the validation
// rules defined using struct tags. It utilizes the "github.com/go-playground/validator/v10"
// package for validation.
//
// Parameters:
// - config (interface{}): The struct to be validated. The struct should have validation
// rules defined using tags such as `validate:"required"`.
//
// Returns:
// - error: Returns nil if the struct passes validation. If validation fails, it returns
// an error of type `validator.ValidationErrors` which provides detailed information
// about validation failures.
//
// Usage Example:
//
// type Config struct {
// Name string `validate:"required"`
// Age int `validate:"gte=0"`
// }
//
// config := Config{
// Name: "",
// Age: -1,
// }
//
// if err := StructValidator(config); err != nil {
// // Handle validation errors, e.g., log or return
// }
//
// Note:
// - The validator is initialized with the `WithRequiredStructEnabled` option, which ensures
// that nil struct fields are treated as invalid.
// - The function will panic if the `config` parameter is not a struct or pointer to a struct.
//
// More validator information https://github.com/go-playground/validator
func StructValidator(config interface{}) error {
v := validator.New(validator.WithRequiredStructEnabled())
err := v.Struct(config)
if err != nil {
var validationErrors validator.ValidationErrors
if errors.As(err, &validationErrors) {
return validationErrors
}
return errors.Join(err, errors.New("validation failed"))
}
return nil
}