blob: cf4298971664e721a238116d8a5238d7c000fca4 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package spec
16
17import (
18 "encoding/json"
19 "strings"
20
21 "github.com/go-openapi/jsonpointer"
22 "github.com/go-openapi/swag"
23)
24
25// SimpleSchema describe swagger simple schemas for parameters and headers
26type SimpleSchema struct {
27 Type string `json:"type,omitempty"`
28 Format string `json:"format,omitempty"`
29 Items *Items `json:"items,omitempty"`
30 CollectionFormat string `json:"collectionFormat,omitempty"`
31 Default interface{} `json:"default,omitempty"`
32 Example interface{} `json:"example,omitempty"`
33}
34
35// TypeName return the type (or format) of a simple schema
36func (s *SimpleSchema) TypeName() string {
37 if s.Format != "" {
38 return s.Format
39 }
40 return s.Type
41}
42
43// ItemsTypeName yields the type of items in a simple schema array
44func (s *SimpleSchema) ItemsTypeName() string {
45 if s.Items == nil {
46 return ""
47 }
48 return s.Items.TypeName()
49}
50
51// CommonValidations describe common JSON-schema validations
52type CommonValidations struct {
53 Maximum *float64 `json:"maximum,omitempty"`
54 ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
55 Minimum *float64 `json:"minimum,omitempty"`
56 ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
57 MaxLength *int64 `json:"maxLength,omitempty"`
58 MinLength *int64 `json:"minLength,omitempty"`
59 Pattern string `json:"pattern,omitempty"`
60 MaxItems *int64 `json:"maxItems,omitempty"`
61 MinItems *int64 `json:"minItems,omitempty"`
62 UniqueItems bool `json:"uniqueItems,omitempty"`
63 MultipleOf *float64 `json:"multipleOf,omitempty"`
64 Enum []interface{} `json:"enum,omitempty"`
65}
66
67// Items a limited subset of JSON-Schema's items object.
68// It is used by parameter definitions that are not located in "body".
69//
70// For more information: http://goo.gl/8us55a#items-object
71type Items struct {
72 Refable
73 CommonValidations
74 SimpleSchema
75 VendorExtensible
76}
77
78// NewItems creates a new instance of items
79func NewItems() *Items {
80 return &Items{}
81}
82
83// Typed a fluent builder method for the type of item
84func (i *Items) Typed(tpe, format string) *Items {
85 i.Type = tpe
86 i.Format = format
87 return i
88}
89
90// CollectionOf a fluent builder method for an array item
91func (i *Items) CollectionOf(items *Items, format string) *Items {
92 i.Type = "array"
93 i.Items = items
94 i.CollectionFormat = format
95 return i
96}
97
98// WithDefault sets the default value on this item
99func (i *Items) WithDefault(defaultValue interface{}) *Items {
100 i.Default = defaultValue
101 return i
102}
103
104// WithMaxLength sets a max length value
105func (i *Items) WithMaxLength(max int64) *Items {
106 i.MaxLength = &max
107 return i
108}
109
110// WithMinLength sets a min length value
111func (i *Items) WithMinLength(min int64) *Items {
112 i.MinLength = &min
113 return i
114}
115
116// WithPattern sets a pattern value
117func (i *Items) WithPattern(pattern string) *Items {
118 i.Pattern = pattern
119 return i
120}
121
122// WithMultipleOf sets a multiple of value
123func (i *Items) WithMultipleOf(number float64) *Items {
124 i.MultipleOf = &number
125 return i
126}
127
128// WithMaximum sets a maximum number value
129func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
130 i.Maximum = &max
131 i.ExclusiveMaximum = exclusive
132 return i
133}
134
135// WithMinimum sets a minimum number value
136func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
137 i.Minimum = &min
138 i.ExclusiveMinimum = exclusive
139 return i
140}
141
142// WithEnum sets a the enum values (replace)
143func (i *Items) WithEnum(values ...interface{}) *Items {
144 i.Enum = append([]interface{}{}, values...)
145 return i
146}
147
148// WithMaxItems sets the max items
149func (i *Items) WithMaxItems(size int64) *Items {
150 i.MaxItems = &size
151 return i
152}
153
154// WithMinItems sets the min items
155func (i *Items) WithMinItems(size int64) *Items {
156 i.MinItems = &size
157 return i
158}
159
160// UniqueValues dictates that this array can only have unique items
161func (i *Items) UniqueValues() *Items {
162 i.UniqueItems = true
163 return i
164}
165
166// AllowDuplicates this array can have duplicates
167func (i *Items) AllowDuplicates() *Items {
168 i.UniqueItems = false
169 return i
170}
171
172// UnmarshalJSON hydrates this items instance with the data from JSON
173func (i *Items) UnmarshalJSON(data []byte) error {
174 var validations CommonValidations
175 if err := json.Unmarshal(data, &validations); err != nil {
176 return err
177 }
178 var ref Refable
179 if err := json.Unmarshal(data, &ref); err != nil {
180 return err
181 }
182 var simpleSchema SimpleSchema
183 if err := json.Unmarshal(data, &simpleSchema); err != nil {
184 return err
185 }
186 var vendorExtensible VendorExtensible
187 if err := json.Unmarshal(data, &vendorExtensible); err != nil {
188 return err
189 }
190 i.Refable = ref
191 i.CommonValidations = validations
192 i.SimpleSchema = simpleSchema
193 i.VendorExtensible = vendorExtensible
194 return nil
195}
196
197// MarshalJSON converts this items object to JSON
198func (i Items) MarshalJSON() ([]byte, error) {
199 b1, err := json.Marshal(i.CommonValidations)
200 if err != nil {
201 return nil, err
202 }
203 b2, err := json.Marshal(i.SimpleSchema)
204 if err != nil {
205 return nil, err
206 }
207 b3, err := json.Marshal(i.Refable)
208 if err != nil {
209 return nil, err
210 }
211 b4, err := json.Marshal(i.VendorExtensible)
212 if err != nil {
213 return nil, err
214 }
215 return swag.ConcatJSON(b4, b3, b1, b2), nil
216}
217
218// JSONLookup look up a value by the json property name
219func (i Items) JSONLookup(token string) (interface{}, error) {
220 if token == "$ref" {
221 return &i.Ref, nil
222 }
223
224 r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)
225 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
226 return nil, err
227 }
228 if r != nil {
229 return r, nil
230 }
231 r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)
232 return r, err
233}