Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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,48 @@ 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;
}

message MessageOneofNoFields {
option (buf.validate.message).oneof = {
fields: []
};
string str_field = 1;
}
27 changes: 27 additions & 0 deletions proto/protovalidate/buf/validate/validate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,33 @@ 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 };
// string field1 = 1;
// bytes field2 = 2;
// bool field3 = 3;
// int32 field4 = 4;
// }
// ```
repeated MessageOneofRule oneof = 4;
}

message MessageOneofRule {
// A list of field names to include in the oneof. All field names must be
// defined in the message.
repeated string fields = 1;
// If true, one of the fields specified _must_ be set.
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
Loading
Loading