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

added: required method #1

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
38 changes: 38 additions & 0 deletions required.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions required_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
}
38 changes: 35 additions & 3 deletions validator.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Loading