You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.5 KiB
166 lines
3.5 KiB
10 years ago
|
package main
|
||
|
|
||
|
import (
|
||
10 years ago
|
"errors"
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"reflect"
|
||
|
|
||
|
sql "database/sql/driver"
|
||
10 years ago
|
|
||
10 years ago
|
"gopkg.in/bluesuncorp/validator.v6"
|
||
10 years ago
|
)
|
||
|
|
||
|
// User contains user information
|
||
|
type User struct {
|
||
|
FirstName string `validate:"required"`
|
||
|
LastName string `validate:"required"`
|
||
|
Age uint8 `validate:"gte=0,lte=130"`
|
||
|
Email string `validate:"required,email"`
|
||
|
FavouriteColor string `validate:"hexcolor|rgb|rgba"`
|
||
|
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() {
|
||
|
|
||
10 years ago
|
config := validator.Config{
|
||
|
TagName: "validate",
|
||
|
ValidationFuncs: validator.BakedInValidators,
|
||
|
}
|
||
|
|
||
|
validate = validator.New(config)
|
||
10 years ago
|
|
||
10 years ago
|
validateStruct()
|
||
|
validateField()
|
||
|
}
|
||
|
|
||
|
func validateStruct() {
|
||
|
|
||
10 years ago
|
address := &Address{
|
||
|
Street: "Eavesdown Docks",
|
||
|
Planet: "Persphone",
|
||
|
Phone: "none",
|
||
|
}
|
||
|
|
||
|
user := &User{
|
||
|
FirstName: "Badger",
|
||
|
LastName: "Smith",
|
||
|
Age: 135,
|
||
|
Email: "Badger.Smith@gmail.com",
|
||
|
FavouriteColor: "#000",
|
||
|
Addresses: []*Address{address},
|
||
|
}
|
||
|
|
||
10 years ago
|
// returns nil or ValidationErrors ( map[string]*FieldError )
|
||
10 years ago
|
errs := validate.Struct(user)
|
||
|
|
||
|
if errs != nil {
|
||
|
|
||
10 years ago
|
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"]
|
||
10 years ago
|
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
|
||
|
}
|
||
10 years ago
|
|
||
|
func validateField() {
|
||
|
myEmail := "joeybloggs.gmail.com"
|
||
|
|
||
|
errs := validate.Field(myEmail, "required,email")
|
||
|
|
||
|
if errs != nil {
|
||
|
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// email ok, move on
|
||
|
}
|
||
10 years ago
|
|
||
|
var validate2 *validator.Validate
|
||
|
|
||
|
type valuer struct {
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
func (v valuer) Value() (sql.Value, error) {
|
||
|
|
||
|
if v.Name == "errorme" {
|
||
|
return nil, errors.New("some kind of error")
|
||
|
}
|
||
|
|
||
|
if v.Name == "blankme" {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
if len(v.Name) == 0 {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
return v.Name, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
// 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
|
||
|
}
|
||
|
|
||
10 years ago
|
func main2() {
|
||
|
|
||
|
customTypes := map[reflect.Type]validator.CustomTypeFunc{}
|
||
|
customTypes[reflect.TypeOf((*sql.Valuer)(nil))] = ValidateValuerType
|
||
|
customTypes[reflect.TypeOf(valuer{})] = ValidateValuerType
|
||
|
|
||
|
config := validator.Config{
|
||
|
TagName: "validate",
|
||
|
ValidationFuncs: validator.BakedInValidators,
|
||
|
CustomTypeFuncs: customTypes,
|
||
|
}
|
||
|
|
||
|
validate2 = validator.New(config)
|
||
|
|
||
|
validateCustomFieldType()
|
||
|
}
|
||
|
|
||
|
func validateCustomFieldType() {
|
||
|
val := valuer{
|
||
|
Name: "blankme",
|
||
|
}
|
||
|
|
||
|
errs := validate2.Field(val, "required")
|
||
|
if errs != nil {
|
||
|
fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "required" tag
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// all ok
|
||
|
}
|