Skip to content

Can't register a custom validator using Echo#Validator #815

@huyhvq

Description

@huyhvq

Description

I used example code with validate and it's return me some errors.

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

Should validate data with 3rd party suck as https://github.com/go-playground/validator

Actual behaviour

>> go run server.go
# command-line-arguments
./server.go:27: e.Validator undefined (type *echo.Echo has no field or method Validator)
./server.go:33: c.Validate undefined (type echo.Context has no field or method Validate)

Steps to reproduce

run the code below

Working code to debug

package main

import (
    "net/http"

    "github.com/labstack/echo"
    "gopkg.in/go-playground/validator.v9"
)

type (
    User struct {
        Name  string `json:"name" validate:"required"`
        Email string `json:"email" validate:"required,email"`
    }

    CustomValidator struct {
        validator *validator.Validate
    }
)

func (cv *CustomValidator) Validate(i interface{}) error {
    return cv.validator.Struct(i)
}

func main() {
    e := echo.New()
    e.Validator = &CustomValidator{validator: validator.New()}
    e.POST("/users", func(c echo.Context) (err error) {
        u := new(User)
        if err = c.Bind(u); err != nil {
            return
        }
        if err = c.Validate(u); err != nil {
            return
        }
        return c.JSON(http.StatusOK, u)
    })
    e.Logger.Fatal(e.Start(":1323"))
}

Version/commit

go1.7.3 darwin/amd64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions