@@ -146,7 +146,9 @@ func New(config Config) *Validate {
146146// NOTE: if the key already exists, the previous validation function will be replaced.
147147// NOTE: this method is not thread-safe
148148func (v * Validate ) RegisterValidation (key string , f Func ) error {
149-
149+ if v == nil {
150+ panic ("Validate.RegisterValidation called with nil receiver" )
151+ }
150152 if len (key ) == 0 {
151153 return errors .New ("Function Key cannot be empty" )
152154 }
@@ -162,6 +164,9 @@ func (v *Validate) RegisterValidation(key string, f Func) error {
162164
163165// RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
164166func (v * Validate ) RegisterCustomTypeFunc (fn CustomTypeFunc , types ... interface {}) {
167+ if v == nil {
168+ panic ("Validate.RegisterCustomTypeFunc called with nil receiver" )
169+ }
165170
166171 if v .config .CustomTypeFuncs == nil {
167172 v .config .CustomTypeFuncs = map [reflect.Type ]CustomTypeFunc {}
@@ -178,7 +183,9 @@ func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{
178183// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
179184// validate Array, Slice and maps fields which may contain more than one error
180185func (v * Validate ) Field (field interface {}, tag string ) ValidationErrors {
181-
186+ if v == nil {
187+ panic ("Validate.Field called with nil receiver" )
188+ }
182189 errs := errsPool .Get ().(ValidationErrors )
183190 fieldVal := reflect .ValueOf (field )
184191
@@ -196,6 +203,9 @@ func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
196203// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
197204// validate Array, Slice and maps fields which may contain more than one error
198205func (v * Validate ) FieldWithValue (val interface {}, field interface {}, tag string ) ValidationErrors {
206+ if v == nil {
207+ panic ("Validate.FieldWithValue called with nil receiver" )
208+ }
199209
200210 errs := errsPool .Get ().(ValidationErrors )
201211 topVal := reflect .ValueOf (val )
@@ -216,7 +226,9 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string
216226// NOTE: This is normally not needed, however in some specific cases such as: tied to a
217227// legacy data structure, it will be useful
218228func (v * Validate ) StructPartial (current interface {}, fields ... string ) ValidationErrors {
219-
229+ if v == nil {
230+ panic ("Validate.StructPartial called with nil receiver" )
231+ }
220232 sv , _ := v .extractType (reflect .ValueOf (current ))
221233 name := sv .Type ().Name ()
222234 m := map [string ]* struct {}{}
@@ -274,7 +286,9 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) Validati
274286// NOTE: This is normally not needed, however in some specific cases such as: tied to a
275287// legacy data structure, it will be useful
276288func (v * Validate ) StructExcept (current interface {}, fields ... string ) ValidationErrors {
277-
289+ if v == nil {
290+ panic ("Validate.StructExcept called with nil receiver" )
291+ }
278292 sv , _ := v .extractType (reflect .ValueOf (current ))
279293 name := sv .Type ().Name ()
280294 m := map [string ]* struct {}{}
@@ -297,7 +311,9 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) Validatio
297311
298312// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
299313func (v * Validate ) Struct (current interface {}) ValidationErrors {
300-
314+ if v == nil {
315+ panic ("Validate.Struct called with nil receiver" )
316+ }
301317 errs := errsPool .Get ().(ValidationErrors )
302318 sv := reflect .ValueOf (current )
303319
0 commit comments