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

Function ExampleResolver

resolver_test.go:35–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33}
34
35func ExampleResolver() {
36 // Create the API.
37 r := http.NewServeMux()
38 api := NewExampleAPI(r, huma.DefaultConfig("Example API", "1.0.0"))
39
40 huma.Register(api, huma.Operation{
41 OperationID: "resolver-example",
42 Method: http.MethodPut,
43 Path: "/resolver",
44 }, func(ctx context.Context, input *struct {
45 // Step 2: Use your custom struct with the resolver as a field in the
46 // request input. Here we use it as the body of the request.
47 Body ExampleInputBody
48 }) (*struct{}, error) {
49 // Do nothing. Validation should catch the error!
50 return nil, nil
51 })
52
53 // Make an example request showing the validation error response.
54 req, _ := http.NewRequest(http.MethodPut, "/resolver", strings.NewReader(`{"count": 30}`))
55 req.Host = "example.com"
56 req.Header.Set("Content-Type", "application/json")
57
58 w := httptest.NewRecorder()
59
60 r.ServeHTTP(w, req)
61
62 out := bytes.NewBuffer(nil)
63 json.Indent(out, w.Body.Bytes(), "", " ")
64 fmt.Println(out.String())
65 // Output: {
66 // "$schema": "https://example.com/schemas/ErrorModel.json",
67 // "title": "Unprocessable Entity",
68 // "status": 422,
69 // "detail": "validation failed",
70 // "errors": [
71 // {
72 // "message": "multiples of 30 are not allowed",
73 // "location": "body.count",
74 // "value": 30
75 // }
76 // ]
77 // }
78}

Callers

nothing calls this directly

Calls 3

BytesMethod · 0.80
ServeHTTPMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…