Skip to content
Open
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
82 changes: 80 additions & 2 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10991,6 +10991,32 @@ func TestStartsWithValidation(t *testing.T) {
}
}
}
func TestStartsNotWithValidation(t *testing.T) {
tests := []struct {
Value string `validate:"startsnotwith=(/^ヮ^)/*:・゚✧"`
Tag string
ExpectedNil bool
}{
{Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "startsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: false},
{Value: "abcd", Tag: "startsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: true},
}

validate := New()

for i, s := range tests {
errs := validate.Var(s.Value, s.Tag)

if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}

errs = validate.Struct(s)

if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}
}
}

func TestEndsWithValidation(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -11019,6 +11045,33 @@ func TestEndsWithValidation(t *testing.T) {
}
}

func TestEndsNotWithValidation(t *testing.T) {
tests := []struct {
Value string `validate:"endsnotwith=(/^ヮ^)/*:・゚✧"`
Tag string
ExpectedNil bool
}{
{Value: "glitter (/^ヮ^)/*:・゚✧", Tag: "endsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: false},
{Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "endsnotwith=(/^ヮ^)/*:・゚✧", ExpectedNil: true},
}

validate := New()

for i, s := range tests {
errs := validate.Var(s.Value, s.Tag)

if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}

errs = validate.Struct(s)

if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}
}
}

func TestRequiredIf(t *testing.T) {
type Inner struct {
Field *string
Expand Down Expand Up @@ -12886,7 +12939,7 @@ func TestIsIso4217Validation(t *testing.T) {
}

func TestIsIso4217NumericValidation(t *testing.T) {
tests := []struct {
testsInt := []struct {
value int `validate:"iso4217_numeric"`
expected bool
}{
Expand All @@ -12897,7 +12950,30 @@ func TestIsIso4217NumericValidation(t *testing.T) {

validate := New()

for i, test := range tests {
for i, test := range testsInt {
errs := validate.Var(test.value, "iso4217_numeric")

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d iso4217 failed Error: %s", i, errs)
}
}
}

testsUInt := []struct {
value uint `validate:"iso4217_numeric"`
expected bool
}{
{8, true},
{12, true},
{13, false},
}

for i, test := range testsUInt {
errs := validate.Var(test.value, "iso4217_numeric")

if test.expected {
Expand All @@ -12910,6 +12986,8 @@ func TestIsIso4217NumericValidation(t *testing.T) {
}
}
}

PanicMatches(t, func() { _ = validate.Var(2.0, "iso4217_numeric") }, "Bad field type float64")
}

func TestTimeZoneValidation(t *testing.T) {
Expand Down