Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -130,7 +129,8 @@ type valuer struct {
func (v valuer) Value() (driver.Value, error) {

if v.Name == "errorme" {
return nil, errors.New("some kind of error")
panic("SQL Driver Valuer error: some kind of error")
// return nil, errors.New("some kind of error")
}

if len(v.Name) == 0 {
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestSQLValue2Validation(t *testing.T) {

val.Name = "errorme"

PanicMatches(t, func() { errs = validate.Field(val, "required") }, "SQL Driver Valuer error: some kind of error")
PanicMatches(t, func() { validate.Field(val, "required") }, "SQL Driver Valuer error: some kind of error")

type myValuer valuer

Expand Down Expand Up @@ -3187,7 +3187,9 @@ func TestHsla(t *testing.T) {
AssertError(t, errs, "", "", "hsla")

i := 1
PanicMatches(t, func() { validate.Field(i, "hsla") }, "interface conversion: interface is int, not string")
validate.Field(i, "hsla")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "hsla")
}

func TestHsl(t *testing.T) {
Expand Down Expand Up @@ -3221,7 +3223,9 @@ func TestHsl(t *testing.T) {
AssertError(t, errs, "", "", "hsl")

i := 1
PanicMatches(t, func() { validate.Field(i, "hsl") }, "interface conversion: interface is int, not string")
errs = validate.Field(i, "hsl")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "hsl")
}

func TestRgba(t *testing.T) {
Expand Down Expand Up @@ -3263,7 +3267,9 @@ func TestRgba(t *testing.T) {
AssertError(t, errs, "", "", "rgba")

i := 1
PanicMatches(t, func() { validate.Field(i, "rgba") }, "interface conversion: interface is int, not string")
errs = validate.Field(i, "rgba")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "rgba")
}

func TestRgb(t *testing.T) {
Expand Down Expand Up @@ -3301,7 +3307,9 @@ func TestRgb(t *testing.T) {
AssertError(t, errs, "", "", "rgb")

i := 1
PanicMatches(t, func() { validate.Field(i, "rgb") }, "interface conversion: interface is int, not string")
errs = validate.Field(i, "rgb")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "rgb")
}

func TestEmail(t *testing.T) {
Expand Down Expand Up @@ -3331,7 +3339,9 @@ func TestEmail(t *testing.T) {
AssertError(t, errs, "", "", "email")

i := true
PanicMatches(t, func() { validate.Field(i, "email") }, "interface conversion: interface is bool, not string")
errs = validate.Field(i, "email")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "email")
}

func TestHexColor(t *testing.T) {
Expand All @@ -3355,7 +3365,9 @@ func TestHexColor(t *testing.T) {
AssertError(t, errs, "", "", "hexcolor")

i := true
PanicMatches(t, func() { validate.Field(i, "hexcolor") }, "interface conversion: interface is bool, not string")
errs = validate.Field(i, "hexcolor")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "hexcolor")
}

func TestHexadecimal(t *testing.T) {
Expand All @@ -3370,7 +3382,9 @@ func TestHexadecimal(t *testing.T) {
AssertError(t, errs, "", "", "hexadecimal")

i := true
PanicMatches(t, func() { validate.Field(i, "hexadecimal") }, "interface conversion: interface is bool, not string")
errs = validate.Field(i, "hexadecimal")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "hexadecimal")
}

func TestNumber(t *testing.T) {
Expand Down Expand Up @@ -3415,7 +3429,9 @@ func TestNumber(t *testing.T) {
AssertError(t, errs, "", "", "number")

i := 1
PanicMatches(t, func() { validate.Field(i, "number") }, "interface conversion: interface is int, not string")
errs = validate.Field(i, "number")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "number")
}

func TestNumeric(t *testing.T) {
Expand Down Expand Up @@ -3455,7 +3471,9 @@ func TestNumeric(t *testing.T) {
AssertError(t, errs, "", "", "numeric")

i := 1
PanicMatches(t, func() { validate.Field(i, "numeric") }, "interface conversion: interface is int, not string")
errs = validate.Field(i, "numeric")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "numeric")
}

func TestAlphaNumeric(t *testing.T) {
Expand All @@ -3469,7 +3487,9 @@ func TestAlphaNumeric(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "alphanum")

PanicMatches(t, func() { validate.Field(1, "alphanum") }, "interface conversion: interface is int, not string")
errs = validate.Field(1, "alphanum")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "alphanum")
}

func TestAlpha(t *testing.T) {
Expand All @@ -3481,10 +3501,11 @@ func TestAlpha(t *testing.T) {
s = "abc1"
errs = validate.Field(s, "alpha")
NotEqual(t, errs, nil)

AssertError(t, errs, "", "", "alpha")

PanicMatches(t, func() { validate.Field(1, "alpha") }, "interface conversion: interface is int, not string")
errs = validate.Field(1, "alpha")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "alpha")
}

func TestStructStringValidation(t *testing.T) {
Expand Down Expand Up @@ -3737,22 +3758,6 @@ func TestInvalidStruct(t *testing.T) {
PanicMatches(t, func() { validate.Struct(s.Test) }, "value passed for validation is not a struct")
}

func TestInvalidField(t *testing.T) {
s := &SubTest{
Test: "1",
}

PanicMatches(t, func() { validate.Field(s, "required") }, "Invalid field passed to traverseField")
}

func TestInvalidTagField(t *testing.T) {
s := &SubTest{
Test: "1",
}

PanicMatches(t, func() { validate.Field(s.Test, "") }, fmt.Sprintf("Invalid validation tag on field %s", ""))
}

func TestInvalidValidatorFunction(t *testing.T) {
s := &SubTest{
Test: "1",
Expand Down