Skip to content

Commit

Permalink
Update Validator instance check
Browse files Browse the repository at this point in the history
* change for one validator instance method and message
  • Loading branch information
joeybloggs authored and joeybloggs committed Sep 3, 2015
1 parent 0650f73 commit 14df416
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
70 changes: 34 additions & 36 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ import (
)

const (
utf8HexComma = "0x2C"
utf8Pipe = "0x7C"
tagSeparator = ","
orSeparator = "|"
tagKeySeparator = "="
structOnlyTag = "structonly"
omitempty = "omitempty"
skipValidationTag = "-"
diveTag = "dive"
existsTag = "exists"
fieldErrMsg = "Key: \"%s\" Error:Field validation for \"%s\" failed on the \"%s\" tag"
arrayIndexFieldName = "%s" + leftBracket + "%d" + rightBracket
mapIndexFieldName = "%s" + leftBracket + "%v" + rightBracket
invalidValidation = "Invalid validation tag on field %s"
undefinedValidation = "Undefined validation function on field %s"
utf8HexComma = "0x2C"
utf8Pipe = "0x7C"
tagSeparator = ","
orSeparator = "|"
tagKeySeparator = "="
structOnlyTag = "structonly"
omitempty = "omitempty"
skipValidationTag = "-"
diveTag = "dive"
existsTag = "exists"
fieldErrMsg = "Key: \"%s\" Error:Field validation for \"%s\" failed on the \"%s\" tag"
arrayIndexFieldName = "%s" + leftBracket + "%d" + rightBracket
mapIndexFieldName = "%s" + leftBracket + "%v" + rightBracket
invalidValidation = "Invalid validation tag on field %s"
undefinedValidation = "Undefined validation function on field %s"
validatorNotInitialized = "Validator instance not initialized"
)

var (
Expand Down Expand Up @@ -78,6 +79,12 @@ type Validate struct {
config Config
}

func (v *Validate) initCheck() {
if v == nil {
panic(validatorNotInitialized)
}
}

// Config contains the options that a Validator instance will use.
// It is passed to the New() function
type Config struct {
Expand Down Expand Up @@ -146,9 +153,8 @@ func New(config Config) *Validate {
// NOTE: if the key already exists, the previous validation function will be replaced.
// NOTE: this method is not thread-safe
func (v *Validate) RegisterValidation(key string, f Func) error {
if v == nil {
panic("Validate.RegisterValidation called with nil receiver")
}
v.initCheck()

if len(key) == 0 {
return errors.New("Function Key cannot be empty")
}
Expand All @@ -164,9 +170,7 @@ func (v *Validate) RegisterValidation(key string, f Func) error {

// RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) {
if v == nil {
panic("Validate.RegisterCustomTypeFunc called with nil receiver")
}
v.initCheck()

if v.config.CustomTypeFuncs == nil {
v.config.CustomTypeFuncs = map[reflect.Type]CustomTypeFunc{}
Expand All @@ -183,9 +187,8 @@ func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
// validate Array, Slice and maps fields which may contain more than one error
func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
if v == nil {
panic("Validate.Field called with nil receiver")
}
v.initCheck()

errs := errsPool.Get().(ValidationErrors)
fieldVal := reflect.ValueOf(field)

Expand All @@ -203,9 +206,7 @@ func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
// validate Array, Slice and maps fields which may contain more than one error
func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string) ValidationErrors {
if v == nil {
panic("Validate.FieldWithValue called with nil receiver")
}
v.initCheck()

errs := errsPool.Get().(ValidationErrors)
topVal := reflect.ValueOf(val)
Expand All @@ -226,9 +227,8 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
// legacy data structure, it will be useful
func (v *Validate) StructPartial(current interface{}, fields ...string) ValidationErrors {
if v == nil {
panic("Validate.StructPartial called with nil receiver")
}
v.initCheck()

sv, _ := v.extractType(reflect.ValueOf(current))
name := sv.Type().Name()
m := map[string]*struct{}{}
Expand Down Expand Up @@ -286,9 +286,8 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) Validati
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
// legacy data structure, it will be useful
func (v *Validate) StructExcept(current interface{}, fields ...string) ValidationErrors {
if v == nil {
panic("Validate.StructExcept called with nil receiver")
}
v.initCheck()

sv, _ := v.extractType(reflect.ValueOf(current))
name := sv.Type().Name()
m := map[string]*struct{}{}
Expand All @@ -311,9 +310,8 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) Validatio

// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
func (v *Validate) Struct(current interface{}) ValidationErrors {
if v == nil {
panic("Validate.Struct called with nil receiver")
}
v.initCheck()

errs := errsPool.Get().(ValidationErrors)
sv := reflect.ValueOf(current)

Expand Down
14 changes: 7 additions & 7 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ func TestNilValidator(t *testing.T) {
return current.String() == field.String()
}

PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, "Validate.RegisterCustomTypeFunc called with nil receiver")
PanicMatches(t, func() { val.RegisterValidation("something", fn) }, "Validate.RegisterValidation called with nil receiver")
PanicMatches(t, func() { val.Field(ts.Test, "required") }, "Validate.Field called with nil receiver")
PanicMatches(t, func() { val.FieldWithValue("test", ts.Test, "required") }, "Validate.FieldWithValue called with nil receiver")
PanicMatches(t, func() { val.Struct(ts) }, "Validate.Struct called with nil receiver")
PanicMatches(t, func() { val.StructExcept(ts, "Test") }, "Validate.StructExcept called with nil receiver")
PanicMatches(t, func() { val.StructPartial(ts, "Test") }, "Validate.StructPartial called with nil receiver")
PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, validatorNotInitialized)
PanicMatches(t, func() { val.RegisterValidation("something", fn) }, validatorNotInitialized)
PanicMatches(t, func() { val.Field(ts.Test, "required") }, validatorNotInitialized)
PanicMatches(t, func() { val.FieldWithValue("test", ts.Test, "required") }, validatorNotInitialized)
PanicMatches(t, func() { val.Struct(ts) }, validatorNotInitialized)
PanicMatches(t, func() { val.StructExcept(ts, "Test") }, validatorNotInitialized)
PanicMatches(t, func() { val.StructPartial(ts, "Test") }, validatorNotInitialized)

}

Expand Down

0 comments on commit 14df416

Please sign in to comment.