@@ -20,21 +20,22 @@ import (
2020)
2121
2222const (
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
4041var (
@@ -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
8390type 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
148155func (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
166172func (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
185189func (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
205208func (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
228229func (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
288288func (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.
313312func (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
0 commit comments