MCPcopy
hub / github.com/go-playground/validator / StructCtx

Method StructCtx

validator_instance.go:355–383  ·  view source on GitHub ↗

StructCtx validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified and also allows passing of context.Context for contextual validation information. It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error othe

(ctx context.Context, s interface{})

Source from the content-addressed store, hash-verified

353// It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise.
354// You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors.
355func (v *Validate) StructCtx(ctx context.Context, s interface{}) (err error) {
356 val := reflect.ValueOf(s)
357 top := val
358
359 if val.Kind() == reflect.Ptr && !val.IsNil() {
360 val = val.Elem()
361 }
362
363 if val.Kind() != reflect.Struct || val.Type().ConvertibleTo(timeType) {
364 return &InvalidValidationError{Type: reflect.TypeOf(s)}
365 }
366
367 // good to validate
368 vd := v.pool.Get().(*validate)
369 vd.top = top
370 vd.isPartial = false
371 // vd.hasExcludes = false // only need to reset in StructPartial and StructExcept
372
373 vd.validateStruct(ctx, top, val, val.Type(), vd.ns[0:0], vd.actualNs[0:0], nil)
374
375 if len(vd.errs) > 0 {
376 err = vd.errs
377 vd.errs = nil
378 }
379
380 v.pool.Put(vd)
381
382 return
383}
384
385// StructFiltered validates a structs exposed fields, that pass the FilterFunc check and automatically validates
386// nested structs, unless otherwise specified.

Callers 2

StructMethod · 0.95

Calls 4

validateStructMethod · 0.80
KindMethod · 0.65
TypeMethod · 0.65
GetMethod · 0.45

Tested by 1