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

Function TestValidateStructRegisterCtx

validator_test.go:10896–10927  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10894}
10895
10896func TestValidateStructRegisterCtx(t *testing.T) {
10897 var ctxVal string
10898
10899 fnCtx := func(ctx context.Context, fl FieldLevel) bool {
10900 ctxVal = ctx.Value(&ctxVal).(string)
10901 return true
10902 }
10903
10904 var ctxSlVal string
10905 slFn := func(ctx context.Context, sl StructLevel) {
10906 ctxSlVal = ctx.Value(&ctxSlVal).(string)
10907 }
10908
10909 type Test struct {
10910 Field string `validate:"val"`
10911 }
10912
10913 var tst Test
10914
10915 validate := New()
10916 err := validate.RegisterValidationCtx("val", fnCtx)
10917 Equal(t, err, nil)
10918
10919 validate.RegisterStructValidationCtx(slFn, Test{})
10920
10921 ctx := context.WithValue(context.Background(), &ctxVal, "testval")
10922 ctx = context.WithValue(ctx, &ctxSlVal, "slVal")
10923 errs := validate.StructCtx(ctx, tst)
10924 Equal(t, errs, nil)
10925 Equal(t, ctxVal, "testval")
10926 Equal(t, ctxSlVal, "slVal")
10927}
10928
10929func TestHostnameRFC952Validation(t *testing.T) {
10930 tests := []struct {

Callers

nothing calls this directly

Calls 5

NewFunction · 0.85
RegisterValidationCtxMethod · 0.80
StructCtxMethod · 0.80
ValueMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…