Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,41 @@ message MessageRequiredOneof {
}

message MessageWith3dInside {}

message MessageOneofSingleField {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test case for MessageOneofSingleFieldRequired?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do in followup PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: #379

option (buf.validate.message).oneof = {
fields: ["str_field"]
};
string str_field = 1;
bool bool_field = 2;
}

message MessageOneofMultipleFields {
option (buf.validate.message).oneof = {
fields: [
"str_field",
"bool_field"
]
};
string str_field = 1;
bool bool_field = 2;
}

message MessageOneofMultipleFieldsRequired {
option (buf.validate.message).oneof = {
fields: [
"str_field",
"bool_field"
]
required: true
};
string str_field = 1;
bool bool_field = 2;
}

message MessageOneofUnknownFieldName {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider also having oneof with no fields (which should also be an error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few rough edges (like this, like if you specify the same field name multiple times, and certainly others we haven't had time to think of yet), but the desire was to get this out fast. We'll keep track of these and hopefully can fix them soon, but I don't want to block this on edge cases.

The faster you can start using this, the faster you can help us find more of those edges cases and we can take care of them!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In light of this, I removed the MessageOneofNoFields test case in fd2217b and we can address as a follow-up.

option (buf.validate.message).oneof = {
fields: ["xxx"]
};
string str_field = 1;
}
20 changes: 20 additions & 0 deletions proto/protovalidate/buf/validate/validate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ message MessageRules {
// }
// ```
repeated Rule cel = 3;

// `oneof` is a repeated field of type MessageOneofRule that specifies a list of fields
// of which exactly one can be present. If `required` is also specified, then exactly one
// of the specified fields _must_ be present.
//
//
// ```proto
// message MyMessage {
// // Only one of 'field1' or 'field2' _can_ be present in this message.
// option (buf.validate.message).oneof = { fields: ["field1", "field2"] };
// // Only one of 'field3' or 'field4' _must_ be present in this message.
// option (buf.validate.message).oneof = { fields: ["field3", "field4"], required: true };
// }
// ```
repeated MessageOneofRule oneof = 4;
}

message MessageOneofRule {
repeated string fields = 1;
optional bool required = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments on the fields too?

}

// The `OneofRules` message type enables you to manage rules for
Expand Down
265 changes: 246 additions & 19 deletions tools/internal/gen/buf/validate/conformance/cases/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading