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

Function TestCustomFieldName

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

Source from the content-addressed store, hash-verified

10134}
10135
10136func TestCustomFieldName(t *testing.T) {
10137 validate := New()
10138 validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
10139 name := strings.SplitN(fld.Tag.Get("schema"), ",", 2)[0]
10140
10141 if name == "-" {
10142 return ""
10143 }
10144
10145 return name
10146 })
10147
10148 type A struct {
10149 B string `schema:"b" validate:"required"`
10150 C string `schema:"c" validate:"required"`
10151 D []bool `schema:"d" validate:"required"`
10152 E string `schema:"-" validate:"required"`
10153 }
10154
10155 a := &A{}
10156
10157 err := validate.Struct(a)
10158 NotEqual(t, err, nil)
10159
10160 errs := err.(ValidationErrors)
10161 Equal(t, len(errs), 4)
10162 Equal(t, getError(errs, "A.b", "A.B").Field(), "b")
10163 Equal(t, getError(errs, "A.c", "A.C").Field(), "c")
10164 Equal(t, getError(errs, "A.d", "A.D").Field(), "d")
10165 Equal(t, getError(errs, "A.E", "A.E").Field(), "E")
10166
10167 v2 := New()
10168 err = v2.Struct(a)
10169 NotEqual(t, err, nil)
10170
10171 errs = err.(ValidationErrors)
10172 Equal(t, len(errs), 4)
10173 Equal(t, getError(errs, "A.B", "A.B").Field(), "B")
10174 Equal(t, getError(errs, "A.C", "A.C").Field(), "C")
10175 Equal(t, getError(errs, "A.D", "A.D").Field(), "D")
10176 Equal(t, getError(errs, "A.E", "A.E").Field(), "E")
10177}
10178
10179func TestMultipleRecursiveExtractStructCache(t *testing.T) {
10180 validate := New()

Callers

nothing calls this directly

Calls 6

NewFunction · 0.85
getErrorFunction · 0.85
RegisterTagNameFuncMethod · 0.80
StructMethod · 0.80
FieldMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…