-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
72 lines (61 loc) · 2.09 KB
/
options.go
File metadata and controls
72 lines (61 loc) · 2.09 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package openapi_validator
import (
"net/http"
"github.com/getkin/kin-openapi/routers"
)
// Option is a function type used to configure the Options struct.
type Option func(*Options)
// Options contains the configuration for the OpenAPI validator.
type Options struct {
// ValidateRequests specifies whether incoming requests should be validated against the spec.
ValidateRequests bool
// ValidateResponses specifies whether outgoing responses should be validated against the spec.
ValidateResponses bool
// SwaggerUIPath is the URL path where Swagger UI will be served.
SwaggerUIPath string
// ErrorEncoder is used to format and send validation error responses.
ErrorEncoder ErrorEncoder
// Router is used for matching requests to OpenAPI paths.
Router routers.Router
}
// ErrorEncoder is a function type used to encode validation errors into an HTTP response.
type ErrorEncoder func(w http.ResponseWriter, r *http.Request, err error)
// DefaultOptions returns the default configuration for the validator.
func DefaultOptions() *Options {
return &Options{
ValidateRequests: true,
ValidateResponses: false,
SwaggerUIPath: "/docs",
ErrorEncoder: DefaultErrorEncoder,
}
}
// WithValidateRequests returns an Option that enables or disables request validation.
func WithValidateRequests(validate bool) Option {
return func(o *Options) {
o.ValidateRequests = validate
}
}
// WithValidateResponses returns an Option that enables or disables response validation.
func WithValidateResponses(validate bool) Option {
return func(o *Options) {
o.ValidateResponses = validate
}
}
// WithSwaggerUIPath returns an Option that sets the URL path for the Swagger UI.
func WithSwaggerUIPath(path string) Option {
return func(o *Options) {
o.SwaggerUIPath = path
}
}
// WithErrorEncoder returns an Option that sets a custom error encoder.
func WithErrorEncoder(encoder ErrorEncoder) Option {
return func(o *Options) {
o.ErrorEncoder = encoder
}
}
// WithRouter returns an Option that sets a custom router.
func WithRouter(router routers.Router) Option {
return func(o *Options) {
o.Router = router
}
}