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

add new cases:add int_gte && int_lte cases && regard enum as integer #22

Closed
wants to merge 3 commits 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
75 changes: 56 additions & 19 deletions examples/nested.pb.go

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

2 changes: 2 additions & 0 deletions examples/nested.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "github.com/mwitkow/go-proto-validators/validator.proto";
message InnerMessage {
// some_integer can only be in range (1, 100).
int32 some_integer = 1 [(validator.field) = {int_gt: 0, int_lt: 100}];
int32 type = 2 [(validator.field) = {int_gte: 0, int_lte: 5}];
string content = 3 [(validator.field)= {string_length_gte:3,string_length_lte:15}];
}

message OuterMessage {
Expand Down
22 changes: 17 additions & 5 deletions examples/nested.validator.pb.go

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

83 changes: 63 additions & 20 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func (p *plugin) isSupportedInt(field *descriptor.FieldDescriptorProto) bool {
return true
case descriptor.FieldDescriptorProto_TYPE_SINT32, descriptor.FieldDescriptorProto_TYPE_SINT64:
return true
case descriptor.FieldDescriptorProto_TYPE_ENUM:
return true
}
return false
}
Expand Down Expand Up @@ -326,22 +328,26 @@ func (p *plugin) generateProto3Message(file *generator.FileDescriptor, message *
}

func (p *plugin) generateIntValidator(variableName string, ccTypeName string, fieldName string, fv *validator.FieldValidator) {
if fv.IntGt != nil {
p.P(`if !(`, variableName, ` > `, fv.IntGt, `) {`)
print := func(cmpStr string, errStr string) {
p.P(`if !(`, variableName, cmpStr, `) {`)
p.In()
errorStr := fmt.Sprintf(`be greater than '%d'`, fv.GetIntGt())
p.generateErrorString(variableName, fieldName, errorStr, fv)
p.generateErrorString(variableName, fieldName, errStr, fv)
p.Out()
p.P(`}`)
}
if fv.IntGt != nil {
print(fmt.Sprintf(` > %d`, *fv.IntGt), fmt.Sprintf(`be greater than '%d'`, *fv.IntGt))
}
if fv.IntGte != nil {
print(fmt.Sprintf(` >= %d`, *fv.IntGte), fmt.Sprintf(`be equal or greater than '%d'`, *fv.IntGte))
}
if fv.IntLt != nil {
p.P(`if !(`, variableName, ` < `, fv.IntLt, `) {`)
p.In()
errorStr := fmt.Sprintf(`be less than '%d'`, fv.GetIntLt())
p.generateErrorString(variableName, fieldName, errorStr, fv)
p.Out()
p.P(`}`)
print(fmt.Sprintf(` < %d`, *fv.IntLt), fmt.Sprintf(`be smaller than '%d'`, *fv.IntLt))
}
if fv.IntLte != nil {
print(fmt.Sprintf(` <= %d`, *fv.IntLte), fmt.Sprintf(`be equal or smaller than '%d'`, *fv.IntLte))
}

}

func (p *plugin) generateFloatValidator(variableName string, ccTypeName string, fieldName string, fv *validator.FieldValidator) {
Expand Down Expand Up @@ -425,22 +431,59 @@ func (p *plugin) generateFloatValidator(variableName string, ccTypeName string,
}

func (p *plugin) generateStringValidator(variableName string, ccTypeName string, fieldName string, fv *validator.FieldValidator) {
if fv.Regex != nil {
p.P(`if !`, p.regexName(ccTypeName, fieldName), `.MatchString(`, variableName, `) {`)
print := func(logicOpeLine string, errStr string) {
p.P(logicOpeLine)
p.In()
errorStr := "be a string conforming to regex " + strconv.Quote(fv.GetRegex())
p.generateErrorString(variableName, fieldName, errorStr, fv)
// fmt.Println("err string ::: ", errStr)
p.generateErrorString(variableName, fieldName, errStr, fv)
p.Out()
p.P(`}`)
}
if fv.Regex != nil {
opeLine := `if !` + p.regexName(ccTypeName, fieldName) + `.MatchString(` + variableName + `) {`
print(opeLine, "be a string conforming to regex "+strconv.Quote(fv.GetRegex()))
}
if fv.StringNotEmpty != nil && fv.GetStringNotEmpty() {
p.P(`if `, variableName, ` == "" {`)
p.In()
errorStr := "not be an empty string"
p.generateErrorString(variableName, fieldName, errorStr, fv)
p.Out()
p.P(`}`)
print(`if `+variableName+` == "" {`, "not be an empty string")
}
if fv.StringLengthGt != nil {
print(fmt.Sprintf(`if ! (len(%s) > %d){`, variableName, *fv.StringLengthGt),
fmt.Sprintf("string length greater than %d", *fv.StringLengthGt))
}
if fv.StringLengthGte != nil {
print(fmt.Sprintf(`if ! (len(%s) >= %d){`, variableName, *fv.StringLengthGte),
fmt.Sprintf("string length equal or greater than %d", *fv.StringLengthGte))
}

if fv.StringLengthLt != nil {
print(fmt.Sprintf(`if ! (len(%s) < %d){`, variableName, *fv.StringLengthLt),
fmt.Sprintf("string length smaller than %d", *fv.StringLengthLt))
}

if fv.StringLengthLte != nil {
print(fmt.Sprintf(`if ! (len(%s) <= %d){`, variableName, *fv.StringLengthLte),
fmt.Sprintf("string length equal or smaller than %d", *fv.StringLengthLte))
}

if fv.RuneLengthGt != nil {
print(fmt.Sprintf(`if ! (len([]rune(%s)) > %d){`, variableName, *fv.RuneLengthGt),
fmt.Sprintf("rune length greater than %d", *fv.RuneLengthGt))
}
if fv.RuneLengthGte != nil {
print(fmt.Sprintf(`if ! (len([]rune(%s)) >= %d){`, variableName, *fv.RuneLengthGte),
fmt.Sprintf("rune length equal or greater than %d", *fv.RuneLengthGte))
}

if fv.RuneLengthLt != nil {
print(fmt.Sprintf(`if ! (len([]rune(%s)) < %d){`, variableName, *fv.RuneLengthLt),
fmt.Sprintf("rune length smaller than %d", *fv.RuneLengthLt))
}

if fv.RuneLengthLte != nil {
print(fmt.Sprintf(`if ! (len([]rune(%s)) <= %d){`, variableName, *fv.RuneLengthLte),
fmt.Sprintf("rune length equal or smaller than %d", *fv.RuneLengthLte))
}

}

func (p *plugin) generateRepeatedCountValidator(variableName string, ccTypeName string, fieldName string, fv *validator.FieldValidator) {
Expand Down
85 changes: 71 additions & 14 deletions test/gogo/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ func buildProto3(someString string, someInt uint32, identifier string, someValue
}

goodProto3 := &ValidatorMessage3{
SomeString: someString,
SomeStringRep: []string{someString, "xyz34"},
SomeStringNoQuotes: someString,

SomeInt: someInt,
SomeIntRep: []uint32{someInt, 12, 13, 14, 15, 16},
SomeIntRepNonNull: []uint32{someInt, 102},

SomeString: someString,
SomeStringRep: []string{someString, "xyz34"},
SomeStringNoQuotes: someString,
SomeStringLength: "abc",
SomeStringRuneLength: "您好啊",

SomeInt: someInt,
SomeIntRep: []uint32{someInt, 12, 13, 14, 15, 16},
SomeIntRepNonNull: []uint32{someInt, 102},
SomeIntE: 25,
SomeEmbedded: nil,
SomeEmbeddedNonNullable: *goodEmbeddedProto3,
SomeEmbeddedExists: goodEmbeddedProto3,
Expand Down Expand Up @@ -60,19 +62,20 @@ func buildProto2(someString string, someInt uint32, identifier string, someValue
Identifier: &identifier,
SomeValue: &someValue,
}

someIntE := new(uint32)
*someIntE = 25
goodProto2 := &ValidatorMessage{
StringReq: &someString,
StringReqNonNull: someString,

StringOpt: nil,
StringOptNonNull: someString,

IntReq: &someInt,
IntReqNonNull: someInt,
IntRep: []uint32{someInt, 12, 13, 14, 15, 16},
IntRepNonNull: []uint32{someInt, 12, 13, 14, 15, 16},

IntReq: &someInt,
IntReqNonNull: someInt,
IntRep: []uint32{someInt, 12, 13, 14, 15, 16},
IntRepNonNull: []uint32{someInt, 12, 13, 14, 15, 16},
SomeIntE: someIntE,
EmbeddedReq: goodEmbeddedProto2,
EmbeddedNonNull: *goodEmbeddedProto2,
EmbeddedRep: []*ValidatorMessage_Embedded{goodEmbeddedProto2},
Expand Down Expand Up @@ -465,3 +468,57 @@ func TestOneOf_Passes(t *testing.T) {
err := example.Validate()
assert.NoError(t, err, "This message should pass all validation")
}

func TestIntStrictUpperLowerBounds(t *testing.T) {
strictBounds20_30Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4)
strictBounds20_30Proto3.SomeIntE = 31
assert.NotNil(t, strictBounds20_30Proto3.Validate())
strictBounds20_30Proto3.SomeIntE = 30
assert.Empty(t, strictBounds20_30Proto3.Validate())

strictBounds20_30Proto3.SomeIntE = 20
assert.Empty(t, strictBounds20_30Proto3.Validate())
strictBounds20_30Proto3.SomeIntE = 19
assert.NotNil(t, strictBounds20_30Proto3.Validate())

strictBounds20_30Proto2 := buildProto2("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4)
strictBounds20_30Proto2.SomeIntE = puint32(31)
assert.NotNil(t, strictBounds20_30Proto2.Validate())
strictBounds20_30Proto2.SomeIntE = puint32(30)
assert.Empty(t, strictBounds20_30Proto2.Validate())

strictBounds20_30Proto2.SomeIntE = puint32(20)
assert.Empty(t, strictBounds20_30Proto2.Validate())
strictBounds20_30Proto2.SomeIntE = puint32(19)
assert.NotNil(t, strictBounds20_30Proto2.Validate())

}

func puint32(i uint32) *uint32 {
p := new(uint32)
*p = uint32(i)
return p
}

func TestCharacterLength(t *testing.T) {
characterLength2_6Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4)
characterLength2_6Proto3.SomeStringLength = "您" // 3 length
assert.Empty(t, characterLength2_6Proto3.Validate())
characterLength2_6Proto3.SomeStringLength = "1"
assert.NotNil(t, characterLength2_6Proto3.Validate())
characterLength2_6Proto3.SomeStringLength = "abcdef"
assert.Empty(t, characterLength2_6Proto3.Validate())
characterLength2_6Proto3.SomeStringLength = "abcdefg"
assert.NotNil(t, characterLength2_6Proto3.Validate())

runeLength2_6Proto3 := buildProto3("-%ab", 11, "abba", 99, 0.5, 0.5, 0.5, 0.5, "x", 4)
runeLength2_6Proto3.SomeStringRuneLength = "您好" // 2 rune
assert.Empty(t, runeLength2_6Proto3.Validate())
runeLength2_6Proto3.SomeStringRuneLength = "您"
assert.NotNil(t, runeLength2_6Proto3.Validate())
runeLength2_6Proto3.SomeStringRuneLength = "吃了嘛您呐."
assert.Empty(t, runeLength2_6Proto3.Validate())
runeLength2_6Proto3.SomeStringRuneLength = "得嘞北京欢迎您"
assert.NotNil(t, runeLength2_6Proto3.Validate())

}
Loading