1package openapi3
2
3import (
4	"context"
5	"errors"
6
7	"github.com/getkin/kin-openapi/jsoninfo"
8)
9
10// Info is specified by OpenAPI/Swagger standard version 3.0.
11type Info struct {
12	ExtensionProps
13	Title          string   `json:"title" yaml:"title"` // Required
14	Description    string   `json:"description,omitempty" yaml:"description,omitempty"`
15	TermsOfService string   `json:"termsOfService,omitempty" yaml:"termsOfService,omitempty"`
16	Contact        *Contact `json:"contact,omitempty" yaml:"contact,omitempty"`
17	License        *License `json:"license,omitempty" yaml:"license,omitempty"`
18	Version        string   `json:"version" yaml:"version"` // Required
19}
20
21func (value *Info) MarshalJSON() ([]byte, error) {
22	return jsoninfo.MarshalStrictStruct(value)
23}
24
25func (value *Info) UnmarshalJSON(data []byte) error {
26	return jsoninfo.UnmarshalStrictStruct(data, value)
27}
28
29func (value *Info) Validate(ctx context.Context) error {
30	if contact := value.Contact; contact != nil {
31		if err := contact.Validate(ctx); err != nil {
32			return err
33		}
34	}
35
36	if license := value.License; license != nil {
37		if err := license.Validate(ctx); err != nil {
38			return err
39		}
40	}
41
42	if value.Version == "" {
43		return errors.New("value of version must be a non-empty string")
44	}
45
46	if value.Title == "" {
47		return errors.New("value of title must be a non-empty string")
48	}
49
50	return nil
51}
52
53// Contact is specified by OpenAPI/Swagger standard version 3.0.
54type Contact struct {
55	ExtensionProps
56	Name  string `json:"name,omitempty" yaml:"name,omitempty"`
57	URL   string `json:"url,omitempty" yaml:"url,omitempty"`
58	Email string `json:"email,omitempty" yaml:"email,omitempty"`
59}
60
61func (value *Contact) MarshalJSON() ([]byte, error) {
62	return jsoninfo.MarshalStrictStruct(value)
63}
64
65func (value *Contact) UnmarshalJSON(data []byte) error {
66	return jsoninfo.UnmarshalStrictStruct(data, value)
67}
68
69func (value *Contact) Validate(ctx context.Context) error {
70	return nil
71}
72
73// License is specified by OpenAPI/Swagger standard version 3.0.
74type License struct {
75	ExtensionProps
76	Name string `json:"name" yaml:"name"` // Required
77	URL  string `json:"url,omitempty" yaml:"url,omitempty"`
78}
79
80func (value *License) MarshalJSON() ([]byte, error) {
81	return jsoninfo.MarshalStrictStruct(value)
82}
83
84func (value *License) UnmarshalJSON(data []byte) error {
85	return jsoninfo.UnmarshalStrictStruct(data, value)
86}
87
88func (value *License) Validate(ctx context.Context) error {
89	if value.Name == "" {
90		return errors.New("value of license name must be a non-empty string")
91	}
92	return nil
93}
94