Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Package vallie stands in for a stand-alone validation library | ||
// purely for discussion purposes. | ||
// | ||
// Assuming this is a desired approach, more fidelity will be added | ||
// and vallie would be extracted into it's own repo | ||
package vallie | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var errZeroVal = errors.New("Object is a zero value") | ||
|
||
// Validate accepts objects to be validated from constructor and returns an error | ||
// if any of the provided object is a zero value object | ||
// | ||
// TODO: if Validate returns an error, how can we hook into the dig resolution | ||
// cycle to produce a meaningful error message, i.e. | ||
// "failed to resolve Type3, because non-optional Type2 is not registered" | ||
func Validate(values ...interface{}) error { | ||
for _, val := range values { | ||
v := reflect.ValueOf(val) | ||
if v == reflect.Zero(v.Type()) { | ||
return errors.Wrapf(errZeroVal, "%v", v.Type()) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package vallie | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidation(t *testing.T) { | ||
type nothing struct{} | ||
|
||
tests := []struct { | ||
desc string | ||
input []interface{} | ||
valid bool | ||
}{ | ||
{"empty input", []interface{}{}, true}, | ||
{"nil pointer", []interface{}{(*nothing)(nil)}, false}, | ||
{"zero value", []interface{}{nothing{}}, false}, | ||
{"non-zero value", []interface{}{5}, true}, | ||
{"mixed input", []interface{}{5, http.DefaultServeMux}, true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
assert.Equal( | ||
t, | ||
tt.valid, | ||
Validate(tt.input...) == nil, | ||
"unexpected validation result for %v", tt.input, | ||
) | ||
}) | ||
} | ||
} |