blob: 492423ef7fd5f75bbd96e5f947e9bf1063dac6a4 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// 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
25type SimpleSchema struct {
26 Type string `json:"type,omitempty"`
27 Format string `json:"format,omitempty"`
28 Items *Items `json:"items,omitempty"`
29 CollectionFormat string `json:"collectionFormat,omitempty"`
30 Default interface{} `json:"default,omitempty"`
31 Example interface{} `json:"example,omitempty"`
32}
33
34func (s *SimpleSchema) TypeName() string {
35 if s.Format != "" {
36 return s.Format
37 }
38 return s.Type
39}
40
41func (s *SimpleSchema) ItemsTypeName() string {
42 if s.Items == nil {
43 return ""
44 }
45 return s.Items.TypeName()
46}
47
48type CommonValidations struct {
49 Maximum *float64 `json:"maximum,omitempty"`
50 ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
51 Minimum *float64 `json:"minimum,omitempty"`
52 ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
53 MaxLength *int64 `json:"maxLength,omitempty"`
54 MinLength *int64 `json:"minLength,omitempty"`
55 Pattern string `json:"pattern,omitempty"`
56 MaxItems *int64 `json:"maxItems,omitempty"`
57 MinItems *int64 `json:"minItems,omitempty"`
58 UniqueItems bool `json:"uniqueItems,omitempty"`
59 MultipleOf *float64 `json:"multipleOf,omitempty"`
60 Enum []interface{} `json:"enum,omitempty"`
61}
62
63// Items a limited subset of JSON-Schema's items object.
64// It is used by parameter definitions that are not located in "body".
65//
66// For more information: http://goo.gl/8us55a#items-object
67type Items struct {
68 Refable
69 CommonValidations
70 SimpleSchema
71 VendorExtensible
72}
73
74// NewItems creates a new instance of items
75func NewItems() *Items {
76 return &Items{}
77}
78
79// Typed a fluent builder method for the type of item
80func (i *Items) Typed(tpe, format string) *Items {
81 i.Type = tpe
82 i.Format = format
83 return i
84}
85
86// CollectionOf a fluent builder method for an array item
87func (i *Items) CollectionOf(items *Items, format string) *Items {
88 i.Type = "array"
89 i.Items = items
90 i.CollectionFormat = format
91 return i
92}
93
94// WithDefault sets the default value on this item
95func (i *Items) WithDefault(defaultValue interface{}) *Items {
96 i.Default = defaultValue
97 return i
98}
99
100// WithMaxLength sets a max length value
101func (i *Items) WithMaxLength(max int64) *Items {
102 i.MaxLength = &max
103 return i
104}
105
106// WithMinLength sets a min length value
107func (i *Items) WithMinLength(min int64) *Items {
108 i.MinLength = &min
109 return i
110}
111
112// WithPattern sets a pattern value
113func (i *Items) WithPattern(pattern string) *Items {
114 i.Pattern = pattern
115 return i
116}
117
118// WithMultipleOf sets a multiple of value
119func (i *Items) WithMultipleOf(number float64) *Items {
120 i.MultipleOf = &number
121 return i
122}
123
124// WithMaximum sets a maximum number value
125func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
126 i.Maximum = &max
127 i.ExclusiveMaximum = exclusive
128 return i
129}
130
131// WithMinimum sets a minimum number value
132func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
133 i.Minimum = &min
134 i.ExclusiveMinimum = exclusive
135 return i
136}
137
138// WithEnum sets a the enum values (replace)
139func (i *Items) WithEnum(values ...interface{}) *Items {
140 i.Enum = append([]interface{}{}, values...)
141 return i
142}
143
144// WithMaxItems sets the max items
145func (i *Items) WithMaxItems(size int64) *Items {
146 i.MaxItems = &size
147 return i
148}
149
150// WithMinItems sets the min items
151func (i *Items) WithMinItems(size int64) *Items {
152 i.MinItems = &size
153 return i
154}
155
156// UniqueValues dictates that this array can only have unique items
157func (i *Items) UniqueValues() *Items {
158 i.UniqueItems = true
159 return i
160}
161
162// AllowDuplicates this array can have duplicates
163func (i *Items) AllowDuplicates() *Items {
164 i.UniqueItems = false
165 return i
166}
167
168// UnmarshalJSON hydrates this items instance with the data from JSON
169func (i *Items) UnmarshalJSON(data []byte) error {
170 var validations CommonValidations
171 if err := json.Unmarshal(data, &validations); err != nil {
172 return err
173 }
174 var ref Refable
175 if err := json.Unmarshal(data, &ref); err != nil {
176 return err
177 }
178 var simpleSchema SimpleSchema
179 if err := json.Unmarshal(data, &simpleSchema); err != nil {
180 return err
181 }
182 var vendorExtensible VendorExtensible
183 if err := json.Unmarshal(data, &vendorExtensible); err != nil {
184 return err
185 }
186 i.Refable = ref
187 i.CommonValidations = validations
188 i.SimpleSchema = simpleSchema
189 i.VendorExtensible = vendorExtensible
190 return nil
191}
192
193// MarshalJSON converts this items object to JSON
194func (i Items) MarshalJSON() ([]byte, error) {
195 b1, err := json.Marshal(i.CommonValidations)
196 if err != nil {
197 return nil, err
198 }
199 b2, err := json.Marshal(i.SimpleSchema)
200 if err != nil {
201 return nil, err
202 }
203 b3, err := json.Marshal(i.Refable)
204 if err != nil {
205 return nil, err
206 }
207 b4, err := json.Marshal(i.VendorExtensible)
208 if err != nil {
209 return nil, err
210 }
211 return swag.ConcatJSON(b4, b3, b1, b2), nil
212}
213
214// JSONLookup look up a value by the json property name
215func (p Items) JSONLookup(token string) (interface{}, error) {
216 if token == "$ref" {
217 return &p.Ref, nil
218 }
219
220 r, _, err := jsonpointer.GetForToken(p.CommonValidations, token)
221 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
222 return nil, err
223 }
224 if r != nil {
225 return r, nil
226 }
227 r, _, err = jsonpointer.GetForToken(p.SimpleSchema, token)
228 return r, err
229}