Validaktor is a struct type validator. It uses a custom tag to validate the structs.
You can download the project using go modules
go get -u github.com/ervitis/validaktor
Let's see an example of validation
type User struct {
Name string `validate:"regex,exp=[A-Z]+" json:"name"`
}
func main() {
user := &User{Name: "HELLO"}
validator := validaktor.NewValidaktor()
errors := validator.ValidateData(user)
for _, e := range errors {
fmt.Println(e)
}
}
You can see more examples inside the examples
folder.
You can add more validators sending a pull request or doing it in your local machine. For more information read the next point in this document.
Let's see an example. For example the regex_validator
file
You should implement two structs:
type (
regexValidator struct {
regex string
}
regexError struct {
message string
}
)
The first one should be used for initialization the validator. The second one for custom errors.
Now the error methods.
func newRegexValidatorError(message string) *regexError {
return ®exError{message: message}
}
func (e *regexError) Error() string {
return e.message
}
Let's implement the validate
method.
func (v *regexValidator) applyValidatorOptions(args ...string) error {
// implement logic and validations for the tag
// if there is an error, return it properly
return nil
}
func (v *regexValidator) validate(data interface{}) (bool, error) {
// implement the validation. You can use here the custom errors created before
// ...
return true, nil
}
After the validator implementation, let's add it in the initializer.go
file. Let's see the regex
type:
func validators() map[string]validator {
return map[string]validator {
"regex": ®exValidator{},
...
}
}
After that, implement the unit tests.
go get -u github.com/ervitis/validaktor
go test -v -race ./...
You can use any framework for testing. Let's see an example:
type testRegex struct {
exp string
isValid bool
err error
data interface{}
}
func TestRegexValidate(t *testing.T) {
testData := []testRegex{
{exp: "[A-Z]+", isValid: true, err: nil, data: "HELLO"},
{exp: "[0-9]{4,6}", isValid: true, err: nil, data: "12345"},
{exp: "\\w+", isValid: true, err: nil, data: "whatever24"},
{exp: `\w+`, isValid: true, err: nil, data: "iamgood"},
{exp: "[^A-Z]+", isValid: true, err: nil, data: "123456asdf"},
}
for _, v := range testData {
validator := ®exValidator{regex: v.exp}
isValid, err := validator.validate(v.data)
if v.isValid != isValid {
t.Errorf("%+v != %+v it should be valid with data %+v", v.isValid, isValid, v.data)
}
if err != v.err {
t.Errorf("there was an error %s", err)
}
}
}
We can prepare the test data using an struct and then iterate it inside the for loop.
Install in your system golangci-lint
for lint your code.
Also you need to install make
in your system. Then write in your console:
make check
- Golang - Golang 1.12
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- ervitis - Initial work - ervitis
This project is licensed under the MIT License - see the LICENSE.md file for details