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