forked from getkin/kin-openapi
/
content.go
45 lines (39 loc) · 876 Bytes
/
content.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
package openapi3
import (
"context"
"strings"
)
// Content is specified by OpenAPI/Swagger 3.0 standard.
type Content map[string]*MediaType
func NewContent() Content {
return make(map[string]*MediaType, 4)
}
func NewContentWithJSONSchema(schema *Schema) Content {
return Content{
"application/json": NewMediaType().WithSchema(schema),
}
}
func NewContentWithJSONSchemaRef(schema *SchemaRef) Content {
return Content{
"application/json": NewMediaType().WithSchemaRef(schema),
}
}
func (content Content) Get(mime string) *MediaType {
if v := content[mime]; v != nil {
return v
}
i := strings.IndexByte(mime, ';')
if i < 0 {
return nil
}
return content[mime[:i]]
}
func (content Content) Validate(c context.Context) error {
for _, v := range content {
// Validate MediaType
if err := v.Validate(c); err != nil {
return err
}
}
return nil
}