Skip to content
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

Feature/96 validators #100

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 50 additions & 0 deletions gogoproto/gogo.pb.go

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

7 changes: 7 additions & 0 deletions gogoproto/gogo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,12 @@ extend google.protobuf.FieldOptions {
optional string jsontag = 65005;
optional string moretags = 65006;
optional string casttype = 65007;
optional FieldValidator validator = 65008;
}

message FieldValidator {
optional string regex = 1;
optional int64 int_gt = 2;
optional int64 int_lt = 3;
optional bool msg_exists = 4;
Copy link
Member

Choose a reason for hiding this comment

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

maybe just make this required and make it work for all field types that are not repeated, just like required used to work :)

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, you can't do that :( In proto3 the fields are always defaults ("" for string or 0 for int) and you can't differentiate between them being present or not.
The only one you can is submessages.
Also, required is a proto keyword :(

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry about that oversight.
Damn thats true.
This is only possible with the serialized bytes.

}
10 changes: 10 additions & 0 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func GetMoreTags(field *google_protobuf.FieldDescriptorProto) *string {
return nil
}

func GetValidator(field *google_protobuf.FieldDescriptorProto) *FieldValidator {
if field.Options != nil {
v, err := proto.GetExtension(field.Options, E_Validator)
if err == nil && v.(*FieldValidator) != nil {
return (v.(*FieldValidator))
}
}
return nil
}

type EnableFunc func(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool

func EnabledGoEnumPrefix(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool {
Expand Down
Loading