-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve required standard constraint docs + conformance tests #124
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,15 @@ message OneofRequired { | |
} | ||
} | ||
|
||
message OneofRequiredWithRequiredField { | ||
oneof o { | ||
option (buf.validate.oneof).required = true; | ||
|
||
string a = 1 [(buf.validate.field).required = true]; | ||
string b = 2; | ||
} | ||
} | ||
|
||
message OneofIgnoreEmpty { | ||
oneof o { | ||
string x = 1 [ | ||
|
@@ -66,8 +75,8 @@ message OneofIgnoreEmpty { | |
]; | ||
int32 z = 3 [ | ||
(buf.validate.field).int32 = { | ||
lte: 128, | ||
gte: 256, | ||
gte: 128, | ||
lte: 256, | ||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, it was legal the other way, too. Alfred was explaining this to me the other day, that when the low and high are flipped, it effectively enforces that the value is not in the range in between (i.e. "gte" and "lte" end up combined with OR instead of AND). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct, however in context of testing the |
||
}, | ||
(buf.validate.field).ignore_empty = true | ||
]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 | ||
// | ||
// 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 buf.validate.conformance.cases; | ||
|
||
import "buf/validate/validate.proto"; | ||
|
||
message RequiredProto2ScalarOptional { | ||
optional string val = 1 [(buf.validate.field).required = true]; | ||
} | ||
|
||
message RequiredProto2ScalarOptionalDefault { | ||
optional string val = 1 [ | ||
(buf.validate.field).required = true, | ||
default = "foo" | ||
]; | ||
} | ||
|
||
message RequiredProto2ScalarRequired { | ||
required string val = 1 [(buf.validate.field).required = true]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentional to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, this is mainly to be exhaustive. At least in protobuf-go, you can set the (un)marshaler options to allow partials and let unset required fields to pass through. |
||
} | ||
|
||
message RequiredProto2Message { | ||
optional Msg val = 1 [(buf.validate.field).required = true]; | ||
message Msg { | ||
optional string val = 1; | ||
} | ||
} | ||
|
||
message RequiredProto2Oneof { | ||
oneof val { | ||
string a = 1 [(buf.validate.field).required = true]; | ||
string b = 2; | ||
} | ||
} | ||
|
||
message RequiredProto2Repeated { | ||
repeated string val = 1 [(buf.validate.field).required = true]; | ||
} | ||
|
||
message RequiredProto2Map { | ||
map<string, string> val = 1 [(buf.validate.field).required = true]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 | ||
// | ||
// 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 = "proto3"; | ||
|
||
package buf.validate.conformance.cases; | ||
|
||
import "buf/validate/validate.proto"; | ||
|
||
message RequiredProto3Scalar { | ||
string val = 1 [(buf.validate.field).required = true]; | ||
} | ||
|
||
message RequiredProto3OptionalScalar { | ||
optional string val = 1 [(buf.validate.field).required = true]; | ||
} | ||
|
||
message RequiredProto3Message { | ||
Msg val = 1 [(buf.validate.field).required = true]; | ||
message Msg { | ||
string val = 1; | ||
} | ||
} | ||
|
||
message RequiredProto3OneOf { | ||
oneof val { | ||
string a = 1 [(buf.validate.field).required = true]; | ||
string b = 2; | ||
} | ||
} | ||
|
||
message RequiredProto3Repeated { | ||
repeated string val = 1 [(buf.validate.field).required = true]; | ||
} | ||
|
||
message RequiredProto3Map { | ||
map<string, string> val = 1 [(buf.validate.field).required = true]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside: this seems like a thing to lint for, no? This effectively makes it impossible to ever use field
b
and the message be valid, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I only included it to be exhaustive! I believe @oliversun9 is covering that in the linting work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is covered in bufbuild/buf#2528