-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invoke all custom validators (fixes #116)
- Loading branch information
1 parent
59d9d8a
commit 13f9f00
Showing
2 changed files
with
32 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1924,11 +1924,14 @@ func TestFieldsRequiredByDefaultButExemptOrOptionalStruct(t *testing.T) { | |
type CustomByteArray [6]byte | ||
|
||
type StructWithCustomByteArray struct { | ||
ID CustomByteArray `valid:"customByteArrayValidator"` | ||
Email string `valid:"email"` | ||
ID CustomByteArray `valid:"customByteArrayValidator,customMinLengthValidator"` | ||
Email string `valid:"email"` | ||
CustomMinLength int `valid:"-"` | ||
} | ||
|
||
func TestStructWithCustomByteArray(t *testing.T) { | ||
t.Parallel() | ||
|
||
// add our custom byte array validator that fails when the byte array is pristine (all zeroes) | ||
CustomTypeTagMap["customByteArrayValidator"] = CustomTypeValidator(func(i interface{}, o interface{}) bool { | ||
switch v := o.(type) { | ||
|
@@ -1952,6 +1955,13 @@ func TestStructWithCustomByteArray(t *testing.T) { | |
} | ||
return false | ||
}) | ||
CustomTypeTagMap["customMinLengthValidator"] = CustomTypeValidator(func(i interface{}, o interface{}) bool { | ||
switch v := o.(type) { | ||
case StructWithCustomByteArray: | ||
return len(v.ID) >= v.CustomMinLength | ||
} | ||
return false | ||
}) | ||
testCustomByteArray := CustomByteArray{'1', '2', '3', '4', '5', '6'} | ||
var tests = []struct { | ||
param StructWithCustomByteArray | ||
|
@@ -1960,7 +1970,9 @@ func TestStructWithCustomByteArray(t *testing.T) { | |
{StructWithCustomByteArray{}, false}, | ||
{StructWithCustomByteArray{Email: "[email protected]"}, false}, | ||
{StructWithCustomByteArray{ID: testCustomByteArray, Email: "[email protected]"}, true}, | ||
{StructWithCustomByteArray{ID: testCustomByteArray, Email: "[email protected]", CustomMinLength: 7}, false}, | ||
} | ||
SetFieldsRequiredByDefault(true) | ||
for _, test := range tests { | ||
actual, err := ValidateStruct(test.param) | ||
if actual != test.expected { | ||
|
@@ -1970,6 +1982,7 @@ func TestStructWithCustomByteArray(t *testing.T) { | |
} | ||
} | ||
} | ||
SetFieldsRequiredByDefault(false) | ||
} | ||
|
||
func TestValidateNegationStruct(t *testing.T) { | ||
|