StructExceptCtx validates all fields except the ones passed in and allows passing of contextual validation information via context.Context Fields may be provided in a namespaced fashion relative to the struct provided i.e. NestedStruct.Field or NestedArrayField[0].Struct.Name It returns InvalidVali
(ctx context.Context, s interface{}, fields ...string)
| 532 | // It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise. |
| 533 | // You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors. |
| 534 | func (v *Validate) StructExceptCtx(ctx context.Context, s interface{}, fields ...string) (err error) { |
| 535 | val := reflect.ValueOf(s) |
| 536 | top := val |
| 537 | |
| 538 | if val.Kind() == reflect.Ptr && !val.IsNil() { |
| 539 | val = val.Elem() |
| 540 | } |
| 541 | |
| 542 | if val.Kind() != reflect.Struct || val.Type().ConvertibleTo(timeType) { |
| 543 | return &InvalidValidationError{Type: reflect.TypeOf(s)} |
| 544 | } |
| 545 | |
| 546 | // good to validate |
| 547 | vd := v.pool.Get().(*validate) |
| 548 | vd.top = top |
| 549 | vd.isPartial = true |
| 550 | vd.ffn = nil |
| 551 | vd.hasExcludes = true |
| 552 | vd.includeExclude = make(map[string]struct{}) |
| 553 | |
| 554 | typ := val.Type() |
| 555 | name := typ.Name() |
| 556 | |
| 557 | for _, key := range fields { |
| 558 | vd.misc = vd.misc[0:0] |
| 559 | |
| 560 | if len(name) > 0 { |
| 561 | vd.misc = append(vd.misc, name...) |
| 562 | vd.misc = append(vd.misc, '.') |
| 563 | } |
| 564 | |
| 565 | vd.misc = append(vd.misc, key...) |
| 566 | vd.includeExclude[string(vd.misc)] = struct{}{} |
| 567 | } |
| 568 | |
| 569 | vd.validateStruct(ctx, top, val, typ, vd.ns[0:0], vd.actualNs[0:0], nil) |
| 570 | |
| 571 | if len(vd.errs) > 0 { |
| 572 | err = vd.errs |
| 573 | vd.errs = nil |
| 574 | } |
| 575 | |
| 576 | v.pool.Put(vd) |
| 577 | |
| 578 | return |
| 579 | } |
| 580 | |
| 581 | // Var validates a single variable using tag style validation. |
| 582 | // eg. |