github.com/kaisawind/go-swagger@v0.19.0/generator/templates/server/parameter.gotmpl (about) 1 {{ define "bindprimitiveparam" }}{{/* an empty test definition to test template repo dependencies resolution - DO NOT CHANGE THIS */}} 2 {{ end }} 3 4 {{ define "bodyvalidator" }} 5 {{- if .HasModelBodyParams }} 6 // validate body object{{/* delegate validation to model object */}} 7 if err := body.Validate(route.Formats); err != nil { 8 res = append(res, err) 9 } 10 11 if len(res) == 0 { 12 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 13 } 14 {{- else if and .HasSimpleBodyParams .HasModelBodyItems }} 15 16 {{- if or .Schema.HasSliceValidations .Schema.Items.HasValidations }} 17 // validate array of body objects 18 {{- end }} 19 {{- if .Schema.HasSliceValidations }} 20 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 21 {{ template "sliceparamvalidator" . }} 22 {{- end }} 23 24 {{- if and .Schema.Items.HasValidations (not (or .Schema.Items.IsInterface .Schema.Items.IsStream)) }} 25 for {{ .IndexVar }} := range body { 26 {{- if .Schema.Items.IsNullable }} 27 if body[{{ .IndexVar }}] == nil { 28 {{- if .Schema.Items.Required }} 29 res = append(res, errors.Required({{ .Child.Path }}, {{ printf "%q" .Child.Location }})) 30 break 31 {{- else }} 32 continue 33 {{- end }} 34 } 35 {{- end }} 36 if err := body[{{ .IndexVar }}].Validate(route.Formats); err != nil { 37 res = append(res, err) 38 break 39 } 40 } 41 42 {{- if not .Schema.HasSliceValidations }} 43 if len(res) == 0 { 44 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 45 } 46 {{- end }} 47 {{- else }} 48 // no validation for items in this slice 49 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 50 {{- end }} 51 52 {{- else if and .HasSimpleBodyParams .HasModelBodyMap }} 53 54 {{- if and .Schema.HasValidations (not (or .Schema.AdditionalProperties.IsInterface .Schema.AdditionalProperties.IsStream)) }} 55 // validate map of body objects 56 for {{ .KeyVar }} := range body { 57 {{- if .Schema.AdditionalProperties.Required }} 58 if err := validate.Required({{ if .Child.Path }}{{ .Child.Path }}{{ else }}""{{ end }}, {{ printf "%q" .Child.Location }}, {{ if not .IsAnonymous }}{{ .Schema.GoType }}({{ end }}body[{{ .KeyVar }}]{{ if not .IsAnonymous }}){{ end }}); err != nil { 59 return err 60 } 61 {{- end }} 62 {{- if and .Schema.AdditionalProperties.IsNullable (not .IsMapNullOverride) }} 63 if body[{{ .KeyVar }}] == nil { 64 {{- if .Schema.AdditionalProperties.Required }} 65 res = append(res, errors.Required({{ .Path }}, {{ printf "%q" .Location }})) 66 break 67 {{- else }} 68 continue 69 {{- end }} 70 } 71 {{- end }} 72 if val , ok :=body[{{ .KeyVar }}]; ok { 73 {{- if and .IsNullable (not .IsMapNullOverride) }} 74 if val != nil { 75 {{- end }} 76 if err := val.Validate(route.Formats); err != nil { 77 res = append(res, err) 78 break 79 } 80 {{- if and .IsNullable (not .IsMapNullOverride) }} 81 } 82 {{- end }} 83 } 84 } 85 86 if len(res) == 0 { 87 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 88 } 89 {{- else }} 90 // no validation for this map 91 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 92 {{- end }} 93 {{- else if .HasSimpleBodyParams }} 94 {{- if and (not .IsArray) (not .IsMap) .Schema.HasValidations }} 95 // validate inline body 96 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 97 if err := {{ .ReceiverName }}.validate{{ pascalize .ID }}Body(route.Formats); err != nil { 98 res = append(res, err) 99 } 100 {{- else if and (or .IsArray .IsMap) .Schema.HasValidations }} 101 // validate inline body {{ if .IsArray }}array{{ else }}map{{ end }} 102 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 103 if err := {{ .ReceiverName }}.validate{{ pascalize .ID }}Body(route.Formats); err != nil { 104 res = append(res, err) 105 } 106 {{- else }} 107 // no validation required on inline body 108 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 109 {{- end}} 110 {{- else }} 111 {{- if .IsInterface }} 112 // no validation on generic interface 113 {{ .ReceiverName }}.{{ pascalize .Name }} = {{ if and (not .Schema.IsBaseType) .IsNullable }}&{{ end }}body 114 {{- end }} 115 {{- end }} 116 {{- end }} 117 118 {{ define "sliceparamvalidator"}} 119 {{ if or .MinItems .MaxItems }} 120 {{ camelize .Name }}Size := int64(len({{ if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}*{{ end }}{{ if and .Child (not (hasPrefix .ValueExpression "o.")) }}{{ .Child.ValueExpression }}C{{ else }}{{ .ValueExpression }}{{ end }})) 121 {{ end }} 122 {{ if .MinItems }} 123 // {{ .ItemsDepth }}minItems: {{ .MinItems }} 124 if err := validate.MinItems({{ .Path }}, {{ printf "%q" .Location }}, {{ camelize .Name }}Size, {{ .MinItems }}); err != nil { 125 return err 126 } 127 {{ end }} 128 {{ if .MaxItems }} 129 // {{ .ItemsDepth }}maxItems: {{ .MaxItems }} 130 if err := validate.MaxItems({{ .Path }}, {{ printf "%q" .Location }}, {{ camelize .Name }}Size, {{.MaxItems}}); err != nil { 131 return err 132 } 133 {{ end }} 134 {{ if .UniqueItems }} 135 // {{ .ItemsDepth }}uniqueItems: true 136 if err := validate.UniqueItems({{ .Path }}, {{ printf "%q" .Location }}, {{ if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}*{{ end }}{{ if and .Child (not ( hasPrefix .ValueExpression "o." )) }}{{ .Child.ValueExpression }}C{{ else }}{{ .ValueExpression }}{{ end }}); err != nil { 137 return err 138 } 139 {{ end }} 140 {{ if .Enum }} 141 // {{ .ItemsDepth }}Enum: {{ .Enum }} 142 if err := validate.Enum( 143 {{- .Path }}, {{ printf "%q" .Location }}, 144 {{- if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}*{{ end -}} 145 {{- if .Child -}} 146 {{- if not ( hasPrefix .ValueExpression "o." ) -}} 147 {{- .Child.ValueExpression }}C{{- if .IsCustomFormatter }}.String(){{ end -}} 148 {{- else -}} 149 {{- .ValueExpression -}}{{- if .Child.IsCustomFormatter }}.String(){{ end -}} 150 {{- end -}} 151 {{- end -}}, 152 {{- printf "%#v" .Enum -}} 153 ); err != nil { 154 return err 155 } 156 {{ end }} 157 {{ end }} 158 159 {{- define "childvalidator" }} 160 {{- if .Converter }} 161 {{- if ne .SwaggerFormat "" }} 162 // {{ .ItemsDepth }}Format: {{ printf "%q" .SwaggerFormat }} 163 {{- end }} 164 {{ varname .ValueExpression }}, err := {{ .Converter }}({{ varname .ValueExpression }}V) 165 if err != nil { 166 return errors.InvalidType({{ .Path }}, {{ printf "%q" .Location }}, "{{ .GoType }}", {{ varname .ValueExpression }}) 167 } 168 {{- else if and .IsCustomFormatter (not .SkipParse) }}{{/* parsing is skipped for simple body items */}} 169 // {{ .ItemsDepth }}Format: {{ printf "%q" .SwaggerFormat }} 170 value, err := formats.Parse({{ printf "%q" .SwaggerFormat }},{{ varname .ValueExpression }}V) 171 if err != nil { 172 return errors.InvalidType({{ .Path }}, {{ printf "%q" .Location }}, "{{ .GoType }}", value) 173 } 174 {{ varname .ValueExpression }} := *(value.(*{{.GoType}})) 175 {{- else if and .IsComplexObject .HasValidations }}{{/* dedicated to nested body params */}} 176 {{ varname .ValueExpression }} := {{ varname .ValueExpression }}V 177 if err := {{ .ValueExpression }}.Validate(formats) ; err != nil { 178 if ve, ok := err.(*errors.Validation); ok { 179 return ve.ValidateName({{ .Path }}) 180 } 181 return err 182 } 183 {{- else }} 184 {{ varname .ValueExpression }} := {{ varname .ValueExpression }}V 185 {{- end }} 186 {{ template "propertyparamvalidator" . }} 187 {{- end }} 188 189 {{- define "mapparamvalidator" }} 190 {{- if and .Child.HasValidations (not (or .Child.IsInterface .Child.IsStream)) }} 191 192 // validations for map 193 {{- else }} 194 195 // map has no validations: copying all elements 196 {{- end }} 197 {{ varname .Child.ValueExpression }}R := make({{ .GoType }},len({{ .Child.ValueExpression }}C)) 198 for {{ .KeyVar }}, {{ .Child.ValueExpression }}V := range {{ .Child.ValueExpression}}C { 199 {{- if .Child.IsArray }} 200 {{ .Child.Child.ValueExpression }}C := {{ varname .Child.ValueExpression }}V 201 {{- if .Child.HasSliceValidations }} 202 {{- template "sliceparamvalidator" .Child }} 203 {{- end }} 204 {{- template "sliceparambinder" .Child }} 205 {{- else if .Child.IsMap }} 206 {{ .Child.Child.ValueExpression }}C := {{ varname .Child.ValueExpression }}V 207 {{ template "mapparamvalidator" .Child }} 208 {{- else }} 209 {{- if and .Child.IsNullable }} 210 if {{ varname .Child.ValueExpression }}V == nil { 211 {{- if .Child.Required }} 212 return errors.Required({{ .Child.Path }}, {{ printf "%q" .Child.Location }}) 213 {{- else }} 214 continue 215 {{- end }} 216 } 217 {{- end }} 218 {{- template "childvalidator" .Child }} 219 {{- end }} 220 {{ varname .Child.ValueExpression }}R[{{.KeyVar}}] = {{ varname .Child.ValueExpression }}{{ if or .Child.IsArray .Child.IsMap}}IR{{end}} 221 } 222 {{- end }} 223 224 {{- define "propertyparamvalidator" }} 225 {{- if .IsPrimitive }} 226 {{ template "validationPrimitive" . }} 227 {{- end }} 228 {{- if and .IsCustomFormatter (not .IsStream) }} 229 {{- if ne .SwaggerFormat "byte" }} 230 if err := validate.FormatOf({{.Path}}, "{{.Location}}", "{{.SwaggerFormat}}", {{ .ValueExpression}}.String(), formats); err != nil { 231 return err 232 } 233 {{- else }} 234 // Format "byte" (base64 string) is already validated when parsed 235 {{- end }} 236 {{- end }} 237 {{- if .IsArray }}{{/* slice validations */}} 238 {{ template "sliceparamvalidator" . }} 239 {{- else if .IsMap }} 240 {{ .Child.ValueExpression }}C := {{ varname .Child.ValueExpression }}V 241 {{ template "mapparamvalidator" . }} 242 {{- end }} 243 {{- end }} 244 245 {{ define "sliceparambinder" }} 246 var {{ varname .Child.ValueExpression }}R {{ .GoType }} 247 for {{ if or .Child.HasValidations .Child.Converter .Child.IsCustomFormatter }}{{ .IndexVar }}{{ else }}_{{ end }}, {{ varname .Child.ValueExpression }}V := range {{ varname .Child.ValueExpression }}C { 248 {{- if .Child.IsArray }}{{/* recursive resolution of arrays in params */}} 249 {{- if not .Child.SkipParse }} 250 // {{ .Child.ItemsDepth }}CollectionFormat: {{ .Child.CollectionFormat }} 251 {{- end }} 252 {{ .Child.Child.ValueExpression }}C := {{ if .Child.SkipParse }}{{ varname .Child.ValueExpression }}V{{ else }}swag.SplitByFormat({{ varname .Child.ValueExpression }}V, {{ printf "%q" .Child.CollectionFormat }}){{ end }} 253 {{- if .Child.HasSliceValidations }} 254 {{- template "sliceparamvalidator" .Child }} 255 {{- end }} 256 if len({{ varname .Child.Child.ValueExpression }}C) > 0 { 257 {{ template "sliceparambinder" .Child }} 258 {{ varname .Child.ValueExpression }}R = append({{ varname .Child.ValueExpression }}R, {{ varname .Child.ValueExpression }}{{ if or .Child.IsArray .Child.IsMap }}IR{{end}}) 259 } 260 {{- else if .Child.IsMap }}{{/* simple map in items (possible with body params)*/}} 261 {{ .Child.Child.ValueExpression }}C := {{ varname .Child.ValueExpression }}V 262 {{- template "mapparamvalidator" .Child }} 263 {{ varname .Child.ValueExpression }}R = append({{ varname .Child.ValueExpression }}R, {{ varname .Child.ValueExpression }}{{ if or .Child.IsArray .Child.IsMap }}IR{{end}}) 264 {{- else }}{{/* non-array && non-map type in items */}} 265 {{- if and .Child.IsNullable (not .IsMapNullOverride) }} 266 if {{ varname .Child.ValueExpression }}V == nil { 267 {{- if .Child.Required }} 268 return errors.Required({{ .Child.Path }}, {{ printf "%q" .Child.Location }}) 269 {{- else }} 270 continue 271 {{- end }} 272 } 273 {{- end }} 274 {{- template "childvalidator" .Child }} 275 {{ varname .Child.ValueExpression }}R = append({{ varname .Child.ValueExpression }}R, {{ varname .Child.ValueExpression }}{{ if or .Child.IsArray .Child.IsMap }}IR{{end}}) 276 {{- end }} 277 } 278 {{ end }} 279 // Code generated by go-swagger; DO NOT EDIT. 280 281 282 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 283 284 285 package {{ .Package }} 286 287 // This file was generated by the swagger tool. 288 // Editing this file might prove futile when you re-run the swagger generate command 289 290 import ( 291 "net/http" 292 293 "github.com/go-openapi/errors" 294 "github.com/go-openapi/validate" 295 "github.com/go-openapi/runtime" 296 "github.com/go-openapi/runtime/security" 297 "github.com/go-openapi/runtime/middleware" 298 "github.com/go-openapi/swag" 299 300 strfmt "github.com/go-openapi/strfmt" 301 302 {{ range .DefaultImports }}{{ printf "%q" .}} 303 {{ end }} 304 {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }} 305 {{ end }} 306 ) 307 308 // New{{ pascalize .Name }}Params creates a new {{ pascalize .Name }}Params object 309 {{ if .Params.HasSomeDefaults -}} 310 // with the default values initialized.{{ else -}} 311 // no default values defined in spec. 312 {{- end }} 313 func New{{ pascalize .Name }}Params() {{ pascalize .Name }}Params { 314 {{ if .Params.HasSomeDefaults }} 315 var ( 316 // initialize parameters with default values 317 {{ range .Params }} 318 {{ if .HasDefault -}} 319 {{ if not .IsFileParam }}{{ varname .ID}}Default = 320 {{- if and .IsPrimitive .IsCustomFormatter (not (stringContains .Zero "(\"" )) }}{{ .Zero }}{{/* strfmt type initializer requires UnmarshalText(), e.g. Date, Datetime, Duration */}} 321 {{- else if and .IsPrimitive .IsCustomFormatter (stringContains .Zero "(\"" ) }}{{.GoType}}({{- printf "%#v" .Default }}){{/* strfmt type initializer takes string */}} 322 {{- else if and .IsPrimitive (not .IsCustomFormatter) -}}{{.GoType}}({{- printf "%#v" .Default }}){{/* regular go primitive type initializer */}} 323 {{- else if .IsArray -}}{{- /* Do not initialize from possible defaults in nested arrays */ -}} 324 {{- if and .Child.IsPrimitive .Child.IsCustomFormatter }}{{ .Zero }}{{/* initialization strategy with UnmarshalText() */}} 325 {{- else if .Child.IsArray -}}{{ .Zero }}{{/* initialization strategy with json.Unmarshal() */}} 326 {{- else if and .Child.IsPrimitive (not .Child.IsCustomFormatter) -}}{{.GoType}}{{- goSliceInitializer .Default }}{{/* regular go primitive type initializer: simple slice initializer */}} 327 {{- else }}{{ printf "%#v" .Default }}{{/* all other cases (e.g. schema) [should not occur] */}} 328 {{- end }} 329 {{- else }}{{ printf "%#v" .Default }}{{/* case .Schema */}} 330 {{- end }} 331 {{- end }} 332 {{- end }} 333 {{- end }} 334 ) 335 336 {{ range .Params }}{{ if .HasDefault -}}{{- /* carry on UnmarshalText initialization strategy */ -}} 337 {{ if and .IsPrimitive .IsCustomFormatter (not (stringContains .Zero "(\"")) }}{{ varname .ID}}Default.UnmarshalText([]byte({{ printf "%q" .Default }})) 338 {{ else if .IsArray -}} 339 {{ if or ( and .Child.IsPrimitive .Child.IsCustomFormatter ) .Child.IsArray -}} 340 if err := json.Unmarshal([]byte(`{{printf "%s" (json .Default)}}`), &{{ varname .ID }}Default); err != nil { 341 // panics if specification is invalid 342 msg := fmt.Sprintf("invalid default value for parameter {{ varname .ID }}: %v",err) 343 panic(msg) 344 } 345 {{ end -}} 346 {{- end }} 347 {{ end -}} 348 {{- end }} 349 {{ end }} 350 return {{ pascalize .Name }}Params{ {{ range .Params }}{{ if .HasDefault }} 351 {{ pascalize .ID}}: {{ if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}&{{ end }}{{ varname .ID }}Default, 352 {{ end }}{{ end }} } 353 } 354 355 // {{ pascalize .Name }}Params contains all the bound params for the {{ humanize .Name }} operation 356 // typically these are obtained from a http.Request 357 // 358 // swagger:parameters {{ .Name }} 359 type {{ pascalize .Name }}Params struct { 360 361 // HTTP Request Object 362 HTTPRequest *http.Request `json:"-"` 363 364 {{ range .Params }}/*{{ if .Description }}{{ blockcomment .Description }}{{ end }}{{ if .Required }} 365 Required: true{{ end }}{{ if .Maximum }} 366 Maximum: {{ if .ExclusiveMaximum }}< {{ end }}{{ .Maximum }}{{ end }}{{ if .Minimum }} 367 Minimum: {{ if .ExclusiveMinimum }}> {{ end }}{{ .Minimum }}{{ end }}{{ if .MultipleOf }} 368 Multiple Of: {{ .MultipleOf }}{{ end }}{{ if .MaxLength }} 369 Max Length: {{ .MaxLength }}{{ end }}{{ if .MinLength }} 370 Min Length: {{ .MinLength }}{{ end }}{{ if .Pattern }} 371 Pattern: {{ .Pattern }}{{ end }}{{ if .MaxItems }} 372 Max Items: {{ .MaxItems }}{{ end }}{{ if .MinItems }} 373 Min Items: {{ .MinItems }}{{ end }}{{ if .UniqueItems }} 374 Unique: true{{ end }}{{ if .Location }} 375 In: {{ .Location }}{{ end }}{{ if .CollectionFormat }} 376 Collection Format: {{ .CollectionFormat }}{{ end }}{{ if .HasDefault }} 377 Default: {{ printf "%#v" .Default }}{{ end }} 378 */ 379 {{ if not .Schema }}{{ pascalize .ID }} {{ if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}*{{ end }}{{.GoType}}{{ else }}{{ pascalize .Name }} {{ if and (not .Schema.IsBaseType) .IsNullable (not .Schema.IsStream) }}*{{ end }}{{.GoType}}{{ end }} 380 {{ end}} 381 } 382 383 // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface 384 // for simple values it will use straight method calls. 385 // 386 // To ensure default values, the struct must have been initialized with New{{ pascalize .Name }}Params() beforehand. 387 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { 388 var res []error 389 390 {{ .ReceiverName }}.HTTPRequest = r 391 392 {{ if .HasQueryParams }}qs := runtime.Values(r.URL.Query()){{ end }} 393 394 {{ if .HasFormParams }}if err := r.ParseMultipartForm(32 << 20); err != nil { 395 if err != http.ErrNotMultipart { 396 return errors.New(400,"%v",err) 397 } else if err := r.ParseForm(); err != nil { 398 return errors.New(400,"%v",err) 399 } 400 }{{ if .HasFormValueParams }} 401 fds := runtime.Values(r.Form) 402 {{ end }}{{ end }} 403 404 {{ range .Params }} 405 {{ if not .IsArray -}} 406 {{ if .IsQueryParam }}q{{ pascalize .Name }}, qhk{{ pascalize .Name }}, _ := qs.GetOK({{ .Path }}) 407 if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(q{{ pascalize .Name }}, qhk{{ pascalize .Name }}, route.Formats); err != nil { 408 res = append(res, err) 409 } 410 {{ else if .IsPathParam }}r{{ pascalize .Name }}, rhk{{ pascalize .Name }}, _ := route.Params.GetOK({{ .Path }}) 411 if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(r{{ pascalize .Name }}, rhk{{ pascalize .Name }}, route.Formats); err != nil { 412 res = append(res, err) 413 } 414 {{ else if .IsHeaderParam }}if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(r.Header[http.CanonicalHeaderKey({{ .Path }})], true, route.Formats); err != nil { 415 res = append(res, err) 416 } 417 {{ else if .IsFormParam -}} 418 {{if .IsFileParam }}{{ camelize .Name }}, {{ camelize .Name }}Header, err := r.FormFile({{ .Path }}) 419 if err != nil {{ if .IsNullable }}&& err != http.ErrMissingFile{{ end }}{ 420 res = append(res, errors.New(400, "reading file %q failed: %v", {{ printf "%q" (camelize .Name) }}, err)) 421 {{ if .IsNullable }}} else if err == http.ErrMissingFile { 422 // no-op for missing but optional file parameter 423 {{ end }}} else if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}({{ camelize .Name }}, {{ camelize .Name }}Header); err != nil { 424 {{ if .Required }}// Required: true{{ printf "\n" }}{{end }}res = append(res, err) 425 } else { 426 {{ .ReceiverName }}.{{ pascalize .Name }} = &runtime.File{Data: {{ camelize .Name }}, Header: {{ camelize .Name }}Header} 427 } 428 {{ else }}fd{{ pascalize .Name }}, fdhk{{ pascalize .Name }}, _ := fds.GetOK({{ .Path }}) 429 if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(fd{{ pascalize .Name }}, fdhk{{ pascalize .Name }}, route.Formats); err != nil { 430 res = append(res, err) 431 } 432 {{ end }}{{/* end .FileParam */}} 433 {{ end }}{{/* end not .Array */}} 434 {{ else if .IsArray -}} 435 {{ if .IsQueryParam }}q{{ pascalize .Name }}, qhk{{ pascalize .Name }}, _ := qs.GetOK({{ .Path }}) 436 if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(q{{ pascalize .Name }}, qhk{{ pascalize .Name }}, route.Formats); err != nil { 437 res = append(res, err) 438 } 439 {{ else if .IsPathParam }}r{{ pascalize .Name }}, rhk{{ pascalize .Name }}, _ := route.Params.GetOK({{ .Path }}) 440 if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(r{{ pascalize .Name }}, rhk{{ pascalize .Name }}, route.Formats); err != nil { 441 res = append(res, err) 442 } 443 {{ else if .IsHeaderParam }}if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(r.Header[http.CanonicalHeaderKey({{ .Path }})], true, route.Formats); err != nil { 444 res = append(res, err) 445 } 446 {{ else if and .IsFormParam }}fd{{ pascalize .Name }}, fdhk{{ pascalize .Name }}, _ := fds.GetOK({{ .Path }}) 447 if err := {{ .ReceiverName }}.bind{{ pascalize .ID }}(fd{{ pascalize .Name }}, fdhk{{ pascalize .Name }}, route.Formats); err != nil { 448 res = append(res, err) 449 } 450 {{ end }}{{ end }} 451 {{ if and .IsBodyParam .Schema -}} 452 if runtime.HasBody(r) { 453 {{- if .Schema.IsStream }} 454 {{ .ReceiverName }}.{{ pascalize .Name }} = r.Body 455 {{- else }} 456 defer r.Body.Close() 457 {{- if and .Schema.IsBaseType .Schema.IsExported }} 458 body, err := {{ toPackageName .ModelsPackage }}.Unmarshal{{ stripPackage .GoType (toPackageName .ModelsPackage) }}{{ if .IsArray }}Slice{{ end }}(r.Body, route.Consumer) 459 if err != nil { 460 {{- if .Required }} 461 if err == io.EOF { 462 err = errors.Required({{ .Path }}, {{ printf "%q" .Location }}) 463 } 464 {{- end }} 465 res = append(res, err) 466 {{- else }} 467 var body {{ .GoType }} 468 if err := route.Consumer.Consume(r.Body, &body); err != nil { 469 {{- if .Required }} 470 if err == io.EOF { 471 res = append(res, errors.Required({{ printf "%q" (camelize .Name) }}, {{ printf "%q" .Location }})) 472 } else { 473 {{- end }} 474 res = append(res, errors.NewParseError({{ printf "%q" (camelize .Name) }}, {{ printf "%q" .Location }}, "", err)) 475 {{- if .Required }} 476 } 477 {{- end -}} 478 {{- end }} 479 } else { 480 {{- template "bodyvalidator" . }} 481 } 482 {{- end }} 483 } 484 {{- if .Required }} else { 485 res = append(res, errors.Required({{ printf "%q" (camelize .Name) }}, {{ printf "%q" .Location }})) 486 } 487 {{- end }} 488 {{- end }} 489 {{- end }} 490 if len(res) > 0 { 491 return errors.CompositeValidationError(res...) 492 } 493 return nil 494 } 495 496 {{- $className := (pascalize .Name) }} 497 {{ range .Params }} 498 {{- if .IsFileParam }} 499 // bind{{ pascalize .ID }} binds file parameter {{ .ID }}. 500 // 501 // The only supported validations on files are MinLength and MaxLength 502 func ({{ .ReceiverName }} *{{ $className }}Params) bind{{ pascalize .ID }}(file multipart.File, header *multipart.FileHeader) error { 503 {{- if or .MinLength .MaxLength }} 504 size, _ := file.Seek(0, io.SeekEnd) 505 file.Seek(0, io.SeekStart) 506 {{- end }} 507 {{- if .MinLength}} 508 if size < {{.MinLength}} { 509 return errors.ExceedsMinimum({{ .Path }}, {{ printf "%q" .Location }}, {{ .MinLength }}, false) 510 } 511 {{- end }} 512 {{- if .MaxLength}} 513 if size > {{.MaxLength}} { 514 return errors.ExceedsMaximum({{ .Path }}, {{ printf "%q" .Location }}, {{ .MaxLength }}, false) 515 } 516 {{- end }} 517 return nil 518 } 519 {{- else if not .IsBodyParam }} 520 {{- if or .IsPrimitive .IsCustomFormatter }} 521 // bind{{ pascalize .ID }} binds and validates parameter {{ .ID }} from {{ .Location }}. 522 func ({{ .ReceiverName }} *{{ $className }}Params) bind{{ pascalize .ID }}(rawData []string, hasKey bool, formats strfmt.Registry) error { 523 {{- if and (not .IsPathParam) .Required }} 524 if !hasKey { 525 return errors.Required({{ .Path }}, {{ printf "%q" .Location }}) 526 } 527 {{- end }} 528 var raw string 529 if len(rawData) > 0 { 530 raw = rawData[len(rawData)-1] 531 } 532 533 // Required: {{ .Required }} 534 {{ if .IsQueryParam }}// AllowEmptyValue: {{ .AllowEmptyValue }}{{ end }}{{ if .IsPathParam }}// Parameter is provided by construction from the route{{ end }} 535 {{ if and (not .IsPathParam) .Required (not .AllowEmptyValue) -}} 536 if err := validate.RequiredString({{ .Path }}, {{ printf "%q" .Location }}, raw); err != nil { 537 return err 538 } 539 {{ else if and ( not .IsPathParam ) (or (not .Required) .AllowEmptyValue) -}} 540 if raw == "" { // empty values pass all other validations{{ if .HasDefault }} 541 // Default values have been previously initialized by New{{ $className }}Params(){{ end }} 542 return nil 543 } 544 {{ end }} 545 {{ if .Converter }}value, err := {{ .Converter }}(raw) 546 if err != nil { 547 return errors.InvalidType({{ .Path }}, {{ printf "%q" .Location }}, {{ printf "%q" .GoType }}, raw) 548 } 549 {{ .ValueExpression }} = {{ if .IsNullable }}&{{ end }}value 550 {{ else if .IsCustomFormatter }}// Format: {{ .SwaggerFormat }} 551 value, err := formats.Parse({{ printf "%q" .SwaggerFormat }}, raw) 552 if err != nil { 553 return errors.InvalidType({{ .Path }}, {{ printf "%q" .Location }}, {{ printf "%q" .GoType }}, raw) 554 } 555 {{ .ValueExpression }} = {{ if and (not .IsArray) (not .HasDiscriminator) (not .IsFileParam) (not .IsStream) (not .IsNullable) }}*{{ end }}(value.(*{{ .GoType }})) 556 {{else}}{{ .ValueExpression }} = {{ if .IsNullable }}&{{ end }}raw 557 {{ end }} 558 {{if .HasValidations }}if err := {{ .ReceiverName }}.validate{{ pascalize .ID }}(formats); err != nil { 559 return err 560 } 561 {{ end }} 562 return nil 563 } 564 {{- else if .IsArray }} 565 // bind{{ pascalize .ID }} binds and validates array parameter {{ .ID }} from {{ .Location }}. 566 // 567 // Arrays are parsed according to CollectionFormat: "{{ .CollectionFormat }}" (defaults to "csv" when empty). 568 func ({{ .ReceiverName }} *{{ $className }}Params) bind{{ pascalize .ID }}(rawData []string, hasKey bool, formats strfmt.Registry) error { 569 {{if .Required }}if !hasKey { 570 return errors.Required({{ .Path }}, {{ printf "%q" .Location }}) 571 } 572 {{ end }} 573 {{ if eq .CollectionFormat "multi" -}} 574 // CollectionFormat: {{ .CollectionFormat }} 575 {{ varname .Child.ValueExpression }}C := rawData 576 {{ else -}} 577 var qv{{ pascalize .Name }} string 578 if len(rawData) > 0 { 579 qv{{ pascalize .Name }} = rawData[len(rawData) - 1] 580 } 581 582 // CollectionFormat: {{ .CollectionFormat }} 583 {{ varname .Child.ValueExpression }}C := swag.SplitByFormat(qv{{ pascalize .Name }}, {{ printf "%q" .CollectionFormat }}){{ end }} 584 {{if and .Required (not .AllowEmptyValue) }} 585 if len({{ varname .Child.ValueExpression }}C) == 0 { 586 return errors.Required({{ .Path }}, {{ printf "%q" .Location }}) 587 } 588 {{ else }}if len({{ varname .Child.ValueExpression }}C) == 0 { {{ if .HasDefault }} 589 // Default values have been previously initialized by New{{ $className }}Params(){{ end }} 590 return nil 591 } {{- end }} 592 {{ template "sliceparambinder" . }} 593 {{ .ValueExpression }} = {{ varname .Child.ValueExpression }}R 594 {{- if .HasSliceValidations }} 595 if err := {{ .ReceiverName }}.validate{{ pascalize .ID }}(formats); err != nil { 596 return err 597 } 598 {{- end }} 599 600 return nil 601 } 602 {{ end }} 603 {{ if or (and (not .IsArray) .HasValidations) (and .IsArray .HasSliceValidations) }} 604 // validate{{ pascalize .ID }} carries on validations for parameter {{ .ID }} 605 func ({{ .ReceiverName }} *{{ $className }}Params) validate{{ pascalize .ID }}(formats strfmt.Registry) error { 606 {{ template "propertyparamvalidator" . }} 607 return nil 608 } 609 {{ end }} 610 {{- else if .IsBodyParam }}{{/* validation method for inline body parameters with validations */}} 611 {{- if and .HasSimpleBodyParams (not .HasModelBodyItems) (not .HasModelBodyMap) }} 612 {{- if .Schema.HasValidations }} 613 // validate{{ pascalize .ID }}Body validates an inline body parameter 614 func ({{ .ReceiverName }} *{{ $className }}Params) validate{{ pascalize .ID }}Body(formats strfmt.Registry) error { 615 {{- if .IsArray }} 616 {{- if .HasSliceValidations }} 617 {{- template "sliceparamvalidator" . }} 618 {{- end }} 619 {{- if .Child.HasValidations }} 620 {{ varname .Child.ValueExpression }}C := {{ .ValueExpression }} 621 {{ template "sliceparambinder" . }} 622 {{ .ValueExpression }} = {{ varname .Child.ValueExpression }}R 623 {{- end }} 624 {{- else if .IsMap }} 625 {{ varname .Child.ValueExpression }}C := {{ .ValueExpression }} 626 {{ template "mapparamvalidator" . }} 627 {{ .ValueExpression }} = {{ varname .Child.ValueExpression }}R 628 {{- else }} 629 {{ template "propertyparamvalidator" . }} 630 {{- end }} 631 return nil 632 } 633 {{- end }} 634 {{- end }} 635 {{- end }} 636 {{ end }}