-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ add validation pipeline for mediatr
- Loading branch information
1 parent
6c00afc
commit 5ff36d2
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |