You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1014 B
48 lines
1014 B
4 months ago
|
package validators
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
|
||
|
"github.com/go-playground/validator/v10"
|
||
|
"repository.basistech.ru/BASIS/dynamix-standart-go-sdk/internal/multierror"
|
||
|
)
|
||
|
|
||
|
func ValidateRequest(req interface{}) error {
|
||
|
validate := getVControlValidator()
|
||
|
return validate.Struct(req)
|
||
|
}
|
||
|
|
||
|
func ValidateConfig(cfg interface{}) error {
|
||
|
validate := getVControlValidator()
|
||
|
return validate.Struct(cfg)
|
||
|
}
|
||
|
|
||
|
func ValidationError(fe validator.FieldError) error {
|
||
|
return errors.New(errorMessage(fe))
|
||
|
}
|
||
|
|
||
|
func ValidationErrors(fes []validator.FieldError) error {
|
||
|
errs := make([]error, 0, len(fes))
|
||
|
for _, fe := range fes {
|
||
|
errs = append(errs, ValidationError(fe))
|
||
|
}
|
||
|
return multierror.Join(errs...)
|
||
|
}
|
||
|
|
||
|
func GetErrors(err error) validator.ValidationErrors {
|
||
|
return err.(validator.ValidationErrors)
|
||
|
}
|
||
|
|
||
|
func IsInSlice(str string, target []string) bool {
|
||
|
for _, v := range target {
|
||
|
if v == str {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func IsInRange(num int64, target []int64) bool {
|
||
|
return num >= target[0] && num <= target[1]
|
||
|
}
|