Skip to content

Commit e66bd70

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #167 from joeybloggs/v7-development
Update Validator instance check
2 parents 0650f73 + 14df416 commit e66bd70

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

validator.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@ import (
2020
)
2121

2222
const (
23-
utf8HexComma = "0x2C"
24-
utf8Pipe = "0x7C"
25-
tagSeparator = ","
26-
orSeparator = "|"
27-
tagKeySeparator = "="
28-
structOnlyTag = "structonly"
29-
omitempty = "omitempty"
30-
skipValidationTag = "-"
31-
diveTag = "dive"
32-
existsTag = "exists"
33-
fieldErrMsg = "Key: \"%s\" Error:Field validation for \"%s\" failed on the \"%s\" tag"
34-
arrayIndexFieldName = "%s" + leftBracket + "%d" + rightBracket
35-
mapIndexFieldName = "%s" + leftBracket + "%v" + rightBracket
36-
invalidValidation = "Invalid validation tag on field %s"
37-
undefinedValidation = "Undefined validation function on field %s"
23+
utf8HexComma = "0x2C"
24+
utf8Pipe = "0x7C"
25+
tagSeparator = ","
26+
orSeparator = "|"
27+
tagKeySeparator = "="
28+
structOnlyTag = "structonly"
29+
omitempty = "omitempty"
30+
skipValidationTag = "-"
31+
diveTag = "dive"
32+
existsTag = "exists"
33+
fieldErrMsg = "Key: \"%s\" Error:Field validation for \"%s\" failed on the \"%s\" tag"
34+
arrayIndexFieldName = "%s" + leftBracket + "%d" + rightBracket
35+
mapIndexFieldName = "%s" + leftBracket + "%v" + rightBracket
36+
invalidValidation = "Invalid validation tag on field %s"
37+
undefinedValidation = "Undefined validation function on field %s"
38+
validatorNotInitialized = "Validator instance not initialized"
3839
)
3940

4041
var (
@@ -78,6 +79,12 @@ type Validate struct {
7879
config Config
7980
}
8081

82+
func (v *Validate) initCheck() {
83+
if v == nil {
84+
panic(validatorNotInitialized)
85+
}
86+
}
87+
8188
// Config contains the options that a Validator instance will use.
8289
// It is passed to the New() function
8390
type Config struct {
@@ -146,9 +153,8 @@ func New(config Config) *Validate {
146153
// NOTE: if the key already exists, the previous validation function will be replaced.
147154
// NOTE: this method is not thread-safe
148155
func (v *Validate) RegisterValidation(key string, f Func) error {
149-
if v == nil {
150-
panic("Validate.RegisterValidation called with nil receiver")
151-
}
156+
v.initCheck()
157+
152158
if len(key) == 0 {
153159
return errors.New("Function Key cannot be empty")
154160
}
@@ -164,9 +170,7 @@ func (v *Validate) RegisterValidation(key string, f Func) error {
164170

165171
// RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
166172
func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) {
167-
if v == nil {
168-
panic("Validate.RegisterCustomTypeFunc called with nil receiver")
169-
}
173+
v.initCheck()
170174

171175
if v.config.CustomTypeFuncs == nil {
172176
v.config.CustomTypeFuncs = map[reflect.Type]CustomTypeFunc{}
@@ -183,9 +187,8 @@ func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{
183187
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
184188
// validate Array, Slice and maps fields which may contain more than one error
185189
func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
186-
if v == nil {
187-
panic("Validate.Field called with nil receiver")
188-
}
190+
v.initCheck()
191+
189192
errs := errsPool.Get().(ValidationErrors)
190193
fieldVal := reflect.ValueOf(field)
191194

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

210211
errs := errsPool.Get().(ValidationErrors)
211212
topVal := reflect.ValueOf(val)
@@ -226,9 +227,8 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
226227
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
227228
// legacy data structure, it will be useful
228229
func (v *Validate) StructPartial(current interface{}, fields ...string) ValidationErrors {
229-
if v == nil {
230-
panic("Validate.StructPartial called with nil receiver")
231-
}
230+
v.initCheck()
231+
232232
sv, _ := v.extractType(reflect.ValueOf(current))
233233
name := sv.Type().Name()
234234
m := map[string]*struct{}{}
@@ -286,9 +286,8 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) Validati
286286
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
287287
// legacy data structure, it will be useful
288288
func (v *Validate) StructExcept(current interface{}, fields ...string) ValidationErrors {
289-
if v == nil {
290-
panic("Validate.StructExcept called with nil receiver")
291-
}
289+
v.initCheck()
290+
292291
sv, _ := v.extractType(reflect.ValueOf(current))
293292
name := sv.Type().Name()
294293
m := map[string]*struct{}{}
@@ -311,9 +310,8 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) Validatio
311310

312311
// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
313312
func (v *Validate) Struct(current interface{}) ValidationErrors {
314-
if v == nil {
315-
panic("Validate.Struct called with nil receiver")
316-
}
313+
v.initCheck()
314+
317315
errs := errsPool.Get().(ValidationErrors)
318316
sv := reflect.ValueOf(current)
319317

validator_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ func TestNilValidator(t *testing.T) {
225225
return current.String() == field.String()
226226
}
227227

228-
PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, "Validate.RegisterCustomTypeFunc called with nil receiver")
229-
PanicMatches(t, func() { val.RegisterValidation("something", fn) }, "Validate.RegisterValidation called with nil receiver")
230-
PanicMatches(t, func() { val.Field(ts.Test, "required") }, "Validate.Field called with nil receiver")
231-
PanicMatches(t, func() { val.FieldWithValue("test", ts.Test, "required") }, "Validate.FieldWithValue called with nil receiver")
232-
PanicMatches(t, func() { val.Struct(ts) }, "Validate.Struct called with nil receiver")
233-
PanicMatches(t, func() { val.StructExcept(ts, "Test") }, "Validate.StructExcept called with nil receiver")
234-
PanicMatches(t, func() { val.StructPartial(ts, "Test") }, "Validate.StructPartial called with nil receiver")
228+
PanicMatches(t, func() { val.RegisterCustomTypeFunc(ValidateCustomType, MadeUpCustomType{}) }, validatorNotInitialized)
229+
PanicMatches(t, func() { val.RegisterValidation("something", fn) }, validatorNotInitialized)
230+
PanicMatches(t, func() { val.Field(ts.Test, "required") }, validatorNotInitialized)
231+
PanicMatches(t, func() { val.FieldWithValue("test", ts.Test, "required") }, validatorNotInitialized)
232+
PanicMatches(t, func() { val.Struct(ts) }, validatorNotInitialized)
233+
PanicMatches(t, func() { val.StructExcept(ts, "Test") }, validatorNotInitialized)
234+
PanicMatches(t, func() { val.StructPartial(ts, "Test") }, validatorNotInitialized)
235235

236236
}
237237

0 commit comments

Comments
 (0)