From 4d5a8292faf123aded1b0745d8d0c01a96453363 Mon Sep 17 00:00:00 2001 From: Chris Roche Date: Mon, 30 Sep 2024 15:09:49 -0700 Subject: [PATCH] checkpoint --- buf.lock | 8 +- .../gen/tests/example/v1/predefined.pb.go | 206 ++++++++++++++++++ proto/tests/example/v1/predefined.proto | 38 ++++ proto/tests/example/v1/validations.proto | 2 +- validator_test.go | 10 + 5 files changed, 259 insertions(+), 5 deletions(-) create mode 100644 internal/gen/tests/example/v1/predefined.pb.go create mode 100644 proto/tests/example/v1/predefined.proto diff --git a/buf.lock b/buf.lock index 11b5b32..9a585dd 100644 --- a/buf.lock +++ b/buf.lock @@ -2,11 +2,11 @@ version: v2 deps: - name: buf.build/bufbuild/protovalidate - commit: a6c49f84cc0f4e038680d390392e2ab0 - digest: b5:e968392e88ff7915adcbd1635d670b45bff8836ec2415d81fc559ca5470a695dbdc30030bad8bc5764647c731079e9e7bba0023ea25c4e4a1672a7d2561d4a19 + commit: 5a7b106cbb87462d9a8c9ffecdbd2e38 + digest: b5:0f2dc6c9453e9cc9e9f36807aaa5f94022e837d91fef4dcaeed79a35c0843cc64eba28ff077aab24da3b2cb12639ad256246f9f9a36c033b99d5754b19996b7e - name: buf.build/bufbuild/protovalidate-testing - commit: 7c0e0340f14c4d6580c200dcaa9b00d0 - digest: b5:f0dacc3a02e3fcd277c3516cd0089e69478374b5ad15fe1032853789668d1d660cac4c6e1776799a66b251b9583ddc320e9db91acf6e8681e31d76ccc1f22ea3 + commit: 165ebb0f38244fc2a5b23a82cabe812f + digest: b5:4e3e21cabc93277d9069f7ded53c17659fefe47dd8daf9bc534387590feabe971b2d51b88b324835385650f3fa227e8a793a99ca9814cba76ee678f20ba9b4ef - name: buf.build/envoyproxy/protoc-gen-validate commit: daf171c6cdb54629b5f51e345a79e4dd digest: b5:c745e1521879f43740230b1df673d0729f55704efefdcfc489d4a0a2d40c92a26cacfeab62813403040a8b180142d53b398c7ca784a065e43823605ee49681de diff --git a/internal/gen/tests/example/v1/predefined.pb.go b/internal/gen/tests/example/v1/predefined.pb.go new file mode 100644 index 0000000..812d166 --- /dev/null +++ b/internal/gen/tests/example/v1/predefined.pb.go @@ -0,0 +1,206 @@ +// Copyright 2023-2024 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.1 +// protoc (unknown) +// source: tests/example/v1/predefined.proto + +package examplev1 + +import ( + validate "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// https://github.com/bufbuild/protovalidate-go/issues/148 +type Issue148 struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Test *int32 `protobuf:"varint,1,opt,name=test" json:"test,omitempty"` +} + +func (x *Issue148) Reset() { + *x = Issue148{} + if protoimpl.UnsafeEnabled { + mi := &file_tests_example_v1_predefined_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Issue148) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Issue148) ProtoMessage() {} + +func (x *Issue148) ProtoReflect() protoreflect.Message { + mi := &file_tests_example_v1_predefined_proto_msgTypes[0] + 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 Issue148.ProtoReflect.Descriptor instead. +func (*Issue148) Descriptor() ([]byte, []int) { + return file_tests_example_v1_predefined_proto_rawDescGZIP(), []int{0} +} + +func (x *Issue148) GetTest() int32 { + if x != nil && x.Test != nil { + return *x.Test + } + return 0 +} + +var file_tests_example_v1_predefined_proto_extTypes = []protoimpl.ExtensionInfo{ + { + ExtendedType: (*validate.Int32Rules)(nil), + ExtensionType: ([]int32)(nil), + Field: 1800, + Name: "tests.example.v1.abs_not_in", + Tag: "varint,1800,rep,name=abs_not_in", + Filename: "tests/example/v1/predefined.proto", + }, +} + +// Extension fields to validate.Int32Rules. +var ( + // repeated int32 abs_not_in = 1800; + E_AbsNotIn = &file_tests_example_v1_predefined_proto_extTypes[0] +) + +var File_tests_example_v1_predefined_proto protoreflect.FileDescriptor + +var file_tests_example_v1_predefined_proto_rawDesc = []byte{ + 0x0a, 0x21, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, + 0x76, 0x31, 0x2f, 0x70, 0x72, 0x65, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65, 0x64, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x12, 0x10, 0x74, 0x65, 0x73, 0x74, 0x73, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x2e, 0x76, 0x31, 0x1a, 0x1b, 0x62, 0x75, 0x66, 0x2f, 0x76, 0x61, 0x6c, 0x69, 0x64, + 0x61, 0x74, 0x65, 0x2f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x22, 0x3a, 0x0a, 0x08, 0x49, 0x73, 0x73, 0x75, 0x65, 0x31, 0x34, 0x38, 0x12, 0x2e, + 0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x42, 0x1a, 0xba, 0x48, + 0x17, 0x1a, 0x15, 0xc0, 0x70, 0x01, 0xc0, 0x70, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0x01, 0x40, 0x03, 0x40, 0x02, 0x10, 0x05, 0x52, 0x04, 0x74, 0x65, 0x73, 0x74, 0x3a, 0xa6, + 0x01, 0x0a, 0x0a, 0x61, 0x62, 0x73, 0x5f, 0x6e, 0x6f, 0x74, 0x5f, 0x69, 0x6e, 0x12, 0x18, 0x2e, + 0x62, 0x75, 0x66, 0x2e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x65, 0x2e, 0x49, 0x6e, 0x74, + 0x33, 0x32, 0x52, 0x75, 0x6c, 0x65, 0x73, 0x18, 0x88, 0x0e, 0x20, 0x03, 0x28, 0x05, 0x42, 0x6d, + 0xc2, 0x48, 0x6a, 0x0a, 0x68, 0x0a, 0x10, 0x69, 0x6e, 0x74, 0x33, 0x32, 0x2e, 0x61, 0x62, 0x73, + 0x5f, 0x6e, 0x6f, 0x74, 0x5f, 0x69, 0x6e, 0x12, 0x2b, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x20, 0x6d, + 0x75, 0x73, 0x74, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x62, + 0x73, 0x6f, 0x6c, 0x75, 0x74, 0x65, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x20, 0x6f, 0x66, 0x20, + 0x6c, 0x69, 0x73, 0x74, 0x1a, 0x27, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x72, 0x75, + 0x6c, 0x65, 0x20, 0x7c, 0x7c, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x72, 0x75, + 0x6c, 0x65, 0x2e, 0x6d, 0x61, 0x70, 0x28, 0x6e, 0x2c, 0x20, 0x2d, 0x6e, 0x29, 0x52, 0x08, 0x61, + 0x62, 0x73, 0x4e, 0x6f, 0x74, 0x49, 0x6e, 0x42, 0xd7, 0x01, 0x0a, 0x14, 0x63, 0x6f, 0x6d, 0x2e, + 0x74, 0x65, 0x73, 0x74, 0x73, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, 0x31, + 0x42, 0x0f, 0x50, 0x72, 0x65, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65, 0x64, 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, +} + +var ( + file_tests_example_v1_predefined_proto_rawDescOnce sync.Once + file_tests_example_v1_predefined_proto_rawDescData = file_tests_example_v1_predefined_proto_rawDesc +) + +func file_tests_example_v1_predefined_proto_rawDescGZIP() []byte { + file_tests_example_v1_predefined_proto_rawDescOnce.Do(func() { + file_tests_example_v1_predefined_proto_rawDescData = protoimpl.X.CompressGZIP(file_tests_example_v1_predefined_proto_rawDescData) + }) + return file_tests_example_v1_predefined_proto_rawDescData +} + +var file_tests_example_v1_predefined_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_tests_example_v1_predefined_proto_goTypes = []interface{}{ + (*Issue148)(nil), // 0: tests.example.v1.Issue148 + (*validate.Int32Rules)(nil), // 1: buf.validate.Int32Rules +} +var file_tests_example_v1_predefined_proto_depIdxs = []int32{ + 1, // 0: tests.example.v1.abs_not_in:extendee -> buf.validate.Int32Rules + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 0, // [0:1] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_tests_example_v1_predefined_proto_init() } +func file_tests_example_v1_predefined_proto_init() { + if File_tests_example_v1_predefined_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_tests_example_v1_predefined_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Issue148); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_tests_example_v1_predefined_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 1, + NumServices: 0, + }, + GoTypes: file_tests_example_v1_predefined_proto_goTypes, + DependencyIndexes: file_tests_example_v1_predefined_proto_depIdxs, + MessageInfos: file_tests_example_v1_predefined_proto_msgTypes, + ExtensionInfos: file_tests_example_v1_predefined_proto_extTypes, + }.Build() + File_tests_example_v1_predefined_proto = out.File + file_tests_example_v1_predefined_proto_rawDesc = nil + file_tests_example_v1_predefined_proto_goTypes = nil + file_tests_example_v1_predefined_proto_depIdxs = nil +} diff --git a/proto/tests/example/v1/predefined.proto b/proto/tests/example/v1/predefined.proto new file mode 100644 index 0000000..442f32c --- /dev/null +++ b/proto/tests/example/v1/predefined.proto @@ -0,0 +1,38 @@ +// Copyright 2023-2024 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto2"; + +package tests.example.v1; + +import "buf/validate/validate.proto"; + +// https://github.com/bufbuild/protovalidate-go/issues/148 +message Issue148 { + optional int32 test = 1 [ + (buf.validate.field).int32.lt = 5, + (buf.validate.field).int32.(abs_not_in) = 1, + (buf.validate.field).int32.(abs_not_in) = -2, + (buf.validate.field).int32.example = 3, + (buf.validate.field).int32.example = 2 + ]; +} + +extend buf.validate.Int32Rules { + repeated int32 abs_not_in = 1800 [(buf.validate.predefined).cel = { + id: "int32.abs_not_in" + expression: "this in rule || this in rule.map(n, -n)" + message: "value must not be in absolute value of list" + }]; +} \ No newline at end of file diff --git a/proto/tests/example/v1/validations.proto b/proto/tests/example/v1/validations.proto index ddc5771..ece3c56 100644 --- a/proto/tests/example/v1/validations.proto +++ b/proto/tests/example/v1/validations.proto @@ -197,4 +197,4 @@ message F2 { message FieldWithIssue { F1 f1 = 1; string name = 2 [(buf.validate.field).string.min_len = 1]; -} +} \ No newline at end of file diff --git a/validator_test.go b/validator_test.go index 5684077..251b078 100644 --- a/validator_test.go +++ b/validator_test.go @@ -20,6 +20,7 @@ import ( pb "github.com/bufbuild/protovalidate-go/internal/gen/tests/example/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/apipb" "google.golang.org/protobuf/types/known/fieldmaskpb" @@ -240,3 +241,12 @@ func TestValidator_Validate_Issue141(t *testing.T) { require.ErrorAs(t, err, &valErr) }) } + +func TestValidator_Validate_Issue148(t *testing.T) { + t.Parallel() + val, err := New() + require.NoError(t, err) + msg := &pb.Issue148{Test: proto.Int32(1)} + err = val.Validate(msg) + require.NoError(t, err) +}