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

     1  package inputvalidation
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/99designs/gqlgen/graphql"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     9  )
    10  
    11  // Validatable missing godoc
    12  type Validatable interface {
    13  	Validate() error
    14  }
    15  
    16  type directive struct{}
    17  
    18  // NewDirective missing godoc
    19  func NewDirective() *directive {
    20  	return &directive{}
    21  }
    22  
    23  // Validate missing godoc
    24  func (d *directive) Validate(ctx context.Context, obj interface{}, next graphql.Resolver) (interface{}, error) {
    25  	constructedObj, err := next(ctx)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	validatableObj, ok := constructedObj.(Validatable)
    31  	if !ok {
    32  		return nil, apperrors.NewInternalError(fmt.Sprintf("misuse of directive, object is not validatable: %T", constructedObj))
    33  	}
    34  
    35  	return validatableObj, Validate(validatableObj)
    36  }