github.com/gudimz/urlShortener@v0.0.0-20230129195305-c8ee33059a67/internal/server/validator.go (about)

     1  package server
     2  
     3  import (
     4  	"github.com/go-playground/validator/v10"
     5  	"github.com/labstack/echo/v4"
     6  	"net/http"
     7  )
     8  
     9  type Validator struct {
    10  	v *validator.Validate
    11  }
    12  
    13  func NewValidator() *Validator {
    14  	return &Validator{v: validator.New()}
    15  }
    16  
    17  func (v *Validator) Validate(i interface{}) error {
    18  	err := v.v.Struct(i)
    19  	if err != nil {
    20  		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
    21  	}
    22  	return nil
    23  }