@@ -13196,14 +13196,14 @@ func TestSpiceDBValueFormatValidation(t *testing.T) {
1319613196 tag string
1319713197 expected bool
1319813198 }{
13199- //Must be an asterisk OR a string containing alphanumeric characters and a restricted set a special symbols: _ | / - = +
13199+ // Must be an asterisk OR a string containing alphanumeric characters and a restricted set a special symbols: _ | / - = +
1320013200 {"*" , "spicedb=id" , true },
1320113201 {`azAZ09_|/-=+` , "spicedb=id" , true },
1320213202 {`a*` , "spicedb=id" , false },
1320313203 {`/` , "spicedb=id" , true },
1320413204 {"*" , "spicedb" , true },
1320513205
13206- //Must begin and end with a lowercase letter, may also contain numbers and underscores between, min length 3, max length 64
13206+ // Must begin and end with a lowercase letter, may also contain numbers and underscores between, min length 3, max length 64
1320713207 {"a" , "spicedb=permission" , false },
1320813208 {"1" , "spicedb=permission" , false },
1320913209 {"a1" , "spicedb=permission" , false },
@@ -13213,7 +13213,7 @@ func TestSpiceDBValueFormatValidation(t *testing.T) {
1321313213 {"abcdefghijklmnopqrstuvwxyz_0123456789_abcdefghijklmnopqrstuvwxyz" , "spicedb=permission" , true },
1321413214 {"abcdefghijklmnopqrstuvwxyz_01234_56789_abcdefghijklmnopqrstuvwxyz" , "spicedb=permission" , false },
1321513215
13216- //Object types follow the same rules as permissions for the type name plus an optional prefix up to 63 characters with a /
13216+ // Object types follow the same rules as permissions for the type name plus an optional prefix up to 63 characters with a /
1321713217 {"a" , "spicedb=type" , false },
1321813218 {"1" , "spicedb=type" , false },
1321913219 {"a1" , "spicedb=type" , false },
@@ -13606,3 +13606,47 @@ func TestTimeRequired(t *testing.T) {
1360613606 NotEqual (t , err , nil )
1360713607 AssertError (t , err .(ValidationErrors ), "TestTime.Time" , "TestTime.Time" , "Time" , "Time" , "required" )
1360813608}
13609+
13610+ func TestOmitNilAndRequired (t * testing.T ) {
13611+ type (
13612+ OmitEmpty struct {
13613+ Str string `validate:"omitempty,required,min=10"`
13614+ StrPtr * string `validate:"omitempty,required,min=10"`
13615+ Inner * OmitEmpty
13616+ }
13617+ OmitNil struct {
13618+ Str string `validate:"omitnil,required,min=10"`
13619+ StrPtr * string `validate:"omitnil,required,min=10"`
13620+ Inner * OmitNil
13621+ }
13622+ )
13623+
13624+ var (
13625+ validate = New (WithRequiredStructEnabled ())
13626+ valid = "this is the long string to pass the validation rule"
13627+ )
13628+
13629+ t .Run ("compare using valid data" , func (t * testing.T ) {
13630+ err1 := validate .Struct (OmitEmpty {Str : valid , StrPtr : & valid , Inner : & OmitEmpty {Str : valid , StrPtr : & valid }})
13631+ err2 := validate .Struct (OmitNil {Str : valid , StrPtr : & valid , Inner : & OmitNil {Str : valid , StrPtr : & valid }})
13632+
13633+ Equal (t , err1 , nil )
13634+ Equal (t , err2 , nil )
13635+ })
13636+
13637+ t .Run ("compare fully empty omitempty and omitnil" , func (t * testing.T ) {
13638+ err1 := validate .Struct (OmitEmpty {})
13639+ err2 := validate .Struct (OmitNil {})
13640+
13641+ Equal (t , err1 , nil )
13642+ AssertError (t , err2 , "OmitNil.Str" , "OmitNil.Str" , "Str" , "Str" , "required" )
13643+ })
13644+
13645+ t .Run ("validate in deep" , func (t * testing.T ) {
13646+ err1 := validate .Struct (OmitEmpty {Str : valid , Inner : & OmitEmpty {}})
13647+ err2 := validate .Struct (OmitNil {Str : valid , Inner : & OmitNil {}})
13648+
13649+ Equal (t , err1 , nil )
13650+ AssertError (t , err2 , "OmitNil.Inner.Str" , "OmitNil.Inner.Str" , "Str" , "Str" , "required" )
13651+ })
13652+ }
0 commit comments