mediatorvalid

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2025 License: GPL-3.0 Imports: 3 Imported by: 0

README

Mediator for Go - Validation Pipeline

This project is designed to validate requests during their execution using the validator package. By integrating this validation pipeline, you can ensure input data adheres to defined rules and constraints, improving the reliability and consistency of your application.

Getting Started

Prerequisites

Mediator for Go requires Go version 1.24 or above. Make sure your environment meets this requirement before proceeding.

Installing Mediator for Go - Validation Pipeline

You can install Mediator for Go with Go's module support. Dependencies will be handled automatically when you include the import statement in your code:

import "github.com/Oleexo/mediator-go-valid"

Alternatively, to add the package manually to your project, run the following command in your terminal:

go get -u github.com/Oleexo/mediator-go-valid

After running the command, the package will be downloaded and added to your go.mod file automatically.

Usage

Registering the Validation Pipeline

You can register the validation pipeline to the Mediator container. Here's how to integrate it into your application:

package main

import (
	"github.com/Oleexo/mediator-go"
	mediatorvalid "github.com/Oleexo/mediator-go-valid"
)

func main() {
	// Create a new Mediator container and register the validation pipeline
	container := mediator.NewSendContainer(
		mediator.WithPipelineBehavior(mediatorvalid.NewValidationPipeline()),
	)

	// Your application logic goes here
}
Explanation of Components
  • mediator.NewSendContainer: Initializes a container for sending requests with behaviors or middleware pipelines attached.
  • mediator.WithPipelineBehavior: Registers custom behaviors (like validation) into the mediator pipeline. In this case, it uses mediatorvalid.NewValidationPipeline() to set up the validation rules.
Example: Adding Validation in Actions

Once you have registered the validation pipeline, any requests passed to the Mediator will be validated automatically based on the structural tags defined in your request types using the well-known go-playground/validator package. Here's an example:

type ExampleRequest struct {
	Name  string `validate:"required"`     // Name must be provided
	Email string `validate:"required,email"` // Email must be valid
}

These tags will trigger the validation checks automatically when the request is processed.

Error Handling

If validation fails, the mediator will return corresponding validation errors. Be sure to handle these errors in your code to provide appropriate feedback or logging.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCustomValidation

func WithCustomValidation(tag string, fn validator.Func, callValidationEvenIfNull ...bool) func(options *Options)

func WithStructValidation

func WithStructValidation(fn validator.StructLevelFunc, types ...interface{}) func(options *Options)

Types

type Options

type Options struct {
	Validator *validator.Validate
}

type ValidationPipeline

type ValidationPipeline struct {
	// contains filtered or unexported fields
}

func NewValidationPipeline

func NewValidationPipeline(optFns ...func(options *Options)) *ValidationPipeline

func (ValidationPipeline) Handle

func (l ValidationPipeline) Handle(_ context.Context,
	request mediator.BaseRequest,
	next mediator.RequestHandlerFunc) (interface{}, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL