asaskevich/govalidator — pull request #2 by slugmobile (2014-10-01 09:32:33+00:00)
Title: Major rewrite of ValidateStruct & more validator functions

Hi, I enjoyed your govalidator library but I it wasn't totally working in the way I needed. So I rewrote parts of it to fit better for me. So please review the changes and decide if they are to you liking. If not I can just keep this as a fork. Here are some of the changes:

1. ValidateStruct now uses the tag syntax: `valid:"email,required"` Basically I totally re-wrote the ValidateStruct function with lots(!) of inspiration from libraries like go json,mgo, and a few others. It should be much more robust in terms of parsing a nested struct, with slices of structs, ptrs to structs, and maps. I changed the tag name to valid, because for now I couldnt figure out how to get regex to work cleanly in the new method and maybe its not needed. The new ValidateStruct makes a map of tags and looks up the matching validation function. So the tag: `valid:"email"` will use email as a key and find the IsEmail(str string) bool function. A user can also easily add new functions to the tag map check out the modified example:

```
func ExampleValidateStruct() {
type Post struct {
Title string `valid:"alphanum,required"`
Message string `valid:"duck,ascii"`
AuthorIP string `valid:"ipv4"`
}
post := &Post{"My1PostaboutExamples", "duck", "123.234.54.3"}

//add your own struct validation tags
TagMap["duck"] = validator(func(str string) bool {
if str == "duck" {
return true
} else {
return false
}
})

result, err := ValidateStruct(post)
if err != nil {
println("error: " + err.Error())
}
println(result)
}
```

For now all functions used by ValidateStruct needs to be of `type validator func(str string) bool` as I haven't created an easy way to pass parameters in the tags. As a result I broke out many of the validation functions that have versions such as IPv4 and IPv6 into separate functions. The ValidateStruct could still use a little organization here and there but it is working. Oh and i added a special `required` tag which basically checks if the struct value is not empty.

2. I also added a few more validation functions that I needed for my own applications. They have been tested, but not extensively. Better validation regexs for these may exist. Here is what I added:

```
IsRGBcolor(str string) bool
IsDataURI(str string) bool
IsLatitude(str string) bool
IsLongitude(str string) bool
```

All tests are passing, and I have tested the ValidateStruct on a much more elaborately nested struct used in my own application and I am happy with the results.

Thanks,
-Noah Shibley