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