parent
933fe0b7a9
commit
41b4a43989
@ -1,89 +1,92 @@ |
|||||||
package validator_test |
package validator_test |
||||||
|
|
||||||
// func ExampleValidate_new() {
|
import ( |
||||||
// validator.New("validate", validator.BakedInValidators)
|
"fmt" |
||||||
// }
|
|
||||||
|
"../validator" |
||||||
// func ExampleValidate_addFunction() {
|
) |
||||||
// // This should be stored somewhere globally
|
|
||||||
// var validate *validator.Validate
|
func ExampleValidate_new() { |
||||||
|
config := validator.Config{ |
||||||
// validate = validator.New("validate", validator.BakedInValidators)
|
TagName: "validate", |
||||||
|
ValidationFuncs: validator.BakedInValidators, |
||||||
// fn := func(top interface{}, current interface{}, field interface{}, param string) bool {
|
} |
||||||
// return field.(string) == "hello"
|
|
||||||
// }
|
validator.New(config) |
||||||
|
} |
||||||
// validate.AddFunction("valueishello", fn)
|
|
||||||
|
func ExampleValidate_field() { |
||||||
// message := "hello"
|
// This should be stored somewhere globally
|
||||||
// err := validate.Field(message, "valueishello")
|
var validate *validator.Validate |
||||||
// fmt.Println(err)
|
|
||||||
// //Output:
|
config := validator.Config{ |
||||||
// //<nil>
|
TagName: "validate", |
||||||
// }
|
ValidationFuncs: validator.BakedInValidators, |
||||||
|
} |
||||||
// func ExampleValidate_field() {
|
|
||||||
// // This should be stored somewhere globally
|
validate = validator.New(config) |
||||||
// var validate *validator.Validate
|
|
||||||
|
i := 0 |
||||||
// validate = validator.New("validate", validator.BakedInValidators)
|
errs := validate.Field(i, "gt=1,lte=10") |
||||||
|
err := errs[""] |
||||||
// i := 0
|
fmt.Println(err.Field) |
||||||
// err := validate.Field(i, "gt=1,lte=10")
|
fmt.Println(err.Tag) |
||||||
// fmt.Println(err.Field)
|
fmt.Println(err.Kind) // NOTE: Kind and Type can be different i.e. time Kind=struct and Type=time.Time
|
||||||
// fmt.Println(err.Tag)
|
fmt.Println(err.Type) |
||||||
// fmt.Println(err.Kind) // NOTE: Kind and Type can be different i.e. time Kind=struct and Type=time.Time
|
fmt.Println(err.Param) |
||||||
// fmt.Println(err.Type)
|
fmt.Println(err.Value) |
||||||
// fmt.Println(err.Param)
|
//Output:
|
||||||
// fmt.Println(err.Value)
|
//
|
||||||
// //Output:
|
//gt
|
||||||
// //
|
//int
|
||||||
// //gt
|
//int
|
||||||
// //int
|
//1
|
||||||
// //int
|
//0
|
||||||
// //1
|
} |
||||||
// //0
|
|
||||||
// }
|
func ExampleValidate_struct() { |
||||||
|
// This should be stored somewhere globally
|
||||||
// func ExampleValidate_struct() {
|
var validate *validator.Validate |
||||||
// // This should be stored somewhere globally
|
|
||||||
// var validate *validator.Validate
|
config := validator.Config{ |
||||||
|
TagName: "validate", |
||||||
// validate = validator.New("validate", validator.BakedInValidators)
|
ValidationFuncs: validator.BakedInValidators, |
||||||
|
} |
||||||
// type ContactInformation struct {
|
|
||||||
// Phone string `validate:"required"`
|
validate = validator.New(config) |
||||||
// Street string `validate:"required"`
|
|
||||||
// City string `validate:"required"`
|
type ContactInformation struct { |
||||||
// }
|
Phone string `validate:"required"` |
||||||
|
Street string `validate:"required"` |
||||||
// type User struct {
|
City string `validate:"required"` |
||||||
// Name string `validate:"required,excludesall=!@#$%^&*()_+-=:;?/0x2C"` // 0x2C = comma (,)
|
} |
||||||
// Age int8 `validate:"required,gt=0,lt=150"`
|
|
||||||
// Email string `validate:"email"`
|
type User struct { |
||||||
// ContactInformation []*ContactInformation
|
Name string `validate:"required,excludesall=!@#$%^&*()_+-=:;?/0x2C"` // 0x2C = comma (,)
|
||||||
// }
|
Age int8 `validate:"required,gt=0,lt=150"` |
||||||
|
Email string `validate:"email"` |
||||||
// contactInfo := &ContactInformation{
|
ContactInformation []*ContactInformation |
||||||
// Street: "26 Here Blvd.",
|
} |
||||||
// City: "Paradeso",
|
|
||||||
// }
|
contactInfo := &ContactInformation{ |
||||||
|
Street: "26 Here Blvd.", |
||||||
// user := &User{
|
City: "Paradeso", |
||||||
// Name: "Joey Bloggs",
|
} |
||||||
// Age: 31,
|
|
||||||
// Email: "joeybloggs@gmail.com",
|
user := &User{ |
||||||
// ContactInformation: []*ContactInformation{contactInfo},
|
Name: "Joey Bloggs", |
||||||
// }
|
Age: 31, |
||||||
|
Email: "joeybloggs@gmail.com", |
||||||
// structError := validate.Struct(user)
|
ContactInformation: []*ContactInformation{contactInfo}, |
||||||
// for _, fieldError := range structError.Errors {
|
} |
||||||
// fmt.Println(fieldError.Field) // Phone
|
|
||||||
// fmt.Println(fieldError.Tag) // required
|
errs := validate.Struct(user) |
||||||
// //... and so forth
|
for _, v := range errs { |
||||||
// //Output:
|
fmt.Println(v.Field) // Phone
|
||||||
// //Phone
|
fmt.Println(v.Tag) // required
|
||||||
// //required
|
//... and so forth
|
||||||
// }
|
//Output:
|
||||||
// }
|
//Phone
|
||||||
|
//required
|
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue