@@ -4,12 +4,16 @@ import (
44 "reflect"
55 "testing"
66
7+ "github.com/go-playground/validator/v10"
78 "github.com/stretchr/testify/require"
89)
910
1011func TestFromValidator (t * testing.T ) {
1112 t .Parallel ()
1213
14+ validator := validator .New (validator .WithRequiredStructEnabled ())
15+ validator .RegisterTagNameFunc (preferPublicName )
16+
1317 // Fields have JSON tags so we can verify those are used over the
1418 // property name.
1519 type TestStruct struct {
@@ -43,71 +47,71 @@ func TestFromValidator(t *testing.T) {
4347
4448 testStruct := validTestStruct ()
4549 testStruct .MaxInt = 1
46- require .Equal (t , "Field `max_int` must be less than or equal to 0." , PublicFacingMessage (validate .Struct (testStruct )))
50+ require .Equal (t , "Field `max_int` must be less than or equal to 0." , PublicFacingMessage (validator , validator .Struct (testStruct )))
4751 })
4852
4953 t .Run ("MaxSlice" , func (t * testing.T ) {
5054 t .Parallel ()
5155
5256 testStruct := validTestStruct ()
5357 testStruct .MaxSlice = []string {"1" }
54- require .Equal (t , "Field `max_slice` must contain at most 0 element(s)." , PublicFacingMessage (validate .Struct (testStruct )))
58+ require .Equal (t , "Field `max_slice` must contain at most 0 element(s)." , PublicFacingMessage (validator , validator .Struct (testStruct )))
5559 })
5660
5761 t .Run ("MaxString" , func (t * testing.T ) {
5862 t .Parallel ()
5963
6064 testStruct := validTestStruct ()
6165 testStruct .MaxString = "value"
62- require .Equal (t , "Field `max_string` must be at most 0 character(s) long." , PublicFacingMessage (validate .Struct (testStruct )))
66+ require .Equal (t , "Field `max_string` must be at most 0 character(s) long." , PublicFacingMessage (validator , validator .Struct (testStruct )))
6367 })
6468
6569 t .Run ("MinInt" , func (t * testing.T ) {
6670 t .Parallel ()
6771
6872 testStruct := validTestStruct ()
6973 testStruct .MinInt = 0
70- require .Equal (t , "Field `min_int` must be greater or equal to 1." , PublicFacingMessage (validate .Struct (testStruct )))
74+ require .Equal (t , "Field `min_int` must be greater or equal to 1." , PublicFacingMessage (validator , validator .Struct (testStruct )))
7175 })
7276
7377 t .Run ("MinSlice" , func (t * testing.T ) {
7478 t .Parallel ()
7579
7680 testStruct := validTestStruct ()
7781 testStruct .MinSlice = nil
78- require .Equal (t , "Field `min_slice` must contain at least 1 element(s)." , PublicFacingMessage (validate .Struct (testStruct )))
82+ require .Equal (t , "Field `min_slice` must contain at least 1 element(s)." , PublicFacingMessage (validator , validator .Struct (testStruct )))
7983 })
8084
8185 t .Run ("MinString" , func (t * testing.T ) {
8286 t .Parallel ()
8387
8488 testStruct := validTestStruct ()
8589 testStruct .MinString = ""
86- require .Equal (t , "Field `min_string` must be at least 1 character(s) long." , PublicFacingMessage (validate .Struct (testStruct )))
90+ require .Equal (t , "Field `min_string` must be at least 1 character(s) long." , PublicFacingMessage (validator , validator .Struct (testStruct )))
8791 })
8892
8993 t .Run ("OneOf" , func (t * testing.T ) {
9094 t .Parallel ()
9195
9296 testStruct := validTestStruct ()
9397 testStruct .OneOf = "red"
94- require .Equal (t , "Field `one_of` should be one of the following values: blue green." , PublicFacingMessage (validate .Struct (testStruct )))
98+ require .Equal (t , "Field `one_of` should be one of the following values: blue green." , PublicFacingMessage (validator , validator .Struct (testStruct )))
9599 })
96100
97101 t .Run ("Required" , func (t * testing.T ) {
98102 t .Parallel ()
99103
100104 testStruct := validTestStruct ()
101105 testStruct .Required = ""
102- require .Equal (t , "Field `required` is required." , PublicFacingMessage (validate .Struct (testStruct )))
106+ require .Equal (t , "Field `required` is required." , PublicFacingMessage (validator , validator .Struct (testStruct )))
103107 })
104108
105109 t .Run ("Unsupported" , func (t * testing.T ) {
106110 t .Parallel ()
107111
108112 testStruct := validTestStruct ()
109113 testStruct .Unsupported = "abc"
110- require .Equal (t , "Validation on field `unsupported` failed on the `e164` tag." , PublicFacingMessage (validate .Struct (testStruct )))
114+ require .Equal (t , "Validation on field `unsupported` failed on the `e164` tag." , PublicFacingMessage (validator , validator .Struct (testStruct )))
111115 })
112116
113117 t .Run ("MultipleErrors" , func (t * testing.T ) {
@@ -116,7 +120,7 @@ func TestFromValidator(t *testing.T) {
116120 testStruct := validTestStruct ()
117121 testStruct .MinInt = 0
118122 testStruct .Required = ""
119- require .Equal (t , "Field `min_int` must be greater or equal to 1. Field `required` is required." , PublicFacingMessage (validate .Struct (testStruct )))
123+ require .Equal (t , "Field `min_int` must be greater or equal to 1. Field `required` is required." , PublicFacingMessage (validator , validator .Struct (testStruct )))
120124 })
121125}
122126
0 commit comments