Skip to content

Commit

Permalink
Add example options for most types (#239)
Browse files Browse the repository at this point in the history
This PR adds an example field for many types. Examples are no-op
constraints that allow you to declare what an example value for each
field may look like. These values SHOULD match the other constraints.
It's planned that a `buf lint` rule will check these values to see if
they do, in fact, validate given the other constraints on the field.
Here are the types that this was added to:
- float
- double
- int32
- int64
- uint32
- uint64
- sint32
- sint64
- fixed32
- fixed64
- sfixed32
- sfixed64
- bool
- string
- bytes
- enum
- duration
- timestamp

All fields are `repeatable`, to allow for multiple examples. I'm unsure
if there is a better way to have a no-op constraint other than
`expression: "true"`, so please let me know if I can just exclude
`expression` altogether or if there is another way to do that. This
resolves #237.

---------

Co-authored-by: Chris Roche <[email protected]>
  • Loading branch information
sudorandom and rodaine authored Aug 21, 2024
1 parent d0a45fa commit eecae2a
Show file tree
Hide file tree
Showing 34 changed files with 6,052 additions and 3,998 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ message BoolConstTrue {
message BoolConstFalse {
bool val = 1 [(buf.validate.field).bool.const = false];
}
message BoolExample {
bool val = 1 [(buf.validate.field).bool.example = true];
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ message BytesIPv6Ignore {
(buf.validate.field).ignore_empty = true
];
}
message BytesExample {
bytes val = 1 [(buf.validate.field).bytes.example = "\x99"];
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ message EnumInsideOneof {
}];
}
}

message EnumExample {
TestEnum val = 1 [(buf.validate.field).enum.example = 2];
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ message FloatIncorrectType {
float val = 1 [(buf.validate.field).double.gt = 0];
}

message FloatExample {
float val = 1 [(buf.validate.field).float.example = 8];
}

message DoubleNone {
double val = 1;
}
Expand Down Expand Up @@ -162,6 +166,10 @@ message DoubleIncorrectType {
double val = 1 [(buf.validate.field).float.gt = 0];
}

message DoubleExample {
double val = 1 [(buf.validate.field).double.example = 0];
}

message Int32None {
int32 val = 1;
}
Expand Down Expand Up @@ -231,6 +239,10 @@ message Int32IncorrectType {
int32 val = 1 [(buf.validate.field).float.gt = 0];
}

message Int32Example {
int32 val = 1 [(buf.validate.field).int32.example = 10];
}

message Int64None {
int64 val = 1;
}
Expand Down Expand Up @@ -325,6 +337,10 @@ message Int64IncorrectType {
int64 val = 1 [(buf.validate.field).float.gt = 0];
}

message Int64Example {
int64 val = 1 [(buf.validate.field).float.example = 10];
}

message UInt32None {
uint32 val = 1;
}
Expand Down Expand Up @@ -394,6 +410,10 @@ message UInt32IncorrectType {
uint32 val = 1 [(buf.validate.field).float.gt = 0];
}

message UInt32Example {
uint32 val = 1 [(buf.validate.field).uint32.example = 0];
}

message UInt64None {
uint64 val = 1;
}
Expand Down Expand Up @@ -463,6 +483,10 @@ message UInt64IncorrectType {
uint64 val = 1 [(buf.validate.field).float.gt = 0];
}

message UInt64Example {
uint64 val = 1 [(buf.validate.field).uint64.example = 0];
}

message SInt32None {
sint32 val = 1;
}
Expand Down Expand Up @@ -532,6 +556,10 @@ message SInt32IncorrectType {
sint32 val = 1 [(buf.validate.field).float.gt = 0];
}

message SInt32Example {
sint32 val = 1 [(buf.validate.field).int32.example = 0];
}

message SInt64None {
sint64 val = 1;
}
Expand Down Expand Up @@ -600,6 +628,10 @@ message SInt64IncorrectType {
sint64 val = 1 [(buf.validate.field).float.gt = 0];
}

message SInt64Example {
sint64 val = 1 [(buf.validate.field).sint64.example = 0];
}

message Fixed32None {
fixed32 val = 1;
}
Expand Down Expand Up @@ -669,6 +701,10 @@ message Fixed32IncorrectType {
fixed32 val = 1 [(buf.validate.field).float.gt = 0];
}

message Fixed32Example {
fixed32 val = 1 [(buf.validate.field).fixed32.example = 0];
}

message Fixed64None {
fixed64 val = 1;
}
Expand Down Expand Up @@ -738,6 +774,10 @@ message Fixed64IncorrectType {
fixed64 val = 1 [(buf.validate.field).float.gt = 0];
}

message Fixed64Example {
fixed64 val = 1 [(buf.validate.field).fixed64.example = 0];
}

message SFixed32None {
sfixed32 val = 1;
}
Expand Down Expand Up @@ -807,6 +847,10 @@ message SFixed32IncorrectType {
sfixed32 val = 1 [(buf.validate.field).float.gt = 0];
}

message SFixed32Example {
sfixed32 val = 1 [(buf.validate.field).sfixed32.example = 0];
}

message SFixed64None {
sfixed64 val = 1;
}
Expand Down Expand Up @@ -876,6 +920,10 @@ message SFixed64IncorrectType {
sfixed64 val = 1 [(buf.validate.field).float.gt = 0];
}

message SFixed64Example {
sfixed64 val = 1 [(buf.validate.field).sfixed64.example = 0];
}

message Int64LTEOptional {
optional int64 val = 1 [(buf.validate.field).int64.lte = 64];
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,7 @@ message StringHostAndOptionalPort {
expression: "this.isHostAndPort(false)"
}];
}

message StringExample {
string val = 1 [(buf.validate.field).string.example = "foo"];
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ message DurationFieldWithOtherFields {
google.protobuf.Duration duration_val = 1 [(buf.validate.field).duration.lte = {seconds: 1}];
int32 int_val = 2 [(buf.validate.field).int32.gt = 16];
}

message DurationExample {
google.protobuf.Duration val = 1 [(buf.validate.field).duration.example = {seconds: 3}];
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ message TimestampGTNowWithin {
within: {seconds: 3600}
}];
}

message TimestampExample {
google.protobuf.Timestamp val = 1 [(buf.validate.field).timestamp.example = {seconds: 3}];
}
Loading

0 comments on commit eecae2a

Please sign in to comment.