Merge pull request #171 from bluesuncorp/v8-development

V8 development
pull/187/head
Dean Karn 9 years ago
commit 1a791b52ba
  1. 6
      README.md
  2. 9
      examples/custom/custom.go
  3. 20
      examples/simple/simple.go
  4. 3
      examples_test.go

@ -2,9 +2,9 @@ Package validator
================ ================
[![Join the chat at https://gitter.im/bluesuncorp/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bluesuncorp/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Join the chat at https://gitter.im/bluesuncorp/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bluesuncorp/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://semaphoreci.com/api/v1/projects/ec20115f-ef1b-4c7d-9393-cc76aba74eb4/517072/badge.svg)](https://semaphoreci.com/joeybloggs/validator) [![Build Status](https://semaphoreci.com/api/v1/projects/ec20115f-ef1b-4c7d-9393-cc76aba74eb4/523019/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
[![Coverage Status](https://coveralls.io/repos/bluesuncorp/validator/badge.svg?branch=v7&service=github)](https://coveralls.io/github/bluesuncorp/validator?branch=v7) [![Coverage Status](https://coveralls.io/repos/bluesuncorp/validator/badge.svg?branch=v8-development&service=github)](https://coveralls.io/github/bluesuncorp/validator?branch=v8-development)
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v7?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.v7) [![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/validator.v8?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/validator.v8)
Package validator implements value validations for structs and individual fields based on tags. Package validator implements value validations for structs and individual fields based on tags.

@ -6,7 +6,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"gopkg.in/bluesuncorp/validator.v7" "gopkg.in/bluesuncorp/validator.v8"
) )
// DbBackedUser User struct // DbBackedUser User struct
@ -17,10 +17,7 @@ type DbBackedUser struct {
func main() { func main() {
config := validator.Config{ config := &validator.Config{TagName: "validate"}
TagName: "validate",
ValidationFuncs: validator.BakedInValidators,
}
validate := validator.New(config) validate := validator.New(config)
@ -30,7 +27,7 @@ func main() {
x := DbBackedUser{Name: sql.NullString{String: "", Valid: true}, Age: sql.NullInt64{Int64: 0, Valid: false}} x := DbBackedUser{Name: sql.NullString{String: "", Valid: true}, Age: sql.NullInt64{Int64: 0, Valid: false}}
errs := validate.Struct(x) errs := validate.Struct(x)
if len(errs) > 0 { if errs != nil {
fmt.Printf("Errs:\n%+v\n", errs) fmt.Printf("Errs:\n%+v\n", errs)
} }
} }

@ -7,7 +7,7 @@ import (
sql "database/sql/driver" sql "database/sql/driver"
"gopkg.in/bluesuncorp/validator.v7" "gopkg.in/bluesuncorp/validator.v8"
) )
// User contains user information // User contains user information
@ -32,10 +32,7 @@ var validate *validator.Validate
func main() { func main() {
config := validator.Config{ config := &validator.Config{TagName: "validate"}
TagName: "validate",
ValidationFuncs: validator.BakedInValidators,
}
validate = validator.New(config) validate = validator.New(config)
@ -67,7 +64,7 @@ func validateStruct() {
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.(validator.ValidationErrors)["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
@ -135,17 +132,10 @@ func ValidateValuerType(field reflect.Value) interface{} {
func main2() { func main2() {
customTypes := map[reflect.Type]validator.CustomTypeFunc{} config := &validator.Config{TagName: "validate"}
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) validate2 = validator.New(config)
validate2.RegisterCustomTypeFunc(ValidateValuerType, (*sql.Valuer)(nil), valuer{})
validateCustomFieldType() validateCustomFieldType()
} }

@ -3,8 +3,7 @@ package validator_test
import ( import (
"fmt" "fmt"
// "gopkg.in/bluesuncorp/validator.v7" "gopkg.in/bluesuncorp/validator.v8"
"../validator"
) )
func ExampleValidate_new() { func ExampleValidate_new() {

Loading…
Cancel
Save