Skip to content

How do I bind and validate path params? #2451

Answered by aldas
smares asked this question in Q&A
Discussion options

You must be logged in to vote

@smares , you function would work like this

func ValidateRequest(c echo.Context, i interface{}) error {
	if err := c.Bind(i); err != nil { // <-- no '&' here. this is already passed as reference
		return err
	}

	if err := c.Validate(i); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}

	return nil
}

and call it as

		if err := ValidateRequest(c, &payload); err != nil {  // <--- dereference is done here
			return err
		}

and full example:

package main

import (
	"errors"
	"github.com/go-playground/validator/v10"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"log"
	"net/http"
)

func ValidateRequest(c echo.Context, i interface{}) error

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@aldas
Comment options

@smares
Comment options

@smares
Comment options

@aldas
Comment options

Answer selected by smares
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants