parent
d802346da4
commit
790122c21c
@ -1,150 +1,150 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
// import (
|
||||||
"errors" |
// "errors"
|
||||||
"fmt" |
// "fmt"
|
||||||
"reflect" |
// "reflect"
|
||||||
|
|
||||||
sql "database/sql/driver" |
// sql "database/sql/driver"
|
||||||
|
|
||||||
"gopkg.in/bluesuncorp/validator.v6" |
// "gopkg.in/bluesuncorp/validator.v6"
|
||||||
) |
// )
|
||||||
|
|
||||||
// User contains user information
|
// // User contains user information
|
||||||
type User struct { |
// type User struct {
|
||||||
FirstName string `validate:"required"` |
// FirstName string `validate:"required"`
|
||||||
LastName string `validate:"required"` |
// LastName string `validate:"required"`
|
||||||
Age uint8 `validate:"gte=0,lte=130"` |
// Age uint8 `validate:"gte=0,lte=130"`
|
||||||
Email string `validate:"required,email"` |
// Email string `validate:"required,email"`
|
||||||
FavouriteColor string `validate:"hexcolor|rgb|rgba"` |
// FavouriteColor string `validate:"hexcolor|rgb|rgba"`
|
||||||
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
|
// Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
|
||||||
} |
// }
|
||||||
|
|
||||||
// Address houses a users address information
|
// // Address houses a users address information
|
||||||
type Address struct { |
// type Address struct {
|
||||||
Street string `validate:"required"` |
// Street string `validate:"required"`
|
||||||
City string `validate:"required"` |
// City string `validate:"required"`
|
||||||
Planet string `validate:"required"` |
// Planet string `validate:"required"`
|
||||||
Phone string `validate:"required"` |
// Phone string `validate:"required"`
|
||||||
} |
// }
|
||||||
|
|
||||||
var validate *validator.Validate |
// var validate *validator.Validate
|
||||||
|
|
||||||
func main() { |
func main() { |
||||||
|
|
||||||
config := validator.Config{ |
// config := validator.Config{
|
||||||
TagName: "validate", |
// TagName: "validate",
|
||||||
ValidationFuncs: validator.BakedInValidators, |
// ValidationFuncs: validator.BakedInValidators,
|
||||||
} |
// }
|
||||||
|
|
||||||
validate = validator.New(config) |
// validate = validator.New(config)
|
||||||
|
|
||||||
validateStruct() |
// validateStruct()
|
||||||
validateField() |
// validateField()
|
||||||
} |
} |
||||||
|
|
||||||
func validateStruct() { |
// func validateStruct() {
|
||||||
|
|
||||||
address := &Address{ |
// address := &Address{
|
||||||
Street: "Eavesdown Docks", |
// Street: "Eavesdown Docks",
|
||||||
Planet: "Persphone", |
// Planet: "Persphone",
|
||||||
Phone: "none", |
// Phone: "none",
|
||||||
} |
// }
|
||||||
|
|
||||||
user := &User{ |
// user := &User{
|
||||||
FirstName: "Badger", |
// FirstName: "Badger",
|
||||||
LastName: "Smith", |
// LastName: "Smith",
|
||||||
Age: 135, |
// Age: 135,
|
||||||
Email: "Badger.Smith@gmail.com", |
// Email: "Badger.Smith@gmail.com",
|
||||||
FavouriteColor: "#000", |
// FavouriteColor: "#000",
|
||||||
Addresses: []*Address{address}, |
// Addresses: []*Address{address},
|
||||||
} |
// }
|
||||||
|
|
||||||
// returns nil or ValidationErrors ( map[string]*FieldError )
|
// // returns nil or ValidationErrors ( map[string]*FieldError )
|
||||||
errs := validate.Struct(user) |
// errs := validate.Struct(user)
|
||||||
|
|
||||||
if errs != nil { |
// if errs != nil {
|
||||||
|
|
||||||
fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag
|
// fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag
|
||||||
// Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
|
// // Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
|
||||||
err := errs["User.Addresses[0].City"] |
// err := errs["User.Addresses[0].City"]
|
||||||
fmt.Println(err.Field) // output: City
|
// fmt.Println(err.Field) // output: City
|
||||||
fmt.Println(err.Tag) // output: required
|
// fmt.Println(err.Tag) // output: required
|
||||||
fmt.Println(err.Kind) // output: string
|
// fmt.Println(err.Kind) // output: string
|
||||||
fmt.Println(err.Type) // output: string
|
// fmt.Println(err.Type) // output: string
|
||||||
fmt.Println(err.Param) // output:
|
// fmt.Println(err.Param) // output:
|
||||||
fmt.Println(err.Value) // output:
|
// fmt.Println(err.Value) // output:
|
||||||
|
|
||||||
// from here you can create your own error messages in whatever language you wish
|
// // from here you can create your own error messages in whatever language you wish
|
||||||
return |
// return
|
||||||
} |
// }
|
||||||
|
|
||||||
// save user to database
|
// // save user to database
|
||||||
} |
// }
|
||||||
|
|
||||||
func validateField() { |
// func validateField() {
|
||||||
myEmail := "joeybloggs.gmail.com" |
// myEmail := "joeybloggs.gmail.com"
|
||||||
|
|
||||||
errs := validate.Field(myEmail, "required,email") |
// errs := validate.Field(myEmail, "required,email")
|
||||||
|
|
||||||
if errs != nil { |
// if errs != nil {
|
||||||
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
|
// fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
|
||||||
return |
// return
|
||||||
} |
// }
|
||||||
|
|
||||||
// email ok, move on
|
// // email ok, move on
|
||||||
} |
// }
|
||||||
|
|
||||||
var validate2 *validator.Validate |
// var validate2 *validator.Validate
|
||||||
|
|
||||||
type valuer struct { |
// type valuer struct {
|
||||||
Name string |
// Name string
|
||||||
} |
// }
|
||||||
|
|
||||||
func (v valuer) Value() (sql.Value, error) { |
// func (v valuer) Value() (sql.Value, error) {
|
||||||
|
|
||||||
if v.Name == "errorme" { |
// if v.Name == "errorme" {
|
||||||
return nil, errors.New("some kind of error") |
// return nil, errors.New("some kind of error")
|
||||||
} |
// }
|
||||||
|
|
||||||
if v.Name == "blankme" { |
// if v.Name == "blankme" {
|
||||||
return "", nil |
// return "", nil
|
||||||
} |
// }
|
||||||
|
|
||||||
if len(v.Name) == 0 { |
// if len(v.Name) == 0 {
|
||||||
return nil, nil |
// return nil, nil
|
||||||
} |
// }
|
||||||
|
|
||||||
return v.Name, nil |
// return v.Name, nil
|
||||||
} |
// }
|
||||||
|
|
||||||
func main2() { |
// func main2() {
|
||||||
|
|
||||||
customTypes := map[reflect.Type]validator.CustomTypeFunc{} |
// customTypes := map[reflect.Type]validator.CustomTypeFunc{}
|
||||||
customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType |
// customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
|
||||||
customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType |
// customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
|
||||||
|
|
||||||
config := validator.Config{ |
// config := validator.Config{
|
||||||
TagName: "validate", |
// TagName: "validate",
|
||||||
ValidationFuncs: validator.BakedInValidators, |
// ValidationFuncs: validator.BakedInValidators,
|
||||||
CustomTypeFuncs: customTypes, |
// CustomTypeFuncs: customTypes,
|
||||||
} |
// }
|
||||||
|
|
||||||
validate2 = validator.New(config) |
// validate2 = validator.New(config)
|
||||||
|
|
||||||
validateCustomFieldType() |
// validateCustomFieldType()
|
||||||
} |
// }
|
||||||
|
|
||||||
func validateCustomFieldType() { |
// func validateCustomFieldType() {
|
||||||
val := valuer{ |
// val := valuer{
|
||||||
Name: "blankme", |
// Name: "blankme",
|
||||||
} |
// }
|
||||||
|
|
||||||
errs := validate2.Field(val, "required") |
// errs := validate2.Field(val, "required")
|
||||||
if errs != nil { |
// if errs != nil {
|
||||||
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "required" tag
|
// fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "required" tag
|
||||||
return |
// return
|
||||||
} |
// }
|
||||||
|
|
||||||
// all ok
|
// // all ok
|
||||||
} |
// }
|
||||||
|
Loading…
Reference in new issue