github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/helpers.go (about)

     1  package inputvalidation
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	validation "github.com/go-ozzo/ozzo-validation/v4"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     9  )
    10  
    11  // Validate missing godoc
    12  func Validate(validatable Validatable) error {
    13  	var typeName string
    14  	split := strings.Split(fmt.Sprintf("%T", validatable), ".")
    15  	if len(split) > 1 {
    16  		typeName = split[1]
    17  	} else {
    18  		typeName = split[0]
    19  	}
    20  
    21  	if err := validatable.Validate(); err != nil {
    22  		switch value := err.(type) {
    23  		case validation.Errors:
    24  			return apperrors.NewInvalidDataErrorWithFields(value, typeName)
    25  		case validation.InternalError:
    26  			return apperrors.InternalErrorFrom(value, "while validating")
    27  		case apperrors.Error:
    28  			return err
    29  		default:
    30  			return apperrors.NewInternalError(fmt.Sprintf("%+v error is not handled", value))
    31  		}
    32  	}
    33  	return nil
    34  }