Commit 2389bc72 authored by astaxie's avatar astaxie

golint validation

parent 1d200da4
...@@ -23,7 +23,8 @@ import ( ...@@ -23,7 +23,8 @@ import (
) )
const ( const (
VALIDTAG = "valid" // ValidTag struct tag
ValidTag = "valid"
) )
var ( var (
...@@ -55,16 +56,16 @@ func init() { ...@@ -55,16 +56,16 @@ func init() {
} }
} }
// Valid function type // ValidFunc Valid function type
type ValidFunc struct { type ValidFunc struct {
Name string Name string
Params []interface{} Params []interface{}
} }
// Validate function map // Funcs Validate function map
type Funcs map[string]reflect.Value type Funcs map[string]reflect.Value
// validate values with named type string // Call validate values with named type string
func (f Funcs) Call(name string, params ...interface{}) (result []reflect.Value, err error) { func (f Funcs) Call(name string, params ...interface{}) (result []reflect.Value, err error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
...@@ -96,7 +97,7 @@ func isStructPtr(t reflect.Type) bool { ...@@ -96,7 +97,7 @@ func isStructPtr(t reflect.Type) bool {
} }
func getValidFuncs(f reflect.StructField) (vfs []ValidFunc, err error) { func getValidFuncs(f reflect.StructField) (vfs []ValidFunc, err error) {
tag := f.Tag.Get(VALIDTAG) tag := f.Tag.Get(ValidTag)
if len(tag) == 0 { if len(tag) == 0 {
return return
} }
......
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
) )
type user struct { type user struct {
Id int ID int
Tag string `valid:"Maxx(aa)"` Tag string `valid:"Maxx(aa)"`
Name string `valid:"Required;"` Name string `valid:"Required;"`
Age int `valid:"Required;Range(1, 140)"` Age int `valid:"Required;Range(1, 140)"`
...@@ -33,7 +33,7 @@ func TestGetValidFuncs(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestGetValidFuncs(t *testing.T) {
var vfs []ValidFunc var vfs []ValidFunc
var err error var err error
f, _ := tf.FieldByName("Id") f, _ := tf.FieldByName("ID")
if vfs, err = getValidFuncs(f); err != nil { if vfs, err = getValidFuncs(f); err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
This diff is collapsed.
...@@ -302,7 +302,7 @@ func TestZipCode(t *testing.T) { ...@@ -302,7 +302,7 @@ func TestZipCode(t *testing.T) {
func TestValid(t *testing.T) { func TestValid(t *testing.T) {
type user struct { type user struct {
Id int ID int
Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"` Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
Age int `valid:"Required;Range(1, 140)"` Age int `valid:"Required;Range(1, 140)"`
} }
...@@ -352,13 +352,13 @@ func TestValid(t *testing.T) { ...@@ -352,13 +352,13 @@ func TestValid(t *testing.T) {
func TestRecursiveValid(t *testing.T) { func TestRecursiveValid(t *testing.T) {
type User struct { type User struct {
Id int ID int
Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"` Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
Age int `valid:"Required;Range(1, 140)"` Age int `valid:"Required;Range(1, 140)"`
} }
type AnonymouseUser struct { type AnonymouseUser struct {
Id2 int ID2 int
Name2 string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"` Name2 string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
Age2 int `valid:"Required;Range(1, 140)"` Age2 int `valid:"Required;Range(1, 140)"`
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment