|
|
@ -3,6 +3,7 @@ package validator |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"database/sql" |
|
|
|
"database/sql" |
|
|
|
"database/sql/driver" |
|
|
|
"database/sql/driver" |
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
@ -191,6 +192,36 @@ func ValidateValuerType(field reflect.Value) interface{} { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestExistsValidation(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonText := "{ \"truthiness2\": true }" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Thing struct { |
|
|
|
|
|
|
|
Truthiness *bool `json:"truthiness" validate:"exists,required"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ting Thing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(jsonText), &ting) |
|
|
|
|
|
|
|
Equal(t, err, nil) |
|
|
|
|
|
|
|
NotEqual(t, ting, nil) |
|
|
|
|
|
|
|
Equal(t, ting.Truthiness, nil) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
errs := validate.Struct(ting) |
|
|
|
|
|
|
|
NotEqual(t, errs, nil) |
|
|
|
|
|
|
|
AssertError(t, errs, "Thing.Truthiness", "Truthiness", "exists") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonText = "{ \"truthiness\": true }" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(jsonText), &ting) |
|
|
|
|
|
|
|
Equal(t, err, nil) |
|
|
|
|
|
|
|
NotEqual(t, ting, nil) |
|
|
|
|
|
|
|
Equal(t, ting.Truthiness, true) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
errs = validate.Struct(ting) |
|
|
|
|
|
|
|
Equal(t, errs, nil) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestSQLValue2Validation(t *testing.T) { |
|
|
|
func TestSQLValue2Validation(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
config := Config{ |
|
|
|
config := Config{ |
|
|
|