-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from adel-hadadi/add-support-for-repository-rules
added: exists rule
- Loading branch information
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters