-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjson_body_validation.go
52 lines (40 loc) · 1.29 KB
/
json_body_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"github.com/swaggest/usecase"
)
func jsonBodyValidation() usecase.Interactor {
u := usecase.IOInteractor{}
u.SetTitle("Request With JSON Body and non-trivial validation")
u.SetDescription("Request with JSON body and query/header/path params, response with JSON body and data from request.")
type JSONPayload struct {
ID int `json:"id" minimum:"100"`
Name string `json:"name" minLength:"3"`
}
type inputWithJSON struct {
Header string `header:"X-Header" description:"Simple scalar value in header." minLength:"3"`
Query int `query:"in_query" description:"Simple scalar value in query." minimum:"100"`
Path string `path:"in-path" description:"Simple scalar value in path" minLength:"3"`
JSONPayload
}
type outputWithJSON struct {
Header string `json:"inHeader" minLength:"3"`
Query int `json:"inQuery" minimum:"3"`
Path string `json:"inPath" minLength:"3"`
JSONPayload
}
u.Input = new(inputWithJSON)
u.Output = new(outputWithJSON)
u.Interactor = usecase.Interact(func(ctx context.Context, input, output any) (err error) {
var (
in = input.(*inputWithJSON)
out = output.(*outputWithJSON)
)
out.Query = in.Query
out.Header = in.Header
out.Path = in.Path
out.JSONPayload = in.JSONPayload
return nil
})
return u
}