Skip to content

Commit

Permalink
🐛 fix: resolve the slice sub item convert type fail. issues #206
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 31, 2023
1 parent a142ed7 commit 18b0e03
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,39 @@ func TestIssues_172(t *testing.T) {
assert.Equal(t, []string{"test.com", "oof.com", "foobar.com"}, f.Domains)
}

// https://github.com/gookit/validate/issues/206
func TestIssues_206(t *testing.T) {
m := map[string]any{
"name": "inhere",
"age": 99,
"oldSt": 10,
"newSt": 10,
"email": "some@email.com",
"tags": []string{"go", "php", "java"},
"origins": []map[string]string{
{"name": "test", "url": "https://test.com"},
{"name": "test", "url": "https://test.com"},
},
}

v := validate.Map(m)
v.StopOnError = false
v.SkipOnEmpty = false
// can also
v.StringRule("age", "required|int|min:1|max:99")
v.StringRule("name", "required|minLen:2")
v.StringRule("tags", "required|slice|minlen:1")
// feat: support check sub-item in slice
v.StringRule("tags.*", "required|string|min_len:1")
v.StringRule("origins", "required|slice|minlen:1")
v.StringRule("origins.*.name", "required|full_url")

assert.False(t, v.Validate())
// fmt.Println(v.Errors)
assert.Len(t, v.Errors, 1)
assert.StrContains(t, v.Errors.String(), "full_url: origins.*.name must be a valid full URL address")
}

// https://github.com/gookit/validate/issues/213
func TestIssues_213(t *testing.T) {
type Person struct {
Expand Down
2 changes: 1 addition & 1 deletion validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (r *Rule) valueValidate(field, name string, val any, v *Validation) (ok boo
var subVal any
// check each element in the slice.
for i := 0; i < sliceLen; i++ {
subRv := rftVal.Index(i)
subRv := indirectInterface(rftVal.Index(i))
subKind := subRv.Kind()

// 1.1 convert field value type, is func first argument.
Expand Down
2 changes: 2 additions & 0 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func NewValidation(data DataFace, scene ...string) *Validation {
* validation settings
*************************************************************/

// TODO Config(opt *Options) *Validation

// ResetResult reset the validate result.
func (v *Validation) ResetResult() {
v.Errors = Errors{}
Expand Down

0 comments on commit 18b0e03

Please sign in to comment.