diff --git a/examples/nested.proto b/examples/nested.proto index 5689c03..cea61d2 100644 --- a/examples/nested.proto +++ b/examples/nested.proto @@ -12,4 +12,4 @@ message OuterMessage { 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}]; -} \ No newline at end of file +} diff --git a/examples/uuid.pb.go b/examples/uuid.pb.go new file mode 100644 index 0000000..175dd07 --- /dev/null +++ b/examples/uuid.pb.go @@ -0,0 +1,81 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: examples/uuid.proto + +package validator_examples + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + _ "github.com/mwitkow/go-proto-validators" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +type UUIDMsg struct { + // user_id must be a valid version 4 UUID. + UserId string `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *UUIDMsg) Reset() { *m = UUIDMsg{} } +func (m *UUIDMsg) String() string { return proto.CompactTextString(m) } +func (*UUIDMsg) ProtoMessage() {} +func (*UUIDMsg) Descriptor() ([]byte, []int) { + return fileDescriptor_0029f00507e892b3, []int{0} +} + +func (m *UUIDMsg) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_UUIDMsg.Unmarshal(m, b) +} +func (m *UUIDMsg) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_UUIDMsg.Marshal(b, m, deterministic) +} +func (m *UUIDMsg) XXX_Merge(src proto.Message) { + xxx_messageInfo_UUIDMsg.Merge(m, src) +} +func (m *UUIDMsg) XXX_Size() int { + return xxx_messageInfo_UUIDMsg.Size(m) +} +func (m *UUIDMsg) XXX_DiscardUnknown() { + xxx_messageInfo_UUIDMsg.DiscardUnknown(m) +} + +var xxx_messageInfo_UUIDMsg proto.InternalMessageInfo + +func (m *UUIDMsg) GetUserId() string { + if m != nil { + return m.UserId + } + return "" +} + +func init() { + proto.RegisterType((*UUIDMsg)(nil), "validator.examples.UUIDMsg") +} + +func init() { proto.RegisterFile("examples/uuid.proto", fileDescriptor_0029f00507e892b3) } + +var fileDescriptor_0029f00507e892b3 = []byte{ + // 144 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4e, 0xad, 0x48, 0xcc, + 0x2d, 0xc8, 0x49, 0x2d, 0xd6, 0x2f, 0x2d, 0xcd, 0x4c, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, + 0x12, 0x2a, 0x4b, 0xcc, 0xc9, 0x4c, 0x49, 0x2c, 0xc9, 0x2f, 0xd2, 0x83, 0x49, 0x4b, 0x99, 0xa5, + 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xe7, 0x96, 0x67, 0x96, 0x64, 0xe7, + 0x97, 0xeb, 0xa7, 0xe7, 0xeb, 0x82, 0x35, 0xe8, 0xc2, 0xd5, 0x17, 0xeb, 0x23, 0xb4, 0x82, 0xa5, + 0x94, 0x74, 0xb9, 0xd8, 0x43, 0x43, 0x3d, 0x5d, 0x7c, 0x8b, 0xd3, 0x85, 0x94, 0xb8, 0xd8, 0x4b, + 0x8b, 0x53, 0x8b, 0xe2, 0x33, 0x53, 0x24, 0x18, 0x15, 0x18, 0x35, 0x38, 0x9d, 0x38, 0x1f, 0xdd, + 0x97, 0x67, 0x8d, 0x60, 0x9c, 0xc0, 0xc8, 0x12, 0xc4, 0x06, 0x92, 0xf1, 0x4c, 0x49, 0x62, 0x03, + 0xeb, 0x32, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xbc, 0x56, 0xba, 0x22, 0x98, 0x00, 0x00, 0x00, +} diff --git a/examples/uuid.proto b/examples/uuid.proto new file mode 100644 index 0000000..05d61ed --- /dev/null +++ b/examples/uuid.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package validator.examples; +import "github.com/mwitkow/go-proto-validators/validator.proto"; + +message UUIDMsg { + // user_id must be a valid version 4 UUID. + string user_id = 1 [(validator.field) = {uuid_ver: 4, string_not_empty: true}]; +} diff --git a/examples/uuid.validator.pb.go b/examples/uuid.validator.pb.go new file mode 100644 index 0000000..b2652af --- /dev/null +++ b/examples/uuid.validator.pb.go @@ -0,0 +1,30 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: examples/uuid.proto + +package validator_examples + +import ( + fmt "fmt" + math "math" + proto "github.com/golang/protobuf/proto" + _ "github.com/mwitkow/go-proto-validators" + regexp "regexp" + github_com_mwitkow_go_proto_validators "github.com/mwitkow/go-proto-validators" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +var _regex_UUIDMsg_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 *UUIDMsg) Validate() error { + if !_regex_UUIDMsg_UserId.MatchString(this.UserId) { + return github_com_mwitkow_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)) + } + if this.UserId == "" { + return github_com_mwitkow_go_proto_validators.FieldError("UserId", fmt.Errorf(`value '%v' must not be an empty string`, this.UserId)) + } + return nil +} diff --git a/plugin/plugin.go b/plugin/plugin.go index 6a7e394..60c20eb 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -57,12 +57,18 @@ import ( "github.com/gogo/protobuf/gogoproto" "github.com/gogo/protobuf/proto" - descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" + "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" "github.com/gogo/protobuf/protoc-gen-gogo/generator" "github.com/gogo/protobuf/vanity" validator "github.com/mwitkow/go-proto-validators" ) +const uuidPattern = "^([a-fA-F0-9]{8}-" + + "[a-fA-F0-9]{4}-" + + "[%s][a-fA-F0-9]{3}-" + + "[8|9|aA|bB][a-fA-F0-9]{3}-" + + "[a-fA-F0-9]{12})?$" + type plugin struct { *generator.Generator generator.PluginImports @@ -154,9 +160,21 @@ func (p *plugin) generateRegexVars(file *generator.FileDescriptor, message *gene ccTypeName := generator.CamelCaseSlice(message.TypeName()) for _, field := range message.Field { validator := getFieldValidatorIfAny(field) - if validator != nil && validator.Regex != nil { + if validator != nil { fieldName := p.GetOneOfFieldName(message, field) - p.P(`var `, p.regexName(ccTypeName, fieldName), ` = `, p.regexPkg.Use(), `.MustCompile(`, "`", *validator.Regex, "`", `)`) + if validator.Regex != nil && validator.UuidVer != nil { + fmt.Fprintf(os.Stderr, "WARNING: regex and uuid validator is set for field %v.%v, only one of them can be set. Regex and UUID validator is ignored for this field.", ccTypeName, fieldName) + } else if validator.UuidVer != nil { + uuid, err := getUUIDRegex(validator.UuidVer) + if err != nil { + fmt.Fprintf(os.Stderr, "WARNING: field %v.%v error %s.\n", ccTypeName, fieldName, err) + } else { + validator.Regex = &uuid + p.P(`var `, p.regexName(ccTypeName, fieldName), ` = `, p.regexPkg.Use(), `.MustCompile(`, "`", *validator.Regex, "`", `)`) + } + } else if validator.Regex != nil { + p.P(`var `, p.regexName(ccTypeName, fieldName), ` = `, p.regexPkg.Use(), `.MustCompile(`, "`", *validator.Regex, "`", `)`) + } } } } @@ -500,8 +518,32 @@ func (p *plugin) generateFloatValidator(variableName string, ccTypeName string, } } +// getUUIDRegex returns a regex to validate that a string is in UUID +// format. The version parameter specified the UUID version. If version is 0, +// the returned regex is valid for any UUID version +func getUUIDRegex(version *int32) (string, error) { + if version == nil { + return "", nil + } else if *version < 0 || *version > 5 { + return "", fmt.Errorf("UUID version should be between 0-5, Got %d", *version) + } else if *version == 0 { + return fmt.Sprintf(uuidPattern, "1-5"), nil + } else { + return fmt.Sprintf(uuidPattern, strconv.Itoa(int(*version))), nil + } +} + func (p *plugin) generateStringValidator(variableName string, ccTypeName string, fieldName string, fv *validator.FieldValidator) { - if fv.Regex != nil { + if fv.Regex != nil || fv.UuidVer != nil { + if fv.UuidVer != nil { + uuid, err := getUUIDRegex(fv.UuidVer) + if err != nil { + fmt.Fprintf(os.Stderr, "WARNING: field %v.%v error %s.\n", ccTypeName, fieldName, err) + } else { + fv.Regex = &uuid + } + } + p.P(`if !`, p.regexName(ccTypeName, fieldName), `.MatchString(`, variableName, `) {`) p.In() errorStr := "be a string conforming to regex " + strconv.Quote(fv.GetRegex()) diff --git a/test/gogo/validator_test.go b/test/gogo/validator_test.go index e556665..25b8306 100644 --- a/test/gogo/validator_test.go +++ b/test/gogo/validator_test.go @@ -4,6 +4,7 @@ package validatortest import ( + fmt "fmt" "strings" "testing" @@ -14,8 +15,16 @@ var ( stableBytes = make([]byte, 12) ) -func buildProto3(someString string, someInt uint32, identifier string, someValue int64, someDoubleStrict float64, - someFloatStrict float32, someDouble float64, someFloat float32, nonEmptyString string, repeatedCount uint32, someStringLength string, someBytes []byte) *ValidatorMessage3 { +const ( + uuid4 = "fbe91ff5-fee7-40d3-89a8-f3db6cf210be" + uuid1 = "66bb25e2-2e0d-11e9-b210-d663bd873d93" +) + +func buildProto3(someString string, someInt uint32, identifier string, + someValue int64, someDoubleStrict float64, someFloatStrict float32, someDouble float64, + someFloat float32, nonEmptyString string, repeatedCount uint32, + someStringLength string, someBytes []byte, + optionalUUIDAny, uuid4 string) *ValidatorMessage3 { goodEmbeddedProto3 := &ValidatorMessage3_Embedded{ Identifier: identifier, SomeValue: someValue, @@ -60,6 +69,9 @@ func buildProto3(someString string, someInt uint32, identifier string, someValue SomeBytesGtReq: someBytes, SomeBytesEqReq: someBytes, RepeatedBaseType: []int32{}, + + UUIDAny: optionalUUIDAny, + UUID4NotEmpty: uuid4, } goodProto3.Repeated = make([]int32, repeatedCount) @@ -67,8 +79,11 @@ func buildProto3(someString string, someInt uint32, identifier string, someValue return goodProto3 } -func buildProto2(someString string, someInt uint32, identifier string, someValue int64, someDoubleStrict float64, - someFloatStrict float32, someDouble float64, someFloat float32, nonEmptyString string, repeatedCount uint32, someStringLength string, someBytes []byte) *ValidatorMessage { +func buildProto2(someString string, someInt uint32, identifier string, + someValue int64, someDoubleStrict float64, someFloatStrict float32, someDouble float64, + someFloat float32, nonEmptyString string, repeatedCount uint32, + someStringLength string, someBytes []byte, + optionalUUIDAny, uuid5 string) *ValidatorMessage { goodEmbeddedProto2 := &ValidatorMessage_Embedded{ Identifier: &identifier, SomeValue: &someValue, @@ -120,6 +135,9 @@ func buildProto2(someString string, someInt uint32, identifier string, someValue SomeBytesGtReq: someBytes, SomeBytesEqReq: someBytes, RepeatedBaseType: []int32{}, + + UUIDAny: &optionalUUIDAny, + UUID4NotEmpty: &uuid5, } goodProto2.Repeated = make([]int32, repeatedCount) @@ -129,7 +147,7 @@ func buildProto2(someString string, someInt uint32, identifier string, someValue func TestGoodProto3(t *testing.T) { var err error - goodProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + goodProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) err = goodProto3.Validate() if err != nil { t.Fatalf("unexpected fail in validator: %v", err) @@ -138,7 +156,7 @@ func TestGoodProto3(t *testing.T) { func TestGoodProto2(t *testing.T) { var err error - goodProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + goodProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) err = goodProto2.Validate() if err != nil { t.Fatalf("unexpected fail in validator: %v", err) @@ -146,254 +164,254 @@ func TestGoodProto2(t *testing.T) { } func TestStringRegex(t *testing.T) { - tooLong1Proto3 := buildProto3("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong1Proto3 := buildProto3("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if tooLong1Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooLong2Proto3 := buildProto3("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong2Proto3 := buildProto3("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if tooLong2Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooLong1Proto2 := buildProto2("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong1Proto2 := buildProto2("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if tooLong1Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooLong2Proto2 := buildProto2("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong2Proto2 := buildProto2("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if tooLong2Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } } func TestIntLowerBounds(t *testing.T) { - lowerThan10Proto3 := buildProto3("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan10Proto3 := buildProto3("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan10Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan10Proto2 := buildProto2("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan10Proto2 := buildProto2("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan10Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan0Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan0Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } } func TestIntUpperBounds(t *testing.T) { - greaterThan100Proto3 := buildProto3("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan100Proto3 := buildProto3("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan100Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan100Proto2 := buildProto2("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan100Proto2 := buildProto2("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan100Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } } func TestDoubleStrictLowerBounds(t *testing.T) { - lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan035EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan035EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan035EpsilonProto3.Validate() != nil { t.Fatalf("unexpected fail in validator") } - greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan035EpsilonProto2.Validate() != nil { t.Fatalf("unexpected fail in validator") } } func TestDoubleStrictUpperBounds(t *testing.T) { - greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan065EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan065EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan065EpsilonProto3.Validate() != nil { t.Fatalf("unexpected fail in validator") } - lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan065EpsilonProto2.Validate() != nil { t.Fatalf("unexpected fail in validator") } } func TestFloatStrictLowerBounds(t *testing.T) { - lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan035EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan035EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := greaterThan035EpsilonProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := greaterThan035EpsilonProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestFloatStrictUpperBounds(t *testing.T) { - greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan065EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if greaterThan065EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := lowerThan065EpsilonProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := lowerThan065EpsilonProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestDoubleNonStrictLowerBounds(t *testing.T) { - lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan0Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan0Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestDoubleNonStrictUpperBounds(t *testing.T) { - higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes) + higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if higherThan1Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes) + higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if higherThan1Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestFloatNonStrictLowerBounds(t *testing.T) { - lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes) + lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan0Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes) + lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if lowerThan0Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestFloatNonStrictUpperBounds(t *testing.T) { - higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes) + higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if higherThan1Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes) + higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if higherThan1Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestStringNonEmpty(t *testing.T) { - emptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes) + emptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes, uuid1, uuid4) if emptyStringProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - emptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes) + emptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes, uuid1, uuid4) if emptyStringProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - nonEmptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + nonEmptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := nonEmptyStringProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - nonEmptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + nonEmptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := nonEmptyStringProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestRepeatedEltsCount(t *testing.T) { - notEnoughEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes) + notEnoughEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes, uuid1, uuid4) if notEnoughEltsProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - notEnoughEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes) + notEnoughEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes, uuid1, uuid4) if notEnoughEltsProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooManyEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes) + tooManyEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes, uuid1, uuid4) if tooManyEltsProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooManyEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes) + tooManyEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes, uuid1, uuid4) if tooManyEltsProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - validEltsCountProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + validEltsCountProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := validEltsCountProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - validEltsCountProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + validEltsCountProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) if err := validEltsCountProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestMsgExist(t *testing.T) { - someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) someProto3.SomeEmbedded = nil if err := someProto3.Validate(); err != nil { t.Fatalf("validate shouldn't fail on missing SomeEmbedded, not annotated") @@ -407,7 +425,7 @@ func TestMsgExist(t *testing.T) { } func TestNestedError3(t *testing.T) { - someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) someProto3.SomeEmbeddedExists.SomeValue = 101 // should be less than 101 if err := someProto3.Validate(); err == nil { t.Fatalf("expected fail due to nested SomeEmbeddedExists.SomeValue being wrong") @@ -417,7 +435,7 @@ func TestNestedError3(t *testing.T) { } func TestCustomError_Proto3(t *testing.T) { - someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) someProto3.CustomErrorInt = 30 expectedErr := "invalid field CustomErrorInt: My Custom Error" if err := someProto3.Validate(); err == nil { @@ -516,3 +534,58 @@ func TestOneOf_Regex(t *testing.T) { err = example.Validate() assert.NoError(t, err, "This message should pass all validation") } + +func TestUUID4Validation(t *testing.T) { + testcases := []struct { + uuid string + fail bool + }{ + { + uuid: uuid1, + fail: true, + }, + + { + uuid: uuid4, + fail: false, + }, + + { + uuid: "", + fail: true, + }, + + { + uuid: "66bb25e2-2e0d", + fail: true, + }, + + { + uuid: "1234abcd", + fail: true, + }, + } + + for _, tc := range testcases { + t.Run(fmt.Sprintf("proto2 uuid '%s'", tc.uuid), func(t *testing.T) { + msg := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, tc.uuid) + + err := msg.Validate() + failed := err != nil + if tc.fail != failed { + t.Errorf("Expected validation failure: %t, but got %t, err: %v", tc.fail, failed, err) + } + }) + + t.Run(fmt.Sprintf("proto3 uuid '%s'", tc.uuid), func(t *testing.T) { + msg := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, tc.uuid) + + err := msg.Validate() + failed := err != nil + if tc.fail != failed { + t.Errorf("Expected validation failure: %t, but got %t, err: %v", tc.fail, failed, err) + } + }) + + } +} diff --git a/test/golang/validator_test.go b/test/golang/validator_test.go index d088075..cc14907 100644 --- a/test/golang/validator_test.go +++ b/test/golang/validator_test.go @@ -4,6 +4,7 @@ package validatortest import ( + fmt "fmt" "strings" "testing" @@ -14,9 +15,15 @@ var ( stableBytes = make([]byte, 12) ) +const ( + uuid4 = "fbe91ff5-fee7-40d3-89a8-f3db6cf210be" + uuid1 = "66bb25e2-2e0d-11e9-b210-d663bd873d93" +) + func buildProto3(someString string, someInt uint32, identifier string, someValue int64, someDoubleStrict float64, someFloatStrict float32, someDouble float64, someFloat float32, nonEmptyString string, repeatedCount uint32, - someStringLength string, someBytes []byte) *ValidatorMessage3 { + someStringLength string, someBytes []byte, + optionalUUIDAny, uuid4 string) *ValidatorMessage3 { goodEmbeddedProto3 := &ValidatorMessage3_Embedded{ Identifier: identifier, SomeValue: someValue, @@ -62,6 +69,9 @@ func buildProto3(someString string, someInt uint32, identifier string, someValue SomeBytesEqReq: someBytes, RepeatedBaseType: []int32{}, + + UUIDAny: optionalUUIDAny, + UUID4NotEmpty: uuid4, } goodProto3.Repeated = make([]int32, repeatedCount) @@ -69,7 +79,11 @@ func buildProto3(someString string, someInt uint32, identifier string, someValue return goodProto3 } -func buildProto2(someString string, someInt uint32, identifier string, someValue int64, someDoubleStrict float64, someFloatStrict float32, someDouble float64, someFloat float32, nonEmptyString string, repeatedCount uint32, someStringLength string, someBytes []byte) *ValidatorMessage { +func buildProto2(someString string, someInt uint32, identifier string, + someValue int64, someDoubleStrict float64, someFloatStrict float32, someDouble float64, + someFloat float32, nonEmptyString string, repeatedCount uint32, + someStringLength string, someBytes []byte, + optionalUUIDAny, uuid5 string) *ValidatorMessage { goodEmbeddedProto2 := &ValidatorMessage_Embedded{ Identifier: &identifier, SomeValue: &someValue, @@ -120,6 +134,9 @@ func buildProto2(someString string, someInt uint32, identifier string, someValue SomeBytesGtReq: someBytes, SomeBytesEqReq: someBytes, RepeatedBaseType: []int32{}, + + UUIDAny: &optionalUUIDAny, + UUID4NotEmpty: &uuid5, } goodProto2.Repeated = make([]int32, repeatedCount) @@ -129,7 +146,7 @@ func buildProto2(someString string, someInt uint32, identifier string, someValue func TestGoodProto3(t *testing.T) { var err error - goodProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + goodProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) err = goodProto3.Validate() if err != nil { t.Fatalf("unexpected fail in validator: %v", err) @@ -138,7 +155,8 @@ func TestGoodProto3(t *testing.T) { func TestGoodProto2(t *testing.T) { var err error - goodProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + goodProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + err = goodProto2.Validate() if err != nil { t.Fatalf("unexpected fail in validator: %v", err) @@ -146,254 +164,307 @@ func TestGoodProto2(t *testing.T) { } func TestStringRegex(t *testing.T) { - tooLong1Proto3 := buildProto3("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong1Proto3 := buildProto3("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if tooLong1Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooLong2Proto3 := buildProto3("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong2Proto3 := buildProto3("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if tooLong2Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooLong1Proto2 := buildProto2("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong1Proto2 := buildProto2("toolong", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if tooLong1Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooLong2Proto2 := buildProto2("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + tooLong2Proto2 := buildProto2("-%ab", 11, "bad#", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if tooLong2Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } } func TestIntLowerBounds(t *testing.T) { - lowerThan10Proto3 := buildProto3("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan10Proto3 := buildProto3("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan10Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan10Proto2 := buildProto2("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan10Proto2 := buildProto2("-%ab", 9, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan10Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan0Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", -1, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan0Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } } func TestIntUpperBounds(t *testing.T) { - greaterThan100Proto3 := buildProto3("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan100Proto3 := buildProto3("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan100Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan100Proto2 := buildProto2("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan100Proto2 := buildProto2("-%ab", 11, "abba", 101, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan100Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } } func TestDoubleStrictLowerBounds(t *testing.T) { - lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan035EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.3, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan035EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan035EpsilonProto3.Validate() != nil { t.Fatalf("unexpected fail in validator") } - greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.300000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan035EpsilonProto2.Validate() != nil { t.Fatalf("unexpected fail in validator") } } func TestDoubleStrictUpperBounds(t *testing.T) { - greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan065EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.70000000001, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan065EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan065EpsilonProto3.Validate() != nil { t.Fatalf("unexpected fail in validator") } - lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.6999999999, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan065EpsilonProto2.Validate() != nil { t.Fatalf("unexpected fail in validator") } } func TestFloatStrictLowerBounds(t *testing.T) { - lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan035EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.2999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan035EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := greaterThan035EpsilonProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan035EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.3000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := greaterThan035EpsilonProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestFloatStrictUpperBounds(t *testing.T) { - greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan065EpsilonProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + greaterThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.7000001, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if greaterThan065EpsilonProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := lowerThan065EpsilonProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan065EpsilonProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.6999999, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := lowerThan065EpsilonProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestDoubleNonStrictLowerBounds(t *testing.T) { - lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan0Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes) + lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.2499999, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan0Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.25, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestDoubleNonStrictUpperBounds(t *testing.T) { - higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes) + higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if higherThan1Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes) + higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75111111, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if higherThan1Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.75, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestFloatNonStrictLowerBounds(t *testing.T) { - lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes) + lowerThan0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan0Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes) + lowerThan0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.2499999, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if lowerThan0Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.25, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestFloatNonStrictUpperBounds(t *testing.T) { - higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes) + higherThan1Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if higherThan1Proto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes) + higherThan1Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75111111, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if higherThan1Proto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes) + equalTo0Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes) + equalTo0Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.75, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := equalTo0Proto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestStringNonEmpty(t *testing.T) { - emptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes) + emptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes, uuid1, uuid4) + if emptyStringProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - emptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes) + emptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "", 4, "1234567890", stableBytes, uuid1, uuid4) + if emptyStringProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - nonEmptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + nonEmptyStringProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := nonEmptyStringProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - nonEmptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + nonEmptyStringProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := nonEmptyStringProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestRepeatedEltsCount(t *testing.T) { - notEnoughEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes) + notEnoughEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes, uuid1, uuid4) + if notEnoughEltsProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - notEnoughEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes) + notEnoughEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 1, "1234567890", stableBytes, uuid1, uuid4) + if notEnoughEltsProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooManyEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes) + tooManyEltsProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes, uuid1, uuid4) + if tooManyEltsProto3.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - tooManyEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes) + tooManyEltsProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 14, "1234567890", stableBytes, uuid1, uuid4) + if tooManyEltsProto2.Validate() == nil { t.Fatalf("expected fail in validator, but it didn't happen") } - validEltsCountProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + validEltsCountProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := validEltsCountProto3.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } - validEltsCountProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + validEltsCountProto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := validEltsCountProto2.Validate(); err != nil { t.Fatalf("unexpected fail in validator %v", err) } } func TestMsgExist(t *testing.T) { - someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + someProto3.SomeEmbedded = nil if err := someProto3.Validate(); err != nil { t.Fatalf("validate shouldn't fail on missing SomeEmbedded, not annotated") @@ -407,31 +478,36 @@ func TestMsgExist(t *testing.T) { } func TestStringLengthValidator(t *testing.T) { - StringLengthErrorProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "abc456", stableBytes) + StringLengthErrorProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "abc456", stableBytes, uuid1, uuid4) + if err := StringLengthErrorProto3.Validate(); err == nil { t.Fatalf("validate shouldn't fail on error length") } - StringLengthSuccess := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + StringLengthSuccess := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := StringLengthSuccess.Validate(); err != nil { t.Fatalf("validate shouldn't fail on equal length") } } func TestBytesLengthValidator(t *testing.T) { - StringLengthErrorProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "abc456", []byte("anc")) + StringLengthErrorProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "abc456", []byte("anc"), uuid1, uuid4) + if err := StringLengthErrorProto3.Validate(); err == nil { t.Fatalf("validate shouldn't fail on error length") } - StringLengthSuccess := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + StringLengthSuccess := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + if err := StringLengthSuccess.Validate(); err != nil { t.Fatalf("validate shouldn't fail on equal length") } } func TestNestedError3(t *testing.T) { - someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + someProto3.SomeEmbeddedExists.SomeValue = 101 // should be less than 101 if err := someProto3.Validate(); err == nil { t.Fatalf("expected fail due to nested SomeEmbeddedNonNullable.SomeValue being wrong") @@ -441,7 +517,8 @@ func TestNestedError3(t *testing.T) { } func TestCustomError_Proto3(t *testing.T) { - someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes) + someProto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, uuid4) + someProto3.CustomErrorInt = 30 expectedErr := "invalid field CustomErrorInt: My Custom Error" if err := someProto3.Validate(); err == nil { @@ -540,3 +617,58 @@ func TestOneOf_Regex(t *testing.T) { err = example.Validate() assert.NoError(t, err, "This message should pass all validation") } + +func TestUUID4Validation(t *testing.T) { + testcases := []struct { + uuid string + fail bool + }{ + { + uuid: uuid1, + fail: true, + }, + + { + uuid: uuid4, + fail: false, + }, + + { + uuid: "", + fail: true, + }, + + { + uuid: "66bb25e2-2e0d", + fail: true, + }, + + { + uuid: "1234abcd", + fail: true, + }, + } + + for _, tc := range testcases { + t.Run(fmt.Sprintf("proto2 uuid '%s'", tc.uuid), func(t *testing.T) { + msg := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, tc.uuid) + + err := msg.Validate() + failed := err != nil + if tc.fail != failed { + t.Errorf("Expected validation failure: %t, but got %t, err: %v", tc.fail, failed, err) + } + }) + + t.Run(fmt.Sprintf("proto3 uuid '%s'", tc.uuid), func(t *testing.T) { + msg := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4, "1234567890", stableBytes, uuid1, tc.uuid) + + err := msg.Validate() + failed := err != nil + if tc.fail != failed { + t.Errorf("Expected validation failure: %t, but got %t, err: %v", tc.fail, failed, err) + } + }) + + } +} diff --git a/test/validator_proto2.proto b/test/validator_proto2.proto index e0e95ae..95fab52 100644 --- a/test/validator_proto2.proto +++ b/test/validator_proto2.proto @@ -70,9 +70,11 @@ message ValidatorMessage { optional string SomeStringLtReq = 36 [(validator.field) = {length_gt: 2}]; optional string SomeStringGtReq = 37 [(validator.field) = {length_lt: 12}]; - optional string SomeStringEqReq = 38 [(validator.field) = {length_eq: 10}]; + optional string SomeStringEqReq = 38 [(validator.field) = {length_eq: 10}]; optional bytes SomeBytesLtReq = 39 [(validator.field) = {length_gt: 5}]; optional bytes SomeBytesGtReq = 40 [(validator.field) = {length_lt: 20}]; optional bytes SomeBytesEqReq = 41 [(validator.field) = {length_eq: 12}]; + optional string UUIDAny = 42 [(validator.field) = {uuid_ver: 0}]; + required string UUID4NotEmpty = 43 [(validator.field) = {uuid_ver: 4, string_not_empty: true}]; } diff --git a/test/validator_proto3.proto b/test/validator_proto3.proto index dd0b9dd..0257d45 100644 --- a/test/validator_proto3.proto +++ b/test/validator_proto3.proto @@ -69,4 +69,7 @@ message ValidatorMessage3 { bytes SomeBytesLtReq = 39 [(validator.field) = {length_gt: 5}]; bytes SomeBytesGtReq = 40 [(validator.field) = {length_lt: 20}]; bytes SomeBytesEqReq = 41 [(validator.field) = {length_eq: 12}]; + + string UUIDAny = 42 [(validator.field) = {uuid_ver: 0}]; + string UUID4NotEmpty = 43 [(validator.field) = {uuid_ver: 4, string_not_empty: true}]; } diff --git a/validator.pb.go b/validator.pb.go index 28aa675..b2befcf 100644 --- a/validator.pb.go +++ b/validator.pb.go @@ -65,7 +65,11 @@ type FieldValidator struct { // Field value of integer strictly equal this value. LengthEq *int64 `protobuf:"varint,16,opt,name=length_eq,json=lengthEq" json:"length_eq,omitempty"` // Requires that the value is in the enum. - IsInEnum *bool `protobuf:"varint,17,opt,name=is_in_enum,json=isInEnum" json:"is_in_enum,omitempty"` + IsInEnum *bool `protobuf:"varint,17,opt,name=is_in_enum,json=isInEnum" json:"is_in_enum,omitempty"` + // Ensures that a string value is in UUID format. + // uuid_ver specifies the valid UUID versions. Valid values are: 0-5. + // If uuid_ver is 0 all UUID versions are accepted. + UuidVer *int32 `protobuf:"varint,18,opt,name=uuid_ver,json=uuidVer" json:"uuid_ver,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -214,6 +218,13 @@ func (m *FieldValidator) GetIsInEnum() bool { return false } +func (m *FieldValidator) GetUuidVer() int32 { + if m != nil && m.UuidVer != nil { + return *m.UuidVer + } + return 0 +} + type OneofValidator struct { // Require that one of the oneof fields is set. Required *bool `protobuf:"varint,1,opt,name=required" json:"required,omitempty"` @@ -281,34 +292,35 @@ func init() { func init() { proto.RegisterFile("validator.proto", fileDescriptor_bf1c6ec7c0d80dd5) } var fileDescriptor_bf1c6ec7c0d80dd5 = []byte{ - // 454 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x93, 0xcf, 0x6b, 0xd4, 0x40, - 0x14, 0xc7, 0x89, 0xed, 0xb6, 0xc9, 0xdb, 0x76, 0x5b, 0x07, 0x85, 0x69, 0xb5, 0x18, 0xea, 0x25, - 0x87, 0x92, 0x82, 0x47, 0x8f, 0x4a, 0x5c, 0x84, 0xd5, 0x4a, 0x0e, 0x1e, 0xbc, 0x84, 0xd8, 0xbc, - 0x4d, 0x07, 0x26, 0x33, 0xd9, 0x99, 0x17, 0x59, 0xff, 0x1f, 0xff, 0x4b, 0x15, 0x64, 0x26, 0x66, - 0x93, 0xc2, 0x1e, 0xe7, 0xf3, 0x79, 0xf9, 0xce, 0x0f, 0xbe, 0x81, 0xb3, 0x1f, 0xa5, 0x14, 0x55, - 0x49, 0xda, 0xa4, 0xad, 0xd1, 0xa4, 0x59, 0xb4, 0x03, 0x97, 0x71, 0xad, 0x75, 0x2d, 0xf1, 0xd6, - 0x8b, 0xef, 0xdd, 0xfa, 0xb6, 0x42, 0x7b, 0x6f, 0x44, 0xbb, 0x1b, 0xbe, 0xfe, 0x75, 0x08, 0x8b, - 0x0f, 0x02, 0x65, 0xf5, 0x75, 0xf8, 0x88, 0x3d, 0x83, 0x99, 0xc1, 0x1a, 0xb7, 0x3c, 0x88, 0x83, - 0x24, 0xca, 0xfb, 0x05, 0x7b, 0x0e, 0x47, 0x42, 0x51, 0x51, 0x13, 0x7f, 0x12, 0x07, 0xc9, 0x41, - 0x3e, 0x13, 0x8a, 0x96, 0x34, 0x60, 0x49, 0xfc, 0x60, 0x87, 0x57, 0xc4, 0xae, 0x00, 0x1a, 0x5b, - 0x17, 0xb8, 0x15, 0x96, 0x2c, 0x3f, 0x8c, 0x83, 0x24, 0xcc, 0xa3, 0xc6, 0xd6, 0x99, 0x07, 0xec, - 0x15, 0xcc, 0x1f, 0xba, 0xa6, 0x54, 0x05, 0x1a, 0xa3, 0x0d, 0x9f, 0xf9, 0x8d, 0xc0, 0xa3, 0xcc, - 0x11, 0x76, 0x01, 0xe1, 0x5a, 0xea, 0xd2, 0xef, 0x77, 0x14, 0x07, 0x49, 0x90, 0x1f, 0xfb, 0xf5, - 0x92, 0x46, 0x25, 0x89, 0x1f, 0x4f, 0xd4, 0x8a, 0xd8, 0x6b, 0x38, 0xed, 0x15, 0xb6, 0x56, 0x48, - 0xad, 0x78, 0xe8, 0xfd, 0x89, 0x87, 0x59, 0xcf, 0xd8, 0x0b, 0x88, 0x86, 0x68, 0xe4, 0x91, 0x1f, - 0x08, 0xff, 0x67, 0xe3, 0x28, 0x25, 0x21, 0x87, 0x89, 0x5c, 0x11, 0xb2, 0x04, 0xce, 0x2d, 0x19, - 0xa1, 0xea, 0x42, 0x69, 0x2a, 0xb0, 0x69, 0xe9, 0x27, 0x9f, 0xfb, 0xab, 0x2d, 0x7a, 0xfe, 0x59, - 0x53, 0xe6, 0x28, 0xbb, 0x01, 0x66, 0xb0, 0xc5, 0x92, 0xb0, 0x2a, 0xee, 0x75, 0xa7, 0xa8, 0x68, - 0x84, 0xe2, 0x27, 0xfe, 0x85, 0xce, 0x07, 0xf3, 0xde, 0x89, 0x4f, 0x42, 0xed, 0x9b, 0x2e, 0xb7, - 0xfc, 0x74, 0xdf, 0x74, 0xb9, 0x75, 0x47, 0x94, 0xa8, 0x6a, 0x7a, 0x70, 0x6f, 0xb3, 0xf0, 0x43, - 0x61, 0x0f, 0x96, 0x34, 0x91, 0x92, 0xf8, 0xd9, 0x54, 0xae, 0xa6, 0x12, 0x37, 0xfc, 0x7c, 0x2a, - 0xb3, 0x0d, 0x7b, 0x09, 0x20, 0x6c, 0x21, 0x54, 0x81, 0xaa, 0x6b, 0xf8, 0x53, 0x7f, 0xad, 0x50, - 0xd8, 0x8f, 0x2a, 0x53, 0x5d, 0x73, 0x7d, 0x03, 0x8b, 0x3b, 0x85, 0x7a, 0x3d, 0xb6, 0xe4, 0x12, - 0x42, 0x83, 0x9b, 0x4e, 0x18, 0xac, 0x7c, 0x51, 0xc2, 0x7c, 0xb7, 0x7e, 0xfb, 0x05, 0x66, 0x6b, - 0xd7, 0x29, 0x76, 0x95, 0xf6, 0x05, 0x4c, 0x87, 0x02, 0xa6, 0xbe, 0x6b, 0x77, 0x2d, 0x09, 0xad, - 0x2c, 0xff, 0xf3, 0xdb, 0x95, 0x66, 0xfe, 0xe6, 0x22, 0x1d, 0x3b, 0xfc, 0xb8, 0x8c, 0x79, 0x1f, - 0xe4, 0x12, 0xb5, 0xdb, 0x7f, 0x4f, 0xa2, 0x3f, 0xd7, 0x90, 0xf8, 0x77, 0x4f, 0xe2, 0xe3, 0x83, - 0xe7, 0x7d, 0xd0, 0xbb, 0xf9, 0xb7, 0xf1, 0x3f, 0xf9, 0x17, 0x00, 0x00, 0xff, 0xff, 0x95, 0xab, - 0x34, 0x60, 0x44, 0x03, 0x00, 0x00, + // 472 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x93, 0x41, 0x6f, 0xd4, 0x30, + 0x10, 0x85, 0x15, 0xda, 0xb4, 0x89, 0xb7, 0xdd, 0x2e, 0x16, 0x48, 0x6e, 0xa1, 0x22, 0x2a, 0x97, + 0x1c, 0xaa, 0x54, 0xe2, 0xc8, 0x11, 0x14, 0x56, 0x48, 0x0b, 0x45, 0x39, 0xf4, 0xc0, 0xc5, 0x0a, + 0xcd, 0x6c, 0x6a, 0xc9, 0xb1, 0xb3, 0xf6, 0xa4, 0x5a, 0x7e, 0x1d, 0x3f, 0x0c, 0x90, 0x90, 0x1d, + 0xb2, 0x49, 0xa5, 0x3d, 0xce, 0xfb, 0x26, 0x6f, 0xc6, 0xa3, 0x17, 0x72, 0xf6, 0x58, 0x4a, 0x51, + 0x95, 0xa8, 0x4d, 0xd6, 0x1a, 0x8d, 0x9a, 0xc6, 0x3b, 0xe1, 0x22, 0xa9, 0xb5, 0xae, 0x25, 0xdc, + 0x78, 0xf0, 0xa3, 0x5b, 0xdf, 0x54, 0x60, 0xef, 0x8d, 0x68, 0x77, 0xcd, 0x57, 0xbf, 0x0e, 0xc9, + 0xfc, 0x93, 0x00, 0x59, 0xdd, 0x0d, 0x1f, 0xd1, 0x17, 0x24, 0x34, 0x50, 0xc3, 0x96, 0x05, 0x49, + 0x90, 0xc6, 0x45, 0x5f, 0xd0, 0x97, 0xe4, 0x48, 0x28, 0xe4, 0x35, 0xb2, 0x67, 0x49, 0x90, 0x1e, + 0x14, 0xa1, 0x50, 0xb8, 0xc4, 0x41, 0x96, 0xc8, 0x0e, 0x76, 0xf2, 0x0a, 0xe9, 0x25, 0x21, 0x8d, + 0xad, 0x39, 0x6c, 0x85, 0x45, 0xcb, 0x0e, 0x93, 0x20, 0x8d, 0x8a, 0xb8, 0xb1, 0x75, 0xee, 0x05, + 0xfa, 0x86, 0xcc, 0x1e, 0xba, 0xa6, 0x54, 0x1c, 0x8c, 0xd1, 0x86, 0x85, 0x7e, 0x10, 0xf1, 0x52, + 0xee, 0x14, 0x7a, 0x4e, 0xa2, 0xb5, 0xd4, 0xa5, 0x9f, 0x77, 0x94, 0x04, 0x69, 0x50, 0x1c, 0xfb, + 0x7a, 0x89, 0x23, 0x92, 0xc8, 0x8e, 0x27, 0x68, 0x85, 0xf4, 0x2d, 0x39, 0xed, 0x11, 0xb4, 0x56, + 0x48, 0xad, 0x58, 0xe4, 0xf9, 0x89, 0x17, 0xf3, 0x5e, 0xa3, 0xaf, 0x48, 0x3c, 0x58, 0x03, 0x8b, + 0x7d, 0x43, 0xf4, 0xdf, 0x1b, 0x46, 0x28, 0x11, 0x18, 0x99, 0xc0, 0x15, 0x02, 0x4d, 0xc9, 0xc2, + 0xa2, 0x11, 0xaa, 0xe6, 0x4a, 0x23, 0x87, 0xa6, 0xc5, 0x9f, 0x6c, 0xe6, 0x9f, 0x36, 0xef, 0xf5, + 0xaf, 0x1a, 0x73, 0xa7, 0xd2, 0x6b, 0x42, 0x0d, 0xb4, 0x50, 0x22, 0x54, 0xfc, 0x5e, 0x77, 0x0a, + 0x79, 0x23, 0x14, 0x3b, 0xf1, 0x17, 0x5a, 0x0c, 0xe4, 0xa3, 0x03, 0x5f, 0x84, 0xda, 0xd7, 0x5d, + 0x6e, 0xd9, 0xe9, 0xbe, 0xee, 0x72, 0xeb, 0x56, 0x94, 0xa0, 0x6a, 0x7c, 0x70, 0xb7, 0x99, 0xfb, + 0xa6, 0xa8, 0x17, 0x96, 0x38, 0x81, 0x12, 0xd9, 0xd9, 0x14, 0xae, 0xa6, 0x10, 0x36, 0x6c, 0x31, + 0x85, 0xf9, 0x86, 0xbe, 0x26, 0x44, 0x58, 0x2e, 0x14, 0x07, 0xd5, 0x35, 0xec, 0xb9, 0x7f, 0x56, + 0x24, 0xec, 0x67, 0x95, 0xab, 0xae, 0x71, 0x47, 0xef, 0x3a, 0x51, 0xf1, 0x47, 0x30, 0x8c, 0x26, + 0x41, 0x1a, 0x16, 0xc7, 0xae, 0xbe, 0x03, 0x73, 0x75, 0x4d, 0xe6, 0xb7, 0x0a, 0xf4, 0x7a, 0x0c, + 0xd0, 0x05, 0x89, 0x0c, 0x6c, 0x3a, 0x61, 0xa0, 0xf2, 0x19, 0x8a, 0x8a, 0x5d, 0xfd, 0xfe, 0x1b, + 0x09, 0xd7, 0x2e, 0x6e, 0xf4, 0x32, 0xeb, 0xb3, 0x99, 0x0d, 0xd9, 0xcc, 0x7c, 0x0c, 0x6f, 0x5b, + 0x14, 0x5a, 0x59, 0xf6, 0xe7, 0xb7, 0xcb, 0xd3, 0xec, 0xdd, 0x79, 0x36, 0xc6, 0xfb, 0x69, 0x4e, + 0x8b, 0xde, 0xc8, 0x39, 0x6a, 0x37, 0x7f, 0x8f, 0xa3, 0xdf, 0x6b, 0x70, 0xfc, 0xbb, 0xc7, 0xf1, + 0xe9, 0xe2, 0x45, 0x6f, 0xf4, 0x61, 0xf6, 0x7d, 0xfc, 0x85, 0xfe, 0x05, 0x00, 0x00, 0xff, 0xff, + 0x05, 0x20, 0x26, 0x13, 0x5f, 0x03, 0x00, 0x00, } diff --git a/validator.proto b/validator.proto index 6a82760..40a24e7 100644 --- a/validator.proto +++ b/validator.proto @@ -68,6 +68,10 @@ message FieldValidator { optional int64 length_eq = 16; // Requires that the value is in the enum. optional bool is_in_enum = 17; + // Ensures that a string value is in UUID format. + // uuid_ver specifies the valid UUID versions. Valid values are: 0-5. + // If uuid_ver is 0 all UUID versions are accepted. + optional int32 uuid_ver = 18; } message OneofValidator {