Skip to content

Commit

Permalink
fix: #140 requiredIf check error on cmp-field kind same is string
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 26, 2022
1 parent da38113 commit 150c83e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
16 changes: 16 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,22 @@ func TestIssue_135(t *testing.T) {
assert.Equal(t, "score min value is 0.1", v.Errors.OneError().Error())
}

// https://github.com/gookit/validate/issues/140
func TestIssue_140(t *testing.T) {
type Test struct {
Field1 string
Field2 string `validate:"requiredIf:Field1,value"`
}

test := &Test{Field1: "value", Field2: ""}

v := validate.Struct(test)
err := v.ValidateE()
dump.Println(err)
assert.Error(t, err)
assert.Equal(t, "Field2 is required when Field1 is [value]", err.One())
}

// https://github.com/gookit/validate/issues/143
func TestIssue_143(t *testing.T) {
type Data struct {
Expand Down
6 changes: 4 additions & 2 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,13 @@ func convTypeByBaseKind(srcVal interface{}, srcKind kind, dstType reflect.Kind)
case stringKind:
switch dstType {
case reflect.Int:
return mathutil.Int(srcVal)
return mathutil.ToInt(srcVal)
case reflect.Int64:
return mathutil.Int64(srcVal)
return mathutil.ToInt64(srcVal)
case reflect.Bool:
return strutil.Bool(srcVal.(string))
case reflect.String:
return srcVal.(string), nil
}
case intKind, uintKind:
i64 := mathutil.MustInt64(srcVal)
Expand Down
8 changes: 8 additions & 0 deletions validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func (v *Validation) ValidateData(data DataFace) bool {
return v.Validate()
}

// ValidateE do validate processing and return error
func (v *Validation) ValidateE(scene ...string) Errors {
if v.Validate(scene...) {
return nil
}
return v.Errors
}

// Validate processing
func (v *Validation) Validate(scene ...string) bool {
// has been validated OR has error
Expand Down

0 comments on commit 150c83e

Please sign in to comment.