mwitkow / go-proto-validators

Generate message validators from .proto annotations.
Apache License 2.0
1.09k stars 164 forks source link

Feature: UUID validator #41

Closed yaronsumel closed 5 years ago

yaronsumel commented 6 years ago

New validator: UUID

  string user_id = 3 [(validator.field) = {uuid: 4}];

uuid is specifying which uuid version to validate, if 0 is used the accepted version will be 1 to 5

Usage:

message OuterMessage {
  // important_string must be a lowercase alpha-numeric of 5 to 30 characters (RE2 syntax).
  string important_string = 1 [(validator.field) = {regex: "^[a-z]{2,5}$"}];
  // proto3 doesn't have `required`, the `msg_exist` enforces presence of InnerMessage.
  InnerMessage inner = 2 [(validator.field) = {msg_exists : true}];
  // user_id must be valid uuid4
  string user_id = 3 [(validator.field) = {uuid: 4}];
}

generated code:

var _regex_OuterMessage_ImportantString = regexp.MustCompile(`^[a-z]{2,5}$`)
var _regex_OuterMessage_UserId = regexp.MustCompile(`^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[4][a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$`)

func (this *OuterMessage) Validate() error {
    if !_regex_OuterMessage_ImportantString.MatchString(this.ImportantString) {
        return go_proto_validators.FieldError("ImportantString", fmt.Errorf(`value '%v' must be a string conforming to regex "^[a-z]{2,5}$"`, this.ImportantString))
    }
    if nil == this.Inner {
        return go_proto_validators.FieldError("Inner", fmt.Errorf("message must exist"))
    }
    if this.Inner != nil {
        if err := go_proto_validators.CallValidatorIfExists(this.Inner); err != nil {
            return go_proto_validators.FieldError("Inner", err)
        }
    }
    if !_regex_OuterMessage_UserId.MatchString(this.UserId) {
        return go_proto_validators.FieldError("UserId", fmt.Errorf(`value '%v' must be a string conforming to regex "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[4][a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$"`, this.UserId))
    }
    return nil
}
krak3n commented 5 years ago

Is there a reason this hasn't been merged yet? It looks really useful :+1:

fho commented 5 years ago

@mwitkow what do you think about the UUID validator?

I added additional tests for the validator from Yaron and did some minor changes in our https://github.com/simplesurance/go-proto-validators fork. If you would be interested I can put either everything together in a PR or create a subsequent PR after this one got merged.

Helcaraxan commented 5 years ago

Hello @fho,

Putting everything together in a PR would be appreciated as that would directly deliver both the features of this branch and your additional testing in one go.

Again, apologies to everyone that this has been laying fallow for so long. I have only recently gotten access to the repo to pick up the maintenance.

Once that PR is up I will close this one.

fho commented 5 years ago

hello @Helcaraxan,

thanks a lot for reviving the project and picking up maintenance. I'll put everything together in a PR until end of next week.

yaronsumel commented 5 years ago

@Helcaraxan Great .. waiting for the PR @fho ;) !

fho commented 5 years ago

@Helcaraxan there is the PR: https://github.com/mwitkow/go-proto-validators/pull/70

Helcaraxan commented 5 years ago

Closing this in favour of #70.