Skip to content

Commit

Permalink
Add conformance tests for ignore_empty (#126)
Browse files Browse the repository at this point in the history
Following #124, this adds conformance tests that ensure the
`ignore_empty` rules is consistent with `required`. Mainly, if a field
cannot differentiate between unset and the zero value (i.e., is not
nullable), then this rule applies; effectively this is just repeated and
map fields, as well as non-optional proto3 scalar outside of a oneof.

Running against `protovalidate-go`, a few places where `ignore_empty`
should be a noop ends up disabling evaluations. This will be a minor
follow-up fix in that library. Patches for the other libraries will
follow to bring them into conformance with these tests and those in
#124.
  • Loading branch information
rodaine committed Nov 6, 2023
1 parent fa528d8 commit 56ee282
Show file tree
Hide file tree
Showing 7 changed files with 1,627 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ proto_library(
"bytes.proto",
"enums.proto",
"filename-with-dash.proto",
"ignore_empty_proto2.proto",
"ignore_empty_proto3.proto",
"kitchen_sink.proto",
"maps.proto",
"messages.proto",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2023 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
//
// https://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 buf.validate.conformance.cases;

import "buf/validate/validate.proto";

message IgnoreEmptyProto2ScalarOptional {
optional int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyProto2ScalarOptionalWithDefault {
optional int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0,
default = 42
];
}

message IgnoreEmptyProto2ScalarRequired {
required int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyProto2Message {
optional Msg val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).cel = {
id: "ignore_empty.proto2.message",
message: "foobar",
expression: "this.val == 'foo'",
}
];
message Msg {
optional string val = 1;
}
}

message IgnoreEmptyProto2Oneof {
oneof o {
int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}
}

message IgnoreEmptyProto2Repeated {
repeated int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).repeated.min_items = 3
];
}

message IgnoreEmptyProto2Map {
map<int32, int32> val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).map.min_pairs = 3
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2023 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
//
// https://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 = "proto3";

package buf.validate.conformance.cases;

import "buf/validate/validate.proto";

message IgnoreEmptyProto3Scalar {
int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyProto3OptionalScalar {
optional int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}

message IgnoreEmptyProto3Message {
optional Msg val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).cel = {
id: "ignore_empty.proto3.message",
message: "foobar",
expression: "this.val == 'foo'",
}
];
message Msg {
string val = 1;
}
}

message IgnoreEmptyProto3Oneof {
oneof o {
int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).int32.gt = 0
];
}
}

message IgnoreEmptyProto3Repeated {
repeated int32 val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).repeated.min_items = 3
];
}

message IgnoreEmptyProto3Map {
map<int32, int32> val = 1 [
(buf.validate.field).ignore_empty = true,
(buf.validate.field).map.min_pairs = 3
];
}
Loading

0 comments on commit 56ee282

Please sign in to comment.