Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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;
}
40 changes: 40 additions & 0 deletions proto/protovalidate/buf/validate/validate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,46 @@ message MessageRules {
// }
// ```
repeated Rule cel = 3;

// `oneof` is a repeated field of type MessageOneofRule that specifies a list of fields
// of which at most one can be present. If `required` is also specified, then exactly one
// of the specified fields _must_ be present.
//
// This will enforce oneof-like constraints with a few features not provided by
// actual Protobuf oneof declarations:
// 1. Repeated and map fields are allowed in this validation. In a Protobuf oneof,
// only scalar fields are allowed.
// 2. Fields with implicit presence are allowed. In a Protobuf oneof, all member
// fields have explicit presence. This means that, for the purpose of determining
// how many fields are set, explicitly setting such a field to its zero value is
// effectively the same as not setting it at all.
// 3. This will generate validation errors when unmarshalling, even from the binary
// format. With a Protobuf oneof, if multiple fields are present in the serialized
// form, earlier values are usually silently ignored when unmarshalling, with only
// the last field being present when unmarshalling completes.
Comment on lines +153 to +156
Copy link
Member

@jhump jhump Jun 11, 2025

Choose a reason for hiding this comment

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

I know I proposed this copy, but I found some issues in it. Here's a take two 😄

Suggested change
// 3. This will generate validation errors when unmarshalling, even from the binary
// format. With a Protobuf oneof, if multiple fields are present in the serialized
// form, earlier values are usually silently ignored when unmarshalling, with only
// the last field being present when unmarshalling completes.
// 3. This will always generate validation errors for a message unmarshalled from
// serialized data that sets more than one field. With a Protobuf oneof, when
// multiple fields are present in the serialized form, earlier values are usually
// silently ignored when unmarshalling, with only the last field being set when
// unmarshalling completes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah shoot, sorry. Will update this in a follow-up.

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: #378

//
//
// ```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