1 // Copyright 2015 go-swagger maintainers
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "github.com/go-openapi/jsonpointer"
22 "github.com/go-openapi/swag"
29 // SimpleSchema describe swagger simple schemas for parameters and headers
30 type SimpleSchema struct {
31 Type string `json:"type,omitempty"`
32 Format string `json:"format,omitempty"`
33 Items *Items `json:"items,omitempty"`
34 CollectionFormat string `json:"collectionFormat,omitempty"`
35 Default interface{} `json:"default,omitempty"`
36 Example interface{} `json:"example,omitempty"`
39 // TypeName return the type (or format) of a simple schema
40 func (s *SimpleSchema) TypeName() string {
47 // ItemsTypeName yields the type of items in a simple schema array
48 func (s *SimpleSchema) ItemsTypeName() string {
52 return s.Items.TypeName()
55 // CommonValidations describe common JSON-schema validations
56 type CommonValidations struct {
57 Maximum *float64 `json:"maximum,omitempty"`
58 ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
59 Minimum *float64 `json:"minimum,omitempty"`
60 ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
61 MaxLength *int64 `json:"maxLength,omitempty"`
62 MinLength *int64 `json:"minLength,omitempty"`
63 Pattern string `json:"pattern,omitempty"`
64 MaxItems *int64 `json:"maxItems,omitempty"`
65 MinItems *int64 `json:"minItems,omitempty"`
66 UniqueItems bool `json:"uniqueItems,omitempty"`
67 MultipleOf *float64 `json:"multipleOf,omitempty"`
68 Enum []interface{} `json:"enum,omitempty"`
71 // Items a limited subset of JSON-Schema's items object.
72 // It is used by parameter definitions that are not located in "body".
74 // For more information: http://goo.gl/8us55a#items-object
82 // NewItems creates a new instance of items
83 func NewItems() *Items {
87 // Typed a fluent builder method for the type of item
88 func (i *Items) Typed(tpe, format string) *Items {
94 // CollectionOf a fluent builder method for an array item
95 func (i *Items) CollectionOf(items *Items, format string) *Items {
98 i.CollectionFormat = format
102 // WithDefault sets the default value on this item
103 func (i *Items) WithDefault(defaultValue interface{}) *Items {
104 i.Default = defaultValue
108 // WithMaxLength sets a max length value
109 func (i *Items) WithMaxLength(max int64) *Items {
114 // WithMinLength sets a min length value
115 func (i *Items) WithMinLength(min int64) *Items {
120 // WithPattern sets a pattern value
121 func (i *Items) WithPattern(pattern string) *Items {
126 // WithMultipleOf sets a multiple of value
127 func (i *Items) WithMultipleOf(number float64) *Items {
128 i.MultipleOf = &number
132 // WithMaximum sets a maximum number value
133 func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
135 i.ExclusiveMaximum = exclusive
139 // WithMinimum sets a minimum number value
140 func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
142 i.ExclusiveMinimum = exclusive
146 // WithEnum sets a the enum values (replace)
147 func (i *Items) WithEnum(values ...interface{}) *Items {
148 i.Enum = append([]interface{}{}, values...)
152 // WithMaxItems sets the max items
153 func (i *Items) WithMaxItems(size int64) *Items {
158 // WithMinItems sets the min items
159 func (i *Items) WithMinItems(size int64) *Items {
164 // UniqueValues dictates that this array can only have unique items
165 func (i *Items) UniqueValues() *Items {
170 // AllowDuplicates this array can have duplicates
171 func (i *Items) AllowDuplicates() *Items {
172 i.UniqueItems = false
176 // UnmarshalJSON hydrates this items instance with the data from JSON
177 func (i *Items) UnmarshalJSON(data []byte) error {
178 var validations CommonValidations
179 if err := json.Unmarshal(data, &validations); err != nil {
183 if err := json.Unmarshal(data, &ref); err != nil {
186 var simpleSchema SimpleSchema
187 if err := json.Unmarshal(data, &simpleSchema); err != nil {
190 var vendorExtensible VendorExtensible
191 if err := json.Unmarshal(data, &vendorExtensible); err != nil {
195 i.CommonValidations = validations
196 i.SimpleSchema = simpleSchema
197 i.VendorExtensible = vendorExtensible
201 // MarshalJSON converts this items object to JSON
202 func (i Items) MarshalJSON() ([]byte, error) {
203 b1, err := json.Marshal(i.CommonValidations)
207 b2, err := json.Marshal(i.SimpleSchema)
211 b3, err := json.Marshal(i.Refable)
215 b4, err := json.Marshal(i.VendorExtensible)
219 return swag.ConcatJSON(b4, b3, b1, b2), nil
222 // JSONLookup look up a value by the json property name
223 func (i Items) JSONLookup(token string) (interface{}, error) {
224 if token == jsonRef {
228 r, _, err := jsonpointer.GetForToken(i.CommonValidations, token)
229 if err != nil && !strings.HasPrefix(err.Error(), "object has no field") {
235 r, _, err = jsonpointer.GetForToken(i.SimpleSchema, token)