From ea0db1fa4709a606ab96baed64efffb4b5ca6fce Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Sun, 26 Jul 2015 21:48:27 -0400 Subject: [PATCH] Add single Field validation example --- README.md | 21 ++++++++++++++++++++- examples/simple.go | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 312a30e..e25bffc 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Usage and documentation Please see http://godoc.org/gopkg.in/bluesuncorp/validator.v6 for detailed usage docs. -##### Example: +##### Examples: ```go package main @@ -73,6 +73,12 @@ func main() { validate = validator.New(config) + validateStruct() + validateField() +} + +func validateStruct() { + address := &Address{ Street: "Eavesdown Docks", Planet: "Persphone", @@ -109,6 +115,19 @@ func main() { // save user to database } + +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 +} ``` Benchmarks diff --git a/examples/simple.go b/examples/simple.go index 98dabed..46e9295 100644 --- a/examples/simple.go +++ b/examples/simple.go @@ -35,6 +35,12 @@ func main() { validate = validator.New(config) + validateStruct() + validateField() +} + +func validateStruct() { + address := &Address{ Street: "Eavesdown Docks", Planet: "Persphone", @@ -71,3 +77,16 @@ func main() { // save user to database } + +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 +}