Merge branch 'v6-development' into v6

pull/139/head
joeybloggs 9 years ago
commit 048d7b8100
  1. 15
      README.md
  2. 253
      examples/simple.go

@ -170,6 +170,21 @@ func (v valuer) Value() (sql.Value, error) {
return v.Name, nil return v.Name, nil
} }
// ValidateValuerType implements validator.CustomTypeFunc
func ValidateValuerType(field reflect.Value) interface{} {
if valuer, ok := field.Interface().(sql.Valuer); ok {
val, err := valuer.Value()
if err != nil {
// handle the error how you want
return nil
}
return val
}
return nil
}
func main() { func main() {
customTypes := map[reflect.Type]validator.CustomTypeFunc{} customTypes := map[reflect.Type]validator.CustomTypeFunc{}

@ -1,150 +1,165 @@
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
// type Address struct {
// Street string `validate:"required"`
// City string `validate:"required"`
// Planet string `validate:"required"`
// Phone string `validate:"required"`
// }
// var validate *validator.Validate
func main() {
// config := validator.Config{
// TagName: "validate",
// ValidationFuncs: validator.BakedInValidators,
// }
// validate = validator.New(config)
// validateStruct() // Address houses a users address information
// validateField() type Address struct {
Street string `validate:"required"`
City string `validate:"required"`
Planet string `validate:"required"`
Phone string `validate:"required"`
} }
// func validateStruct() { var validate *validator.Validate
// address := &Address{ func main() {
// Street: "Eavesdown Docks",
// Planet: "Persphone",
// Phone: "none",
// }
// user := &User{ config := validator.Config{
// FirstName: "Badger", TagName: "validate",
// LastName: "Smith", ValidationFuncs: validator.BakedInValidators,
// Age: 135, }
// Email: "Badger.Smith@gmail.com",
// FavouriteColor: "#000",
// Addresses: []*Address{address},
// }
// // returns nil or ValidationErrors ( map[string]*FieldError ) validate = validator.New(config)
// errs := validate.Struct(user)
// if errs != nil { validateStruct()
validateField()
}
// fmt.Println(errs) // output: Key: "User.Age" Error:Field validation for "Age" failed on the "lte" tag func validateStruct() {
// // Key: "User.Addresses[0].City" Error:Field validation for "City" failed on the "required" tag
// err := errs["User.Addresses[0].City"] address := &Address{
// fmt.Println(err.Field) // output: City Street: "Eavesdown Docks",
// fmt.Println(err.Tag) // output: required Planet: "Persphone",
// fmt.Println(err.Kind) // output: string Phone: "none",
// fmt.Println(err.Type) // output: string }
// fmt.Println(err.Param) // output:
// fmt.Println(err.Value) // output: user := &User{
FirstName: "Badger",
LastName: "Smith",
Age: 135,
Email: "Badger.Smith@gmail.com",
FavouriteColor: "#000",
Addresses: []*Address{address},
}
// returns nil or ValidationErrors ( map[string]*FieldError )
errs := validate.Struct(user)
if errs != nil {
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
err := errs["User.Addresses[0].City"]
fmt.Println(err.Field) // output: City
fmt.Println(err.Tag) // output: required
fmt.Println(err.Kind) // output: string
fmt.Println(err.Type) // output: string
fmt.Println(err.Param) // output:
fmt.Println(err.Value) // output:
// from here you can create your own error messages in whatever language you wish
return
}
// save user to database
}
// // from here you can create your own error messages in whatever language you wish func validateField() {
// return myEmail := "joeybloggs.gmail.com"
// }
// // save user to database errs := validate.Field(myEmail, "required,email")
// }
// func validateField() { if errs != nil {
// myEmail := "joeybloggs.gmail.com" fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
return
}
// errs := validate.Field(myEmail, "required,email") // email ok, move on
}
// if errs != nil { var validate2 *validator.Validate
// fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
// return
// }
// // email ok, move on type valuer struct {
// } Name string
}
// var validate2 *validator.Validate func (v valuer) Value() (sql.Value, error) {
// type valuer struct { if v.Name == "errorme" {
// Name string return nil, errors.New("some kind of error")
// } }
// func (v valuer) Value() (sql.Value, error) { if v.Name == "blankme" {
return "", nil
}
// if v.Name == "errorme" { if len(v.Name) == 0 {
// return nil, errors.New("some kind of error") return nil, nil
// } }
// if v.Name == "blankme" { return v.Name, nil
// return "", nil }
// }
// if len(v.Name) == 0 { // ValidateValuerType implements validator.CustomTypeFunc
// return nil, nil func ValidateValuerType(field reflect.Value) interface{} {
// } if valuer, ok := field.Interface().(sql.Valuer); ok {
val, err := valuer.Value()
if err != nil {
// handle the error how you want
return nil
}
// return v.Name, nil return val
// } }
// func main2() { return nil
}
// customTypes := map[reflect.Type]validator.CustomTypeFunc{} func main2() {
// customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
// customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
// config := validator.Config{ customTypes := map[reflect.Type]validator.CustomTypeFunc{}
// TagName: "validate", customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
// ValidationFuncs: validator.BakedInValidators, customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
// CustomTypeFuncs: customTypes,
// }
// validate2 = validator.New(config) config := validator.Config{
TagName: "validate",
ValidationFuncs: validator.BakedInValidators,
CustomTypeFuncs: customTypes,
}
// validateCustomFieldType() validate2 = validator.New(config)
// }
// func validateCustomFieldType() { validateCustomFieldType()
// val := valuer{ }
// Name: "blankme",
// } func validateCustomFieldType() {
val := valuer{
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…
Cancel
Save