@@ -13094,6 +13094,80 @@ func TestMongoDBObjectIDFormatValidation(t *testing.T) {
1309413094 }
1309513095}
1309613096
13097+ func TestSpiceDBValueFormatValidation (t * testing.T ) {
13098+ tests := []struct {
13099+ value string
13100+ tag string
13101+ expected bool
13102+ }{
13103+ //Must be an asterisk OR a string containing alphanumeric characters and a restricted set a special symbols: _ | / - = +
13104+ {"*" , "spicedb=id" , true },
13105+ {`azAZ09_|/-=+` , "spicedb=id" , true },
13106+ {`a*` , "spicedb=id" , false },
13107+ {`/` , "spicedb=id" , true },
13108+ {"*" , "spicedb" , true },
13109+
13110+ //Must begin and end with a lowercase letter, may also contain numbers and underscores between, min length 3, max length 64
13111+ {"a" , "spicedb=permission" , false },
13112+ {"1" , "spicedb=permission" , false },
13113+ {"a1" , "spicedb=permission" , false },
13114+ {"a_b" , "spicedb=permission" , true },
13115+ {"A_b" , "spicedb=permission" , false },
13116+ {"a_B" , "spicedb=permission" , false },
13117+ {"abcdefghijklmnopqrstuvwxyz_0123456789_abcdefghijklmnopqrstuvwxyz" , "spicedb=permission" , true },
13118+ {"abcdefghijklmnopqrstuvwxyz_01234_56789_abcdefghijklmnopqrstuvwxyz" , "spicedb=permission" , false },
13119+
13120+ //Object types follow the same rules as permissions for the type name plus an optional prefix up to 63 characters with a /
13121+ {"a" , "spicedb=type" , false },
13122+ {"1" , "spicedb=type" , false },
13123+ {"a1" , "spicedb=type" , false },
13124+ {"a_b" , "spicedb=type" , true },
13125+ {"A_b" , "spicedb=type" , false },
13126+ {"a_B" , "spicedb=type" , false },
13127+ {"abcdefghijklmnopqrstuvwxyz_0123456789_abcdefghijklmnopqrstuvwxyz" , "spicedb=type" , true },
13128+ {"abcdefghijklmnopqrstuvwxyz_01234_56789_abcdefghijklmnopqrstuvwxyz" , "spicedb=type" , false },
13129+
13130+ {`a_b/a` , "spicedb=type" , false },
13131+ {`a_b/1` , "spicedb=type" , false },
13132+ {`a_b/a1` , "spicedb=type" , false },
13133+ {`a_b/a_b` , "spicedb=type" , true },
13134+ {`a_b/A_b` , "spicedb=type" , false },
13135+ {`a_b/a_B` , "spicedb=type" , false },
13136+ {`a_b/abcdefghijklmnopqrstuvwxyz_0123456789_abcdefghijklmnopqrstuvwxyz` , "spicedb=type" , true },
13137+ {`a_b/abcdefghijklmnopqrstuvwxyz_01234_56789_abcdefghijklmnopqrstuvwxyz` , "spicedb=type" , false },
13138+
13139+ {`a/a_b` , "spicedb=type" , false },
13140+ {`1/a_b` , "spicedb=type" , false },
13141+ {`a1/a_b` , "spicedb=type" , false },
13142+ {`a_b/a_b` , "spicedb=type" , true },
13143+ {`A_b/a_b` , "spicedb=type" , false },
13144+ {`a_B/a_b` , "spicedb=type" , false },
13145+ {`abcdefghijklmnopqrstuvwxyz_0123456789_abcdefghijklmnopqrstuvwxy/a_b` , "spicedb=type" , true },
13146+ {`abcdefghijklmnopqrstuvwxyz_0123456789_abcdefghijklmnopqrstuvwxyz/a_b` , "spicedb=type" , false },
13147+ }
13148+
13149+ validate := New ()
13150+
13151+ for i , test := range tests {
13152+ errs := validate .Var (test .value , test .tag )
13153+
13154+ if test .expected {
13155+ if ! IsEqual (errs , nil ) {
13156+ t .Fatalf ("Index: %d spicedb failed Error: %s" , i , errs )
13157+ }
13158+ } else {
13159+ if IsEqual (errs , nil ) {
13160+ t .Fatalf ("Index: %d spicedb - expected error but there was none." , i )
13161+ } else {
13162+ val := getError (errs , "" , "" )
13163+ if val .Tag () != "spicedb" {
13164+ t .Fatalf ("Index: %d spicedb failed Error: %s" , i , errs )
13165+ }
13166+ }
13167+ }
13168+ }
13169+ }
13170+
1309713171func TestCreditCardFormatValidation (t * testing.T ) {
1309813172 tests := []struct {
1309913173 value string `validate:"credit_card"`
0 commit comments