MCPcopy
hub / github.com/danielgtaylor/huma / TestValidateCustomFormatter

Function TestValidateCustomFormatter

validate_test.go:1495–1515  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1493}
1494
1495func TestValidateCustomFormatter(t *testing.T) {
1496 originalFormatter := huma.ErrorFormatter
1497 defer func() {
1498 huma.ErrorFormatter = originalFormatter
1499 }()
1500
1501 huma.ErrorFormatter = func(format string, a ...any) string {
1502 return fmt.Sprintf("custom: %v", a)
1503 }
1504
1505 registry := huma.NewMapRegistry("#/components/schemas/", huma.DefaultSchemaNamer)
1506 s := registry.Schema(reflect.TypeFor[struct {
1507 Value string "json:\"value\" format:\"email\""
1508 }](), true, "TestInput")
1509 pb := huma.NewPathBuffer([]byte(""), 0)
1510 res := &huma.ValidateResult{}
1511
1512 huma.Validate(registry, s, pb, huma.ModeReadFromServer, map[string]any{"value": "alice"}, res)
1513 assert.Len(t, res.Errors, 1)
1514 assert.Equal(t, "custom: [mail: missing '@' or angle-addr] (value: alice)", res.Errors[0].Error())
1515}
1516
1517type TransformDeleteField struct {
1518 Field1 string `json:"field1"`

Callers

nothing calls this directly

Calls 4

LenMethod · 0.80
SchemaMethod · 0.65
ErrorMethod · 0.65
ValidateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…