diff --git a/go.mod b/go.mod index ecd63f1..81130e2 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/rezakhademix/govalidator go 1.22.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5bddba9 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/required.go b/required.go new file mode 100644 index 0000000..a463e8c --- /dev/null +++ b/required.go @@ -0,0 +1,38 @@ +package validator + +import ( + "fmt" + "strings" +) + +const ( + // RequiredMethod determine method name for finding default error message + RequiredMethod = "required" + + // RequiredErrorMessage determine method default error message + RequiredErrorMessage = "%s is required" +) + +// RequiredString check if string value is empty return validation error message +func (v *Validator) RequiredString(value, field string, msg ...string) *Validator { + if strings.TrimSpace(value) == "" { + if msg[0] == "" { + msg[0] = fmt.Sprintf(RequiredErrorMessage, field) + } + + v.addErrors(field, msg[0]) + } + + return v +} + +// RequiredInt check if integer value is empty return validation error message +func (v *Validator) RequiredInt(value int, field string, msgArgs ...any) *Validator { + if value == 0 { + msg := FindErrorMessage(RequiredMethod, field, msgArgs) + + v.addErrors(field, msg) + } + + return v +} diff --git a/required_test.go b/required_test.go new file mode 100644 index 0000000..87d86d6 --- /dev/null +++ b/required_test.go @@ -0,0 +1,49 @@ +package validator + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestValidator_RequiredString(t *testing.T) { + tests := []struct { + tag string + value string + message string + isPassed bool + exceptedMsg string + }{ + { + tag: "t0", + value: "test 0", + message: "", + isPassed: true, + exceptedMsg: "", + }, + { + tag: "t1", + value: "", + message: "t1 is required", + isPassed: false, + exceptedMsg: "t1 is required", + }, + { + tag: "t2", + value: " ", + message: "t2 is required", + isPassed: false, + exceptedMsg: "t2 is required", + }, + } + + v := New() + for _, test := range tests { + v.RequiredString(test.value, test.tag, test.message) + + assert.Equal(t, test.isPassed, v.IsPassed()) + + if !test.isPassed { + assert.Equal(t, test.exceptedMsg, v.Errors()[test.tag]) + } + } +} diff --git a/validator.go b/validator.go index bed1e72..22bcb16 100644 --- a/validator.go +++ b/validator.go @@ -1,7 +1,11 @@ // Package validator provides configurable rules for validating data of various types. package validator -import "maps" +import ( + "errors" + "fmt" + "maps" +) type ( // Err the defined type which will be returned when one or many validator rules failed. @@ -10,8 +14,17 @@ type ( Validator struct{} ) -// initiates map errors which has map[string]string type -var errs = make(Err) +var ( + // errs initiates map errors which has map[string]string type + errs = make(Err) + + methodToErrorMessage = map[string]string{ + RequiredMethod: RequiredErrorMessage, + } + + // ErrMethodMessageNotFound return error when method name doesn`t have default error message + ErrMethodMessageNotFound = errors.New("rule error message not exists") +) // New will return a new validator struct func New() *Validator { @@ -51,3 +64,22 @@ func (v *Validator) addErrors(field, msg string) { errs[field] = msg } } + +// FindErrorMessage return error message and check if custom error message is set return formatted custom message +// otherwise return rule default message +func FindErrorMessage(method, field string, msgArgs ...any) string { + if len(msgArgs) == 1 { + return msgArgs[0].(string) + } + + if len(msgArgs) > 1 { + return fmt.Sprintf(msgArgs[0].(string), msgArgs[1:]) + } + + format, ok := methodToErrorMessage[method] + if !ok { + panic(ErrMethodMessageNotFound) + } + + return fmt.Sprintf(format, field) +}