From 70cb087d5ac37008dc7edf011b7f30f34d3aa771 Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Wed, 2 Sep 2015 08:25:26 -0400 Subject: [PATCH] update how config is passed in and assigned to validator * makes it safer as options passed in by reference, in the future, will not be manipulatable externally. --- examples_test.go | 6 +++--- validator.go | 8 ++++---- validator_test.go | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples_test.go b/examples_test.go index 2ebf8b3..3144bae 100644 --- a/examples_test.go +++ b/examples_test.go @@ -8,7 +8,7 @@ import ( ) func ExampleValidate_new() { - config := validator.Config{TagName: "validate"} + config := &validator.Config{TagName: "validate"} validator.New(config) } @@ -17,7 +17,7 @@ func ExampleValidate_field() { // This should be stored somewhere globally var validate *validator.Validate - config := validator.Config{TagName: "validate"} + config := &validator.Config{TagName: "validate"} validate = validator.New(config) @@ -43,7 +43,7 @@ func ExampleValidate_struct() { // This should be stored somewhere globally var validate *validator.Validate - config := validator.Config{TagName: "validate"} + config := &validator.Config{TagName: "validate"} validate = validator.New(config) diff --git a/validator.go b/validator.go index eb5b7d6..bef479d 100644 --- a/validator.go +++ b/validator.go @@ -82,7 +82,7 @@ func (s *tagCacheMap) Set(key string, value *cachedTag) { // Validate contains the validator settings passed in using the Config struct type Validate struct { - config Config + tagName string validationFuncs map[string]Func customTypeFuncs map[reflect.Type]CustomTypeFunc aliasValidators map[string]string @@ -144,12 +144,12 @@ type FieldError struct { } // New creates a new Validate instance for use. -func New(config Config) *Validate { +func New(config *Config) *Validate { // if config.CustomTypeFuncs != nil && len(config.CustomTypeFuncs) > 0 { // config.hasCustomFuncs = true // } - v := &Validate{config: config, tagsCache: &tagCacheMap{m: map[string]*cachedTag{}}} + v := &Validate{tagName: config.TagName, tagsCache: &tagCacheMap{m: map[string]*cachedTag{}}} if len(v.aliasValidators) == 0 { // must copy validators for separate validations to be used in each validator instance @@ -403,7 +403,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec } } - v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.config.TagName), fld.Name, partial, exclude, includeExclude) + v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, partial, exclude, includeExclude) } } diff --git a/validator_test.go b/validator_test.go index 117d7e0..12c19ff 100644 --- a/validator_test.go +++ b/validator_test.go @@ -111,7 +111,7 @@ type TestSlice struct { OmitEmpty []int `validate:"omitempty,min=1,max=10"` } -var validate = New(Config{TagName: "validate"}) +var validate = New(&Config{TagName: "validate"}) func AssertError(t *testing.T, err error, key, field, expectedTag string) { @@ -1255,7 +1255,7 @@ func TestExistsValidation(t *testing.T) { func TestSQLValue2Validation(t *testing.T) { - config := Config{ + config := &Config{ TagName: "validate", } @@ -1318,7 +1318,7 @@ func TestSQLValueValidation(t *testing.T) { // customTypes[reflect.TypeOf(MadeUpCustomType{})] = ValidateCustomType // customTypes[reflect.TypeOf(1)] = OverrideIntTypeForSomeReason - validate := New(Config{TagName: "validate"}) + validate := New(&Config{TagName: "validate"}) validate.RegisterCustomTypeFunc(ValidateValuerType, (*driver.Valuer)(nil), valuer{}) validate.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) validate.RegisterCustomTypeFunc(OverrideIntTypeForSomeReason, 1) @@ -3875,7 +3875,7 @@ func TestAddFunctions(t *testing.T) { return true } - config := Config{ + config := &Config{ TagName: "validateme", } @@ -3898,7 +3898,7 @@ func TestAddFunctions(t *testing.T) { func TestChangeTag(t *testing.T) { - config := Config{ + config := &Config{ TagName: "val", } validate := New(config)