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

Method StructPartialCtx

validator_instance.go:448–515  ·  view source on GitHub ↗

StructPartialCtx validates the fields passed in only, ignoring all others and allows passing of contextual validation information via context.Context Fields may be provided in a namespaced fashion relative to the struct provided eg. NestedStruct.Field or NestedArrayField[0].Struct.Name It returns I

(ctx context.Context, s interface{}, fields ...string)

Source from the content-addressed store, hash-verified

446// It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise.
447// You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors.
448func (v *Validate) StructPartialCtx(ctx context.Context, s interface{}, fields ...string) (err error) {
449 val := reflect.ValueOf(s)
450 top := val
451
452 if val.Kind() == reflect.Ptr && !val.IsNil() {
453 val = val.Elem()
454 }
455
456 if val.Kind() != reflect.Struct || val.Type().ConvertibleTo(timeType) {
457 return &InvalidValidationError{Type: reflect.TypeOf(s)}
458 }
459
460 // good to validate
461 vd := v.pool.Get().(*validate)
462 vd.top = top
463 vd.isPartial = true
464 vd.ffn = nil
465 vd.hasExcludes = false
466 vd.includeExclude = make(map[string]struct{})
467
468 typ := val.Type()
469 name := typ.Name()
470
471 for _, k := range fields {
472 flds := strings.Split(k, namespaceSeparator)
473 if len(flds) > 0 {
474 vd.misc = append(vd.misc[0:0], name...)
475 // Don't append empty name for unnamed structs
476 if len(vd.misc) != 0 {
477 vd.misc = append(vd.misc, '.')
478 }
479
480 for _, s := range flds {
481 idx := strings.Index(s, leftBracket)
482
483 if idx != -1 {
484 for idx != -1 {
485 vd.misc = append(vd.misc, s[:idx]...)
486 vd.includeExclude[string(vd.misc)] = struct{}{}
487
488 idx2 := strings.Index(s, rightBracket)
489 idx2++
490 vd.misc = append(vd.misc, s[idx:idx2]...)
491 vd.includeExclude[string(vd.misc)] = struct{}{}
492 s = s[idx2:]
493 idx = strings.Index(s, leftBracket)
494 }
495 } else {
496 vd.misc = append(vd.misc, s...)
497 vd.includeExclude[string(vd.misc)] = struct{}{}
498 }
499
500 vd.misc = append(vd.misc, '.')
501 }
502 }
503 }
504
505 vd.validateStruct(ctx, top, val, typ, vd.ns[0:0], vd.actualNs[0:0], nil)

Callers 2

StructPartialMethod · 0.95
TestStructPartialFunction · 0.80

Calls 4

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

Tested by 1

TestStructPartialFunction · 0.64