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