Skip to content

Commit

Permalink
Lots of tweaks for whitespace cleanup.
Browse files Browse the repository at this point in the history
Added a "Contains" check for strings and string arrays.  Maybe support maps and other array types (like []int) in the future.
  • Loading branch information
abice committed Nov 16, 2016
1 parent 528330c commit 8bc4bc7
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 215 deletions.
75 changes: 49 additions & 26 deletions generator/assets.go

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions internal/benchmark/types_test_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ func (s SingleString) Validate() error {

// BEGIN Entry Validations
// required

if s.Entry == "" {
return append(vErrors, gencheck.NewFieldError("SingleString", "Entry", "required", errors.New("is required")))
}

// END Entry Validations

return nil
Expand All @@ -34,38 +32,30 @@ func (s TestString) Validate() error {

// BEGIN Required Validations
// required

if s.Required == "" {
return append(vErrors, gencheck.NewFieldError("TestString", "Required", "required", errors.New("is required")))
}

// END Required Validations

// BEGIN Len Validations
// len

if !(len(s.Len) == 10) {
return append(vErrors, gencheck.NewFieldError("TestString", "Len", "len", errors.New("length mismatch")))
}

// END Len Validations

// BEGIN Min Validations
// min

if len(s.Min) < 5 {
return append(vErrors, gencheck.NewFieldError("TestString", "Min", "min", errors.New("length failed check for min=5")))
}

// END Min Validations

// BEGIN Max Validations
// max

if len(s.Max) > 100 {
return append(vErrors, gencheck.NewFieldError("TestString", "Max", "max", errors.New("length failed check for max=100")))
}

// END Max Validations

return nil
Expand Down
2 changes: 2 additions & 0 deletions internal/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type Test struct {
URI string `valid:"uri"`
Base64 string `valid:"base64"`
Contains string `valid:"contains=purpose"`
ContainsPtr *string `valid:"contains=purpose"`
ContainsArray []string `valid:"contains=nonsense"`
ContainsAny string `valid:"containsany=!@#$"`
Excludes string `valid:"excludes=text"`
ExcludesAll string `valid:"excludesall=!@#$"`
Expand Down
11 changes: 11 additions & 0 deletions internal/example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ func (s *ExampleTestSuite) TestValidateTestStruct_NoValues() {
gencheck.NewFieldError("Test", "GteTime", "gte", fmt.Errorf("is before now")),
gencheck.NewFieldError("Test", "GteTimeVal", "gte", fmt.Errorf("is before %s", time.Now().UTC().Add(1*time.Second).Truncate(time.Second).Format("2006-01-02 15:04:05"))),
gencheck.NewFieldError("Test", "GteTimePtr", "gte", fmt.Errorf("is before now")),
gencheck.NewFieldError("Test", "Contains", "contains", fmt.Errorf("Contains did not contain purpose")),
gencheck.NewFieldError("Test", "ContainsPtr", "contains", fmt.Errorf("ContainsPtr did not contain purpose")),
gencheck.NewFieldError("Test", "ContainsArray", "contains", fmt.Errorf("ContainsArray did not contain nonsense")),
gencheck.NewFieldError("Test", "UUID", "uuid", fmt.Errorf("'' is not a UUID")),
gencheck.NewFieldError("Test", "MinIntPtr", "required", fmt.Errorf("is required")),
}
testTime := time.Now().UTC()
notPurpose := "notPurpose"
underTest := Test{
RequiredString: "Here I am", // Put in required string to prevent fast failure
MaxString: "1234",
Expand All @@ -91,6 +95,7 @@ func (s *ExampleTestSuite) TestValidateTestStruct_NoValues() {
LteNumber: 5.56000001,
LteMultiple: []string{"", "", ""},
GteTimePtr: &testTime,
ContainsPtr: &notPurpose,
}

err := underTest.Validate()
Expand Down Expand Up @@ -130,6 +135,8 @@ func (s *ExampleTestSuite) TestValidateTestStruct_Values() {
GtNumber: 5.5600001,
GtMultiple: []string{"", "", ""},
GtString: "1234",
Contains: "purpose Of this test",
ContainsArray: []string{"test", "last", "purpose", "nonsense"},
}

err := underTest.Validate()
Expand Down Expand Up @@ -161,6 +168,8 @@ func (s *ExampleTestSuite) TestValidateTestStruct_MinPtrFailure() {
GtMultiple: []string{"", "", ""},
GtString: "1234",
MinIntPtr: &i,
Contains: "purpose Of this test",
ContainsArray: []string{"test", "last", "purpose", "nonsense"},
}

err := underTest.Validate()
Expand Down Expand Up @@ -220,6 +229,8 @@ func (s *ExampleTestSuite) TestValidateTestStruct_LteTime() {
GtMultiple: []string{"", "", ""},
GtString: "1234",
MinIntPtr: &i,
Contains: "purpose Of this test",
ContainsArray: []string{"test", "last", "purpose", "nonsense"},
}

err := underTest.Validate()
Expand Down
Loading

0 comments on commit 8bc4bc7

Please sign in to comment.