bihire boris
bihire boris

Reputation: 1672

unable to validate using validator.v2 golang

I am having trouble validating fields with gin validator. This the error I am getting back everytime I add validate on the struct

what am I doing wrong here

schema

package entity

type User struct {
    Username         string `json:"username" binding:"min=2,required"`
    Email            string `json:"email" binding:"required,email" validate:"email"`
    Password         string `json:"password" binding:"min=8,max=32,alphanum"`
    ConfirmPassword  string `json:"confirm_password" binding:"eqfield=Password,required"`
}

validation middleware I amtrying to add

package validations

import (
    "github.com/bihire/ikaze_server_app/entity"
    "net/http"
    "github.com/gin-gonic/gin"
    "gopkg.in/validator.v2"
)


func SignupValidator(ctx *gin.Context) {
    var user entity.User
    if err := validator.Validate(&user); err != nil {
        ctx.JSON(http.StatusBadRequest, gin.H{
            "error": err.Error(),
        })
    }
}

the error I am getting back evrywhere I am adding the validate on the schema

{
    "error": "Email: unknown tag"
}{
    "username": "this the first video description",
    "email": "h",
    "password": "password",
    "confirm_password": "password"
}

Upvotes: 0

Views: 1411

Answers (1)

Nathan E
Nathan E

Reputation: 31

There is no "email" validator in your current package: https://github.com/go-validator/validator/tree/v2

There is an "email" validator in https://github.com/go-playground/validator

Upvotes: 3

Related Questions