| 1493 | } |
| 1494 | |
| 1495 | func 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 | |
| 1517 | type TransformDeleteField struct { |
| 1518 | Field1 string `json:"field1"` |