Skip to content

Commit

Permalink
Merge pull request #14 from adel-hadadi/add-support-for-repository-rules
Browse files Browse the repository at this point in the history
added: exists rule
  • Loading branch information
rezakhademix authored Mar 4, 2024
2 parents d945d57 + 5cfc2c9 commit 566f6c6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
15 changes: 15 additions & 0 deletions exists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package validator

const (
// Exists represents the rule name which will be used to find the default error message.
Exists = "exists"
// ExistsMsg is default error message format for records that not exists.
ExistsMsg = "% not exists"
)

// Exists checks if given value is exists in database.
func (v *Validator) Exists(value any, table, column, field, msg string) *Validator {
v.Check(v.repo.Exists(value, table, column), field, msg)

return v
}
90 changes: 90 additions & 0 deletions exists_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package validator

import (
"github.com/stretchr/testify/assert"
"testing"
)

var (
tables = map[string][]map[string]any{
"users": {
{
"id": 1,
"username": "test_1",
},
{
"id": 2,
"username": "test_2",
},
},
}
)

type repo struct{}

func (repo) Exists(value any, table, column string) bool {
data, exists := tables[table]
if !exists {
return false
}

for _, item := range data {
if item[column] == value {
return true
}
}

return false
}

func TestValidator_Exists(t *testing.T) {
tests := []struct {
tag string
value any
table string
column string
isPassed bool
msg string
expectedMsg string
}{
{
tag: "id",
value: 5,
table: "users",
column: "id",
isPassed: false,
msg: "record not exists",
expectedMsg: "record not exists",
},
{
tag: "username",
value: "test_1",
table: "users",
column: "username",
isPassed: true,
msg: "",
expectedMsg: "",
},
{
tag: "username_2",
value: "test_5",
table: "users",
column: "username",
isPassed: false,
msg: "",
expectedMsg: "username_2 not exists",
},
}

v := New().WithRepo(repo{})

for _, test := range tests {
v.Exists(test.value, test.table, test.column, test.tag, test.msg)

assert.Equal(t, test.isPassed, v.IsPassed())

if v.IsFailed() {
assert.Equal(t, test.msg, v.Errors()[test.tag])
}
}
}
16 changes: 15 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ type (
// Err is the defined type which will be returned when one or many validator rules fail.
Err = map[string]string
// Validator represents the validator structure
Validator struct{}
Validator struct {
repo Repository
}
// Repository represent needed repository methods for using in some rules that need database connection.
Repository interface {
Exists(value any, table, column string) bool
}
)

var (
Expand All @@ -21,6 +27,7 @@ var (
// methodToErrorMessage contains each validation method and its corresponding error message.
methodToErrorMessage = map[string]string{
Required: RequiredMsg,
Exists: ExistsMsg,
Len: LenMsg,
Max: MaxMsg,
Min: MinMsg,
Expand All @@ -36,6 +43,13 @@ func New() *Validator {
return &Validator{}
}

// WithRepo get a repository for using validation rules that need to contact with database
func (v *Validator) WithRepo(r Repository) *Validator {
v.repo = r

return v
}

// IsPassed checks validator result is passed or not.
func (v *Validator) IsPassed() bool {
return len(errs) == 0
Expand Down

0 comments on commit 566f6c6

Please sign in to comment.