mwitkow / go-proto-validators

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

UUID validator regex is incorrect #96

Open kostyay opened 4 years ago

kostyay commented 4 years ago

The validator fails for a completely valid UUID 96733dad-7258-716b-22cb-e7963b266675. Google parser parses it correctly, the generated validator does not.

The regex makes several incorrect assumptions, like hat the 3rd block must start with 4, which is not the case.

Code Sample:

package main

import (
    "fmt"
    "regexp"

    "github.com/google/uuid"
)

var _regex_GetApiTokenByClientIdRequest_ClientId = 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 main() {

    guids := []string{"96733dad-7258-716b-22cb-e7963b266675", "89e20584-48b6-4b0b-9ce2-31e3d1125dad"}
    for _, g := range guids {
        _, err := uuid.Parse(g)
        fmt.Printf("v1: %s - %t\n", g, err == nil)
        a := _regex_GetApiTokenByClientIdRequest_ClientId.MatchString(g)
        fmt.Printf("v2: %s - %t\n", g, a)
    }
}