145 | 145 |
// NOTE: if the key already exists, the previous validation function will be replaced.
|
146 | 146 |
// NOTE: this method is not thread-safe
|
147 | 147 |
func (v *Validate) RegisterValidation(key string, f Func) error {
|
148 | |
|
|
148 |
if v == nil {
|
|
149 |
panic("Validate.RegisterValidation called with nil receiver")
|
|
150 |
}
|
149 | 151 |
if len(key) == 0 {
|
150 | 152 |
return errors.New("Function Key cannot be empty")
|
151 | 153 |
}
|
|
161 | 163 |
|
162 | 164 |
// RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
|
163 | 165 |
func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) {
|
|
166 |
if v == nil {
|
|
167 |
panic("Validate.RegisterCustomTypeFunc called with nil receiver")
|
|
168 |
}
|
164 | 169 |
|
165 | 170 |
if v.config.CustomTypeFuncs == nil {
|
166 | 171 |
v.config.CustomTypeFuncs = map[reflect.Type]CustomTypeFunc{}
|
|
177 | 182 |
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
|
178 | 183 |
// validate Array, Slice and maps fields which may contain more than one error
|
179 | 184 |
func (v *Validate) Field(field interface{}, tag string) ValidationErrors {
|
180 | |
|
|
185 |
if v == nil {
|
|
186 |
panic("Validate.Field called with nil receiver")
|
|
187 |
}
|
181 | 188 |
errs := errsPool.Get().(ValidationErrors)
|
182 | 189 |
fieldVal := reflect.ValueOf(field)
|
183 | 190 |
|
|
195 | 202 |
// NOTE: it returns ValidationErrors instead of a single FieldError because this can also
|
196 | 203 |
// validate Array, Slice and maps fields which may contain more than one error
|
197 | 204 |
func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string) ValidationErrors {
|
|
205 |
if v == nil {
|
|
206 |
panic("Validate.FieldWithValue called with nil receiver")
|
|
207 |
}
|
198 | 208 |
|
199 | 209 |
errs := errsPool.Get().(ValidationErrors)
|
200 | 210 |
topVal := reflect.ValueOf(val)
|
|
215 | 225 |
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
|
216 | 226 |
// legacy data structure, it will be useful
|
217 | 227 |
func (v *Validate) StructPartial(current interface{}, fields ...string) ValidationErrors {
|
218 | |
|
|
228 |
if v == nil {
|
|
229 |
panic("Validate.StructPartial called with nil receiver")
|
|
230 |
}
|
219 | 231 |
sv, _ := v.extractType(reflect.ValueOf(current))
|
220 | 232 |
name := sv.Type().Name()
|
221 | 233 |
m := map[string]*struct{}{}
|
|
273 | 285 |
// NOTE: This is normally not needed, however in some specific cases such as: tied to a
|
274 | 286 |
// legacy data structure, it will be useful
|
275 | 287 |
func (v *Validate) StructExcept(current interface{}, fields ...string) ValidationErrors {
|
276 | |
|
|
288 |
if v == nil {
|
|
289 |
panic("Validate.StructExcept called with nil receiver")
|
|
290 |
}
|
277 | 291 |
sv, _ := v.extractType(reflect.ValueOf(current))
|
278 | 292 |
name := sv.Type().Name()
|
279 | 293 |
m := map[string]*struct{}{}
|
|
296 | 310 |
|
297 | 311 |
// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
|
298 | 312 |
func (v *Validate) Struct(current interface{}) ValidationErrors {
|
299 | |
|
|
313 |
if v == nil {
|
|
314 |
panic("Validate.Struct called with nil receiver")
|
|
315 |
}
|
300 | 316 |
errs := errsPool.Get().(ValidationErrors)
|
301 | 317 |
sv := reflect.ValueOf(current)
|
302 | 318 |
|