Created
April 18, 2020 13:24
-
-
Save NimaGhaedsharafi/0c879ddd0b4b6600042307463714a5e4 to your computer and use it in GitHub Desktop.
Validator
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
package cmd | |
import ( | |
v10 "github.com/go-playground/validator/v10" | |
"github.com/labstack/echo/v4" | |
"log" | |
) | |
func Serve() { | |
e := echo.New() | |
e.Validator = &validator.Validator{Validator: v10.New()} | |
registerRoutes(e) | |
err := e.Start(":8000") | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
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
package place | |
import ( | |
"github.com/labstack/echo/v4" | |
"net/http" | |
) | |
func Store(c echo.Context) error { | |
var input ReqStore | |
if err := c.Bind(&input); err != nil { | |
return c.NoContent(http.StatusBadRequest) | |
} | |
if err := c.Validate(input); err != nil { | |
return c.NoContent(http.StatusBadRequest) | |
} | |
return c.NoContent(http.StatusCreated) | |
} |
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
package place | |
type ReqStore struct { | |
Name string `validate:"required"` | |
Lat string `validate:"required"` | |
Lng string `validate:"required"` | |
} |
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
package validator | |
import "github.com/go-playground/validator/v10" | |
type Validator struct { | |
Validator *validator.Validate | |
} | |
func (v *Validator) Validate(i interface{}) error { | |
return v.Validator.Struct(i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment