Skip to content

Commit

Permalink
feat: ✨ add validation pipeline for mediatr
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Dec 18, 2023
1 parent 6c00afc commit 5ff36d2
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/pkg/validation/pipeline/validation_pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pipeline

import (
"context"

customErrors "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/http/httperrors/customerrors"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/validation"

"github.com/mehdihadeli/go-mediatr"
)

type mediatorValidationPipeline struct {
logger logger.Logger
}

func NewMediatorValidationPipeline(l logger.Logger) mediatr.PipelineBehavior {
return &mediatorValidationPipeline{logger: l}
}

func (m mediatorValidationPipeline) Handle(
ctx context.Context,
request interface{},
next mediatr.RequestHandlerFunc,
) (interface{}, error) {
v, ok := request.(validation.Validator)
if ok {
err := v.Validate()
if err != nil {
validationErr := customErrors.NewValidationErrorWrap(err, "validation failed")

return nil, validationErr
}
}

return next(ctx)
}

0 comments on commit 5ff36d2

Please sign in to comment.