diff --git a/internal/gen/tests/example/v1/validations.pb.go b/internal/gen/tests/example/v1/validations.pb.go index d747d34..4917f40 100644 --- a/internal/gen/tests/example/v1/validations.pb.go +++ b/internal/gen/tests/example/v1/validations.pb.go @@ -651,87 +651,6 @@ func (x *FieldOfTypeAny) GetAny() *anypb.Any { return nil } -type RequiredOneof struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - // Types that are assignable to RequiredOneof: - // - // *RequiredOneof_Fld_1 - // *RequiredOneof_Fld_2 - RequiredOneof isRequiredOneof_RequiredOneof `protobuf_oneof:"required_oneof"` -} - -func (x *RequiredOneof) Reset() { - *x = RequiredOneof{} - if protoimpl.UnsafeEnabled { - mi := &file_tests_example_v1_validations_proto_msgTypes[11] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *RequiredOneof) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*RequiredOneof) ProtoMessage() {} - -func (x *RequiredOneof) ProtoReflect() protoreflect.Message { - mi := &file_tests_example_v1_validations_proto_msgTypes[11] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use RequiredOneof.ProtoReflect.Descriptor instead. -func (*RequiredOneof) Descriptor() ([]byte, []int) { - return file_tests_example_v1_validations_proto_rawDescGZIP(), []int{11} -} - -func (m *RequiredOneof) GetRequiredOneof() isRequiredOneof_RequiredOneof { - if m != nil { - return m.RequiredOneof - } - return nil -} - -func (x *RequiredOneof) GetFld_1() string { - if x, ok := x.GetRequiredOneof().(*RequiredOneof_Fld_1); ok { - return x.Fld_1 - } - return "" -} - -func (x *RequiredOneof) GetFld_2() *fieldmaskpb.FieldMask { - if x, ok := x.GetRequiredOneof().(*RequiredOneof_Fld_2); ok { - return x.Fld_2 - } - return nil -} - -type isRequiredOneof_RequiredOneof interface { - isRequiredOneof_RequiredOneof() -} - -type RequiredOneof_Fld_1 struct { - Fld_1 string `protobuf:"bytes,1,opt,name=fld_1,json=fld1,proto3,oneof"` -} - -type RequiredOneof_Fld_2 struct { - Fld_2 *fieldmaskpb.FieldMask `protobuf:"bytes,2,opt,name=fld_2,json=fld2,proto3,oneof"` -} - -func (*RequiredOneof_Fld_1) isRequiredOneof_RequiredOneof() {} - -func (*RequiredOneof_Fld_2) isRequiredOneof_RequiredOneof() {} - var File_tests_example_v1_validations_proto protoreflect.FileDescriptor var file_tests_example_v1_validations_proto_rawDesc = []byte{ @@ -875,28 +794,21 @@ var file_tests_example_v1_validations_proto_rawDesc = []byte{ 0x79, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x74, 0x68, 0x69, 0x73, 0x20, 0x73, 0x68, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6e, 0x65, 0x76, 0x65, 0x72, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x1a, 0x0c, 0x74, 0x68, 0x69, 0x73, 0x20, 0x3d, 0x3d, 0x20, 0x74, 0x68, 0x69, 0x73, 0x52, 0x03, 0x61, 0x6e, - 0x79, 0x22, 0x72, 0x0a, 0x0d, 0x52, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x4f, 0x6e, 0x65, - 0x6f, 0x66, 0x12, 0x15, 0x0a, 0x05, 0x66, 0x6c, 0x64, 0x5f, 0x31, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x48, 0x00, 0x52, 0x04, 0x66, 0x6c, 0x64, 0x31, 0x12, 0x31, 0x0a, 0x05, 0x66, 0x6c, 0x64, - 0x5f, 0x32, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, - 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, - 0x4d, 0x61, 0x73, 0x6b, 0x48, 0x00, 0x52, 0x04, 0x66, 0x6c, 0x64, 0x32, 0x42, 0x17, 0x0a, 0x0e, - 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x5f, 0x6f, 0x6e, 0x65, 0x6f, 0x66, 0x12, 0x05, - 0xba, 0x48, 0x02, 0x08, 0x01, 0x42, 0xd8, 0x01, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x74, 0x65, - 0x73, 0x74, 0x73, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x10, - 0x56, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, - 0x50, 0x01, 0x5a, 0x4c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, - 0x75, 0x66, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x76, 0x61, 0x6c, - 0x69, 0x64, 0x61, 0x74, 0x65, 0x2d, 0x67, 0x6f, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, - 0x6c, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x65, 0x78, 0x61, 0x6d, - 0x70, 0x6c, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x76, 0x31, - 0xa2, 0x02, 0x03, 0x54, 0x45, 0x58, 0xaa, 0x02, 0x10, 0x54, 0x65, 0x73, 0x74, 0x73, 0x2e, 0x45, - 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x10, 0x54, 0x65, 0x73, 0x74, - 0x73, 0x5c, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x1c, 0x54, - 0x65, 0x73, 0x74, 0x73, 0x5c, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x5c, 0x56, 0x31, 0x5c, - 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x12, 0x54, 0x65, - 0x73, 0x74, 0x73, 0x3a, 0x3a, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3a, 0x3a, 0x56, 0x31, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x79, 0x42, 0xd8, 0x01, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2e, + 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x10, 0x56, 0x61, 0x6c, 0x69, + 0x64, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x4c, + 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x75, 0x66, 0x62, 0x75, + 0x69, 0x6c, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, + 0x65, 0x2d, 0x67, 0x6f, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x67, 0x65, + 0x6e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, + 0x76, 0x31, 0x3b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x54, + 0x45, 0x58, 0xaa, 0x02, 0x10, 0x54, 0x65, 0x73, 0x74, 0x73, 0x2e, 0x45, 0x78, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x10, 0x54, 0x65, 0x73, 0x74, 0x73, 0x5c, 0x45, 0x78, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x1c, 0x54, 0x65, 0x73, 0x74, 0x73, + 0x5c, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, + 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x12, 0x54, 0x65, 0x73, 0x74, 0x73, 0x3a, + 0x3a, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -911,7 +823,7 @@ func file_tests_example_v1_validations_proto_rawDescGZIP() []byte { return file_tests_example_v1_validations_proto_rawDescData } -var file_tests_example_v1_validations_proto_msgTypes = make([]protoimpl.MessageInfo, 15) +var file_tests_example_v1_validations_proto_msgTypes = make([]protoimpl.MessageInfo, 14) var file_tests_example_v1_validations_proto_goTypes = []interface{}{ (*HasMsgExprs)(nil), // 0: tests.example.v1.HasMsgExprs (*SelfRecursive)(nil), // 1: tests.example.v1.SelfRecursive @@ -924,13 +836,12 @@ var file_tests_example_v1_validations_proto_goTypes = []interface{}{ (*MultipleStepsTransitiveFieldConstraints)(nil), // 8: tests.example.v1.MultipleStepsTransitiveFieldConstraints (*Simple)(nil), // 9: tests.example.v1.Simple (*FieldOfTypeAny)(nil), // 10: tests.example.v1.FieldOfTypeAny - (*RequiredOneof)(nil), // 11: tests.example.v1.RequiredOneof - nil, // 12: tests.example.v1.MsgHasMap.Int32mapEntry - nil, // 13: tests.example.v1.MsgHasMap.StringMapEntry - nil, // 14: tests.example.v1.MsgHasMap.MessageMapEntry - (*fieldmaskpb.FieldMask)(nil), // 15: google.protobuf.FieldMask - (*apipb.Api)(nil), // 16: google.protobuf.Api - (*anypb.Any)(nil), // 17: google.protobuf.Any + nil, // 11: tests.example.v1.MsgHasMap.Int32mapEntry + nil, // 12: tests.example.v1.MsgHasMap.StringMapEntry + nil, // 13: tests.example.v1.MsgHasMap.MessageMapEntry + (*fieldmaskpb.FieldMask)(nil), // 14: google.protobuf.FieldMask + (*apipb.Api)(nil), // 15: google.protobuf.Api + (*anypb.Any)(nil), // 16: google.protobuf.Any } var file_tests_example_v1_validations_proto_depIdxs = []int32{ 1, // 0: tests.example.v1.SelfRecursive.turtle:type_name -> tests.example.v1.SelfRecursive @@ -938,19 +849,18 @@ var file_tests_example_v1_validations_proto_depIdxs = []int32{ 2, // 2: tests.example.v1.LoopRecursiveB.a:type_name -> tests.example.v1.LoopRecursiveA 0, // 3: tests.example.v1.MsgHasOneof.msg:type_name -> tests.example.v1.HasMsgExprs 0, // 4: tests.example.v1.MsgHasRepeated.z:type_name -> tests.example.v1.HasMsgExprs - 12, // 5: tests.example.v1.MsgHasMap.int32map:type_name -> tests.example.v1.MsgHasMap.Int32mapEntry - 13, // 6: tests.example.v1.MsgHasMap.string_map:type_name -> tests.example.v1.MsgHasMap.StringMapEntry - 14, // 7: tests.example.v1.MsgHasMap.message_map:type_name -> tests.example.v1.MsgHasMap.MessageMapEntry - 15, // 8: tests.example.v1.TransitiveFieldConstraint.mask:type_name -> google.protobuf.FieldMask - 16, // 9: tests.example.v1.MultipleStepsTransitiveFieldConstraints.api:type_name -> google.protobuf.Api - 17, // 10: tests.example.v1.FieldOfTypeAny.any:type_name -> google.protobuf.Any - 15, // 11: tests.example.v1.RequiredOneof.fld_2:type_name -> google.protobuf.FieldMask - 2, // 12: tests.example.v1.MsgHasMap.MessageMapEntry.value:type_name -> tests.example.v1.LoopRecursiveA - 13, // [13:13] is the sub-list for method output_type - 13, // [13:13] is the sub-list for method input_type - 13, // [13:13] is the sub-list for extension type_name - 13, // [13:13] is the sub-list for extension extendee - 0, // [0:13] is the sub-list for field type_name + 11, // 5: tests.example.v1.MsgHasMap.int32map:type_name -> tests.example.v1.MsgHasMap.Int32mapEntry + 12, // 6: tests.example.v1.MsgHasMap.string_map:type_name -> tests.example.v1.MsgHasMap.StringMapEntry + 13, // 7: tests.example.v1.MsgHasMap.message_map:type_name -> tests.example.v1.MsgHasMap.MessageMapEntry + 14, // 8: tests.example.v1.TransitiveFieldConstraint.mask:type_name -> google.protobuf.FieldMask + 15, // 9: tests.example.v1.MultipleStepsTransitiveFieldConstraints.api:type_name -> google.protobuf.Api + 16, // 10: tests.example.v1.FieldOfTypeAny.any:type_name -> google.protobuf.Any + 2, // 11: tests.example.v1.MsgHasMap.MessageMapEntry.value:type_name -> tests.example.v1.LoopRecursiveA + 12, // [12:12] is the sub-list for method output_type + 12, // [12:12] is the sub-list for method input_type + 12, // [12:12] is the sub-list for extension type_name + 12, // [12:12] is the sub-list for extension extendee + 0, // [0:12] is the sub-list for field type_name } func init() { file_tests_example_v1_validations_proto_init() } @@ -1091,35 +1001,19 @@ func file_tests_example_v1_validations_proto_init() { return nil } } - file_tests_example_v1_validations_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*RequiredOneof); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } } file_tests_example_v1_validations_proto_msgTypes[4].OneofWrappers = []interface{}{ (*MsgHasOneof_X)(nil), (*MsgHasOneof_Y)(nil), (*MsgHasOneof_Msg)(nil), } - file_tests_example_v1_validations_proto_msgTypes[11].OneofWrappers = []interface{}{ - (*RequiredOneof_Fld_1)(nil), - (*RequiredOneof_Fld_2)(nil), - } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_tests_example_v1_validations_proto_rawDesc, NumEnums: 0, - NumMessages: 15, + NumMessages: 14, NumExtensions: 0, NumServices: 0, }, diff --git a/proto/tests/example/v1/validations.proto b/proto/tests/example/v1/validations.proto index 708b9d2..f5b7307 100644 --- a/proto/tests/example/v1/validations.proto +++ b/proto/tests/example/v1/validations.proto @@ -153,11 +153,3 @@ message FieldOfTypeAny { expression: "this == this" }]; } - -message RequiredOneof { - oneof required_oneof { - option (buf.validate.oneof).required = true; - string fld_1 = 1; - google.protobuf.FieldMask fld_2 = 2; - } -} diff --git a/validator_test.go b/validator_test.go index e11ee73..d2aeff9 100644 --- a/validator_test.go +++ b/validator_test.go @@ -167,65 +167,3 @@ func TestValidator_Validate_FieldOfTypeAny(t *testing.T) { err = val.Validate(msg) require.NoError(t, err) } - -func TestValidator_RequiredOneof(t *testing.T) { - t.Parallel() - val, err := New() - require.NoError(t, err) - - tests := []struct { - msg *pb.RequiredOneof - exErr bool - }{ - { - &pb.RequiredOneof{}, - true, - }, - { - &pb.RequiredOneof{ - RequiredOneof: &pb.RequiredOneof_Fld_2{ - // nil FieldMask is empty. - Fld_2: nil, - }, - }, - true, - }, - { - &pb.RequiredOneof{ - RequiredOneof: &pb.RequiredOneof_Fld_2{ - // this passes `required` - Fld_2: &fieldmaskpb.FieldMask{}, - }, - }, - false, - }, - { - &pb.RequiredOneof{ - RequiredOneof: &pb.RequiredOneof_Fld_1{ - // empty string does not pass `required` as a normal field, - // and it shouldn't pass it here either. - Fld_1: "", - }, - }, - true, - }, - { - &pb.RequiredOneof{ - RequiredOneof: &pb.RequiredOneof_Fld_1{ - // passes `required` - Fld_1: "foo", - }, - }, - false, - }, - } - for _, test := range tests { - test := test - err = val.Validate(test.msg) - if test.exErr { - require.Error(t, err) - } else { - require.NoError(t, err) - } - } -}