diff --git a/internal/evaluator/builder.go b/internal/evaluator/builder.go index 1057cf9..4264b01 100644 --- a/internal/evaluator/builder.go +++ b/internal/evaluator/builder.go @@ -270,7 +270,7 @@ func (bldr *Builder) processIgnoreEmpty( func (bldr *Builder) processFieldExpressions( fieldDesc protoreflect.FieldDescriptor, fieldConstraints *validate.FieldConstraints, - _ bool, + forItems bool, eval *value, _ MessageCache, ) error { @@ -279,7 +279,7 @@ func (bldr *Builder) processFieldExpressions( return nil } - celTyp := celext.ProtoFieldToCELType(fieldDesc, false, false) + celTyp := celext.ProtoFieldToCELType(fieldDesc, false, forItems) opts := append( celext.RequiredCELEnvOptions(fieldDesc), cel.Variable("this", celTyp), diff --git a/internal/gen/tests/example/v1/validations.pb.go b/internal/gen/tests/example/v1/validations.pb.go index 0669b72..6ad6264 100644 --- a/internal/gen/tests/example/v1/validations.pb.go +++ b/internal/gen/tests/example/v1/validations.pb.go @@ -699,6 +699,53 @@ func (x *CelMapOnARepeated) GetValues() []*CelMapOnARepeated_Value { return nil } +type RepeatedItemCel struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Paths []string `protobuf:"bytes,1,rep,name=paths,proto3" json:"paths,omitempty"` +} + +func (x *RepeatedItemCel) Reset() { + *x = RepeatedItemCel{} + if protoimpl.UnsafeEnabled { + mi := &file_tests_example_v1_validations_proto_msgTypes[12] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RepeatedItemCel) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RepeatedItemCel) ProtoMessage() {} + +func (x *RepeatedItemCel) ProtoReflect() protoreflect.Message { + mi := &file_tests_example_v1_validations_proto_msgTypes[12] + 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 RepeatedItemCel.ProtoReflect.Descriptor instead. +func (*RepeatedItemCel) Descriptor() ([]byte, []int) { + return file_tests_example_v1_validations_proto_rawDescGZIP(), []int{12} +} + +func (x *RepeatedItemCel) GetPaths() []string { + if x != nil { + return x.Paths + } + return nil +} + type CelMapOnARepeated_Value struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -710,7 +757,7 @@ type CelMapOnARepeated_Value struct { func (x *CelMapOnARepeated_Value) Reset() { *x = CelMapOnARepeated_Value{} if protoimpl.UnsafeEnabled { - mi := &file_tests_example_v1_validations_proto_msgTypes[15] + mi := &file_tests_example_v1_validations_proto_msgTypes[16] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -723,7 +770,7 @@ func (x *CelMapOnARepeated_Value) String() string { func (*CelMapOnARepeated_Value) ProtoMessage() {} func (x *CelMapOnARepeated_Value) ProtoReflect() protoreflect.Message { - mi := &file_tests_example_v1_validations_proto_msgTypes[15] + mi := &file_tests_example_v1_validations_proto_msgTypes[16] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -902,21 +949,27 @@ var file_tests_example_v1_validations_proto_rawDesc = []byte{ 0x75, 0x73, 0x74, 0x20, 0x62, 0x65, 0x20, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x27, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x1a, 0x1b, 0x0a, 0x05, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 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, + 0x61, 0x6d, 0x65, 0x22, 0x5b, 0x0a, 0x0f, 0x52, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65, 0x64, 0x49, + 0x74, 0x65, 0x6d, 0x43, 0x65, 0x6c, 0x12, 0x48, 0x0a, 0x05, 0x70, 0x61, 0x74, 0x68, 0x73, 0x18, + 0x01, 0x20, 0x03, 0x28, 0x09, 0x42, 0x32, 0xba, 0x48, 0x2f, 0x92, 0x01, 0x2c, 0x22, 0x2a, 0xba, + 0x01, 0x27, 0x0a, 0x0e, 0x70, 0x61, 0x74, 0x68, 0x73, 0x2e, 0x6e, 0x6f, 0x5f, 0x73, 0x70, 0x61, + 0x63, 0x65, 0x1a, 0x15, 0x21, 0x74, 0x68, 0x69, 0x73, 0x2e, 0x73, 0x74, 0x61, 0x72, 0x74, 0x73, + 0x57, 0x69, 0x74, 0x68, 0x28, 0x27, 0x20, 0x27, 0x29, 0x52, 0x05, 0x70, 0x61, 0x74, 0x68, 0x73, + 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 ( @@ -931,7 +984,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, 16) +var file_tests_example_v1_validations_proto_msgTypes = make([]protoimpl.MessageInfo, 17) var file_tests_example_v1_validations_proto_goTypes = []interface{}{ (*HasMsgExprs)(nil), // 0: tests.example.v1.HasMsgExprs (*SelfRecursive)(nil), // 1: tests.example.v1.SelfRecursive @@ -945,13 +998,14 @@ var file_tests_example_v1_validations_proto_goTypes = []interface{}{ (*Simple)(nil), // 9: tests.example.v1.Simple (*FieldOfTypeAny)(nil), // 10: tests.example.v1.FieldOfTypeAny (*CelMapOnARepeated)(nil), // 11: tests.example.v1.CelMapOnARepeated - nil, // 12: tests.example.v1.MsgHasMap.Int32mapEntry - nil, // 13: tests.example.v1.MsgHasMap.StringMapEntry - nil, // 14: tests.example.v1.MsgHasMap.MessageMapEntry - (*CelMapOnARepeated_Value)(nil), // 15: tests.example.v1.CelMapOnARepeated.Value - (*fieldmaskpb.FieldMask)(nil), // 16: google.protobuf.FieldMask - (*apipb.Api)(nil), // 17: google.protobuf.Api - (*anypb.Any)(nil), // 18: google.protobuf.Any + (*RepeatedItemCel)(nil), // 12: tests.example.v1.RepeatedItemCel + nil, // 13: tests.example.v1.MsgHasMap.Int32mapEntry + nil, // 14: tests.example.v1.MsgHasMap.StringMapEntry + nil, // 15: tests.example.v1.MsgHasMap.MessageMapEntry + (*CelMapOnARepeated_Value)(nil), // 16: tests.example.v1.CelMapOnARepeated.Value + (*fieldmaskpb.FieldMask)(nil), // 17: google.protobuf.FieldMask + (*apipb.Api)(nil), // 18: google.protobuf.Api + (*anypb.Any)(nil), // 19: 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 @@ -959,13 +1013,13 @@ 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 - 16, // 8: tests.example.v1.TransitiveFieldConstraint.mask:type_name -> google.protobuf.FieldMask - 17, // 9: tests.example.v1.MultipleStepsTransitiveFieldConstraints.api:type_name -> google.protobuf.Api - 18, // 10: tests.example.v1.FieldOfTypeAny.any:type_name -> google.protobuf.Any - 15, // 11: tests.example.v1.CelMapOnARepeated.values:type_name -> tests.example.v1.CelMapOnARepeated.Value + 13, // 5: tests.example.v1.MsgHasMap.int32map:type_name -> tests.example.v1.MsgHasMap.Int32mapEntry + 14, // 6: tests.example.v1.MsgHasMap.string_map:type_name -> tests.example.v1.MsgHasMap.StringMapEntry + 15, // 7: tests.example.v1.MsgHasMap.message_map:type_name -> tests.example.v1.MsgHasMap.MessageMapEntry + 17, // 8: tests.example.v1.TransitiveFieldConstraint.mask:type_name -> google.protobuf.FieldMask + 18, // 9: tests.example.v1.MultipleStepsTransitiveFieldConstraints.api:type_name -> google.protobuf.Api + 19, // 10: tests.example.v1.FieldOfTypeAny.any:type_name -> google.protobuf.Any + 16, // 11: tests.example.v1.CelMapOnARepeated.values:type_name -> tests.example.v1.CelMapOnARepeated.Value 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 @@ -1124,7 +1178,19 @@ func file_tests_example_v1_validations_proto_init() { return nil } } - file_tests_example_v1_validations_proto_msgTypes[15].Exporter = func(v interface{}, i int) interface{} { + file_tests_example_v1_validations_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*RepeatedItemCel); 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[16].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*CelMapOnARepeated_Value); i { case 0: return &v.state @@ -1148,7 +1214,7 @@ func file_tests_example_v1_validations_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_tests_example_v1_validations_proto_rawDesc, NumEnums: 0, - NumMessages: 16, + NumMessages: 17, NumExtensions: 0, NumServices: 0, }, diff --git a/proto/tests/example/v1/validations.proto b/proto/tests/example/v1/validations.proto index b6f0507..e10842a 100644 --- a/proto/tests/example/v1/validations.proto +++ b/proto/tests/example/v1/validations.proto @@ -165,3 +165,10 @@ message CelMapOnARepeated { string name = 1; } } + +message RepeatedItemCel { + repeated string paths = 1 [(buf.validate.field).repeated.items.cel = { + id: "paths.no_space", + expression: "!this.startsWith(' ')" + }]; +} diff --git a/validator_test.go b/validator_test.go index 5944843..10b631d 100644 --- a/validator_test.go +++ b/validator_test.go @@ -184,3 +184,17 @@ func TestValidator_Validate_CelMapOnARepeated(t *testing.T) { valErr := &ValidationError{} assert.ErrorAs(t, err, &valErr) } + +func TestValidator_Validate_RepeatedItemCel(t *testing.T) { + t.Parallel() + val, err := New() + require.NoError(t, err) + msg := &pb.RepeatedItemCel{Paths: []string{"foo"}} + err = val.Validate(msg) + require.NoError(t, err) + msg.Paths = append(msg.Paths, " bar") + err = val.Validate(msg) + valErr := &ValidationError{} + assert.ErrorAs(t, err, &valErr) + assert.Equal(t, "paths.no_space", valErr.Violations[0].ConstraintId) +}