Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major rewrite of ValidateStruct & more validator functions #2

Merged
merged 4 commits into from
Oct 1, 2014

Conversation

nullboundary
Copy link
Contributor

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.

  1. 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

asaskevich added a commit that referenced this pull request Oct 1, 2014
Major rewrite of ValidateStruct & more validator functions
@asaskevich asaskevich merged commit 9e4ddf0 into asaskevich:master Oct 1, 2014
@asaskevich
Copy link
Owner

Great! I worked with ValidateStruct and wanted to make it work like your variant. It's really great. I already merge this request, and will write some new tests for new features. Thank you!

@nullboundary
Copy link
Contributor Author

No problem! Let me know if you have any questions about any of the code changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants