MCPcopy Create free account
hub / github.com/go-playground/validator / VarCtx

Method VarCtx

validator_instance.go:612–631  ·  view source on GitHub ↗

VarCtx validates a single variable using tag style validation and allows passing of contextual validation information via context.Context. eg. var i int validate.Var(i, "gt=1,lt=10") WARNING: a struct can be passed for validation eg. time.Time is a struct or if you have a custom type and have regis

(ctx context.Context, field interface{}, tag string)

Source from the content-addressed store, hash-verified

610// You will need to assert the error if it's not nil eg. err.(validator.ValidationErrors) to access the array of errors.
611// validate Array, Slice and maps fields which may contain more than one error
612func (v *Validate) VarCtx(ctx context.Context, field interface{}, tag string) (err error) {
613 if len(tag) == 0 || tag == skipValidationTag {
614 return nil
615 }
616
617 ctag := v.fetchCacheTag(tag)
618
619 val := reflect.ValueOf(field)
620 vd := v.pool.Get().(*validate)
621 vd.top = val
622 vd.isPartial = false
623 vd.traverseField(ctx, val, val, vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)
624
625 if len(vd.errs) > 0 {
626 err = vd.errs
627 vd.errs = nil
628 }
629 v.pool.Put(vd)
630 return
631}
632
633// VarWithValue validates a single variable, against another variable/field's value using tag style validation
634// eg.

Callers 2

VarMethod · 0.95
TestSQLValue2ValidationFunction · 0.80

Calls 3

fetchCacheTagMethod · 0.95
traverseFieldMethod · 0.80
GetMethod · 0.45

Tested by 1

TestSQLValue2ValidationFunction · 0.64