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.
pull/169/head
joeybloggs 9 years ago
parent 10cf645b91
commit 70cb087d5a
  1. 6
      examples_test.go
  2. 8
      validator.go
  3. 10
      validator_test.go

@ -8,7 +8,7 @@ import (
) )
func ExampleValidate_new() { func ExampleValidate_new() {
config := validator.Config{TagName: "validate"} config := &validator.Config{TagName: "validate"}
validator.New(config) validator.New(config)
} }
@ -17,7 +17,7 @@ func ExampleValidate_field() {
// This should be stored somewhere globally // This should be stored somewhere globally
var validate *validator.Validate var validate *validator.Validate
config := validator.Config{TagName: "validate"} config := &validator.Config{TagName: "validate"}
validate = validator.New(config) validate = validator.New(config)
@ -43,7 +43,7 @@ func ExampleValidate_struct() {
// This should be stored somewhere globally // This should be stored somewhere globally
var validate *validator.Validate var validate *validator.Validate
config := validator.Config{TagName: "validate"} config := &validator.Config{TagName: "validate"}
validate = validator.New(config) validate = validator.New(config)

@ -82,7 +82,7 @@ func (s *tagCacheMap) Set(key string, value *cachedTag) {
// Validate contains the validator settings passed in using the Config struct // Validate contains the validator settings passed in using the Config struct
type Validate struct { type Validate struct {
config Config tagName string
validationFuncs map[string]Func validationFuncs map[string]Func
customTypeFuncs map[reflect.Type]CustomTypeFunc customTypeFuncs map[reflect.Type]CustomTypeFunc
aliasValidators map[string]string aliasValidators map[string]string
@ -144,12 +144,12 @@ type FieldError struct {
} }
// New creates a new Validate instance for use. // 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 { // if config.CustomTypeFuncs != nil && len(config.CustomTypeFuncs) > 0 {
// config.hasCustomFuncs = true // 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 { if len(v.aliasValidators) == 0 {
// must copy validators for separate validations to be used in each validator instance // 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)
} }
} }

@ -111,7 +111,7 @@ type TestSlice struct {
OmitEmpty []int `validate:"omitempty,min=1,max=10"` 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) { 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) { func TestSQLValue2Validation(t *testing.T) {
config := Config{ config := &Config{
TagName: "validate", TagName: "validate",
} }
@ -1318,7 +1318,7 @@ func TestSQLValueValidation(t *testing.T) {
// customTypes[reflect.TypeOf(MadeUpCustomType{})] = ValidateCustomType // customTypes[reflect.TypeOf(MadeUpCustomType{})] = ValidateCustomType
// customTypes[reflect.TypeOf(1)] = OverrideIntTypeForSomeReason // customTypes[reflect.TypeOf(1)] = OverrideIntTypeForSomeReason
validate := New(Config{TagName: "validate"}) validate := New(&Config{TagName: "validate"})
validate.RegisterCustomTypeFunc(ValidateValuerType, (*driver.Valuer)(nil), valuer{}) validate.RegisterCustomTypeFunc(ValidateValuerType, (*driver.Valuer)(nil), valuer{})
validate.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) validate.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{})
validate.RegisterCustomTypeFunc(OverrideIntTypeForSomeReason, 1) validate.RegisterCustomTypeFunc(OverrideIntTypeForSomeReason, 1)
@ -3875,7 +3875,7 @@ func TestAddFunctions(t *testing.T) {
return true return true
} }
config := Config{ config := &Config{
TagName: "validateme", TagName: "validateme",
} }
@ -3898,7 +3898,7 @@ func TestAddFunctions(t *testing.T) {
func TestChangeTag(t *testing.T) { func TestChangeTag(t *testing.T) {
config := Config{ config := &Config{
TagName: "val", TagName: "val",
} }
validate := New(config) validate := New(config)

Loading…
Cancel
Save