Major rewrite of ValidateStruct & more validator functions #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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: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 specialrequired
tag which basically checks if the struct value is not empty.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