Validator: Custom Validator With Database

Created on 26 Mar 2019  路  2Comments  路  Source: go-playground/validator

Package version eg. v8, v9:

v8

Issue, Question or Enhancement:

How to create custom validator with database usage? For example, I need to check that value is unique across table users within email column

Code sample, to showcase or reproduce:

Unique rule. Users is table, Email is column

type CreateUserRequest struct {
    Email string `validator:"required,unique=users;email"`
}
question

Most helpful comment

Hey @vyuldashev yes there is the validation function is just a function and so can capture outside variables or you can use a method, here are a couple examples:

capture

package main

import (
    "fmt"

    validator "gopkg.in/go-playground/validator.v9"
)

// MyStruct ..
type MyStruct struct {
    String string `validate:"is-awesome"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

func main() {

    myDBHandle := "awesome"

    validate = validator.New()
    validate.RegisterValidation("is-awesome", func(fl validator.FieldLevel) bool {
        return fl.Field().String() == myDBHandle
    })

    s := MyStruct{String: "awesome"}

    err := validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }

    s.String = "not awesome"
    err = validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }
}

method

package main

import (
    "fmt"

    validator "gopkg.in/go-playground/validator.v9"
)

// MyStruct ..
type MyStruct struct {
    String string `validate:"is-awesome"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

type myDBAbstraction struct {
    db string
}

func (a *myDBAbstraction) ValidateUser(fl validator.FieldLevel) bool {
    return fl.Field().String() == a.db
}

func main() {

    a := &myDBAbstraction{db: "awesome"}

    validate = validator.New()
    validate.RegisterValidation("is-awesome", a.ValidateUser)

    s := MyStruct{String: "awesome"}

    err := validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }

    a.db = "not awesome"
    err = validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }
}

let me know if that helps :)

All 2 comments

Hey @vyuldashev yes there is the validation function is just a function and so can capture outside variables or you can use a method, here are a couple examples:

capture

package main

import (
    "fmt"

    validator "gopkg.in/go-playground/validator.v9"
)

// MyStruct ..
type MyStruct struct {
    String string `validate:"is-awesome"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

func main() {

    myDBHandle := "awesome"

    validate = validator.New()
    validate.RegisterValidation("is-awesome", func(fl validator.FieldLevel) bool {
        return fl.Field().String() == myDBHandle
    })

    s := MyStruct{String: "awesome"}

    err := validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }

    s.String = "not awesome"
    err = validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }
}

method

package main

import (
    "fmt"

    validator "gopkg.in/go-playground/validator.v9"
)

// MyStruct ..
type MyStruct struct {
    String string `validate:"is-awesome"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

type myDBAbstraction struct {
    db string
}

func (a *myDBAbstraction) ValidateUser(fl validator.FieldLevel) bool {
    return fl.Field().String() == a.db
}

func main() {

    a := &myDBAbstraction{db: "awesome"}

    validate = validator.New()
    validate.RegisterValidation("is-awesome", a.ValidateUser)

    s := MyStruct{String: "awesome"}

    err := validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }

    a.db = "not awesome"
    err = validate.Struct(s)
    if err != nil {
        fmt.Printf("Err(s):\n%+v\n", err)
    }
}

let me know if that helps :)

@joeybloggs Yes, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muhammadkholidb picture muhammadkholidb  路  3Comments

romangithub1024 picture romangithub1024  路  5Comments

phenrigomes picture phenrigomes  路  5Comments

ivybridge-3c33 picture ivybridge-3c33  路  3Comments

deschmih picture deschmih  路  3Comments