github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/generator/templates/schemavalidator.gotmpl (about) 1 {{ define "primitivefieldcontextvalidator" }} 2 {{ if .ReadOnly }} 3 if err := validate.ReadOnly(ctx, {{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil{ 4 return err 5 } 6 {{ end }} 7 {{ end }} 8 {{ define "primitivefieldvalidator" }} 9 {{ if .Required }} 10 {{- if and (eq .GoType "string") (not .IsNullable) }} 11 if err := validate.RequiredString({{ path . }}, {{ printf "%q" .Location }}, {{ if .IsAliased }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if .IsAliased }}){{ end }}); err != nil { 12 {{- else }} 13 if err := validate.Required({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 14 {{- end }} 15 return err 16 } 17 {{- end }} 18 {{ if .MinLength }} 19 if err := validate.MinLength({{ path . }}, {{ printf "%q" .Location }}, {{ .ToString }}, {{.MinLength }}); err != nil { 20 return err 21 } 22 {{- end }} 23 {{ if .MaxLength }} 24 if err := validate.MaxLength({{ path . }}, {{ printf "%q" .Location }}, {{ .ToString }}, {{.MaxLength }}); err != nil { 25 return err 26 } 27 {{ end }} 28 {{ if .Pattern }} 29 if err := validate.Pattern({{ path . }}, {{ printf "%q" .Location }}, {{ .ToString }}, `{{ escapeBackticks .Pattern }}`); err != nil { 30 return err 31 } 32 {{- end }} 33 {{ if .Minimum }} 34 {{ template "validationMinimum" . }} 35 {{ end }} 36 {{ if .Maximum }} 37 {{ template "validationMaximum" . }} 38 {{ end }} 39 {{ if .MultipleOf }} 40 {{ template "validationMultipleOf" . }} 41 {{ end }} 42 {{ if .Enum }} 43 // value enum 44 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}{{ .Suffix }}Enum({{ path . }}, {{ printf "%q" .Location }}, {{ if .IsNullable }}*{{ end }}{{.ValueExpression }}); err != nil { 45 return err 46 } 47 {{- end }} 48 {{ if and .IsCustomFormatter (not .IsStream) (not .IsBase64) }} 49 {{ template "validationCustomformat" . }} 50 {{- end }} 51 {{ end }} 52 53 {{ define "slicecontextvalidator" }} 54 {{ if .ReadOnly }} 55 if err := validate.ReadOnly(ctx, {{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil{ 56 return err 57 } 58 {{ end }} 59 {{ if .Items }} 60 {{- if and (or .Items.ReadOnly .Items.HasContextValidations) (not .Items.IsInterface) (not .Items.IsStream) }} 61 for {{.IndexVar }} := 0; {{.IndexVar }} < len({{.ValueExpression }}); {{.IndexVar }}++ { 62 {{- with .Items }} 63 {{ template "propertycontextvalidator" . }} 64 {{- end }} 65 } 66 {{- end }} 67 {{- else }} 68 {{- if or .IsAliased (ne .ValueExpression .ReceiverName) }}{{/* prevents generated code to call itself: this is reserved for aliased types */}} 69 {{- if and .IsNullable (not .IsMapNullOverride) }} 70 if {{ .ValueExpression }} != nil { 71 {{- end }} 72 if err := {{.ValueExpression }}.ContextValidate(ctx, formats); err != nil { 73 if ve, ok := err.(*errors.Validation); ok { 74 return ve.ValidateName({{ path . }}) 75 } else if ce, ok := err.(*errors.CompositeError); ok { 76 return ce.ValidateName({{ path . }}) 77 } 78 return err 79 } 80 {{- if and .IsNullable (not .IsMapNullOverride) }} 81 } 82 {{- end }} 83 {{- end }} 84 {{- end }} 85 {{ end }} 86 87 {{define "slicevalidator" }} 88 {{ if .Required }} 89 if err := validate.Required({{ path . }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}); err != nil { 90 return err 91 } 92 {{ end }} 93 {{ if or .MinItems .MaxItems }} 94 {{ .IndexVar }}{{ pascalize .Name }}Size := int64(len({{.ValueExpression }})) 95 {{ end }} 96 {{ if .MinItems }} 97 if err := validate.MinItems({{ path . }}, {{ printf "%q" .Location }}, {{ .IndexVar }}{{ pascalize .Name }}Size, {{.MinItems }}); err != nil { 98 return err 99 } 100 {{ end }} 101 {{ if .MaxItems }} 102 if err := validate.MaxItems({{ path . }}, {{ printf "%q" .Location }}, {{ .IndexVar }}{{ pascalize .Name }}Size, {{.MaxItems }}); err != nil { 103 return err 104 } 105 {{ end }} 106 {{ if .UniqueItems }} 107 if err := validate.UniqueItems({{ path . }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 108 return err 109 } 110 {{ end }} 111 {{ if .Enum }} 112 // for slice 113 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}Enum({{ path . }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 114 return err 115 } 116 {{ end }} 117 {{ if .Items }} 118 {{- if and (or .Items.Required .Items.HasValidations .Items.IsBaseType .Items.IsAliased) (not .Items.IsInterface) (not .Items.IsStream) (not .Items.SkipExternalValidation) }} 119 for {{.IndexVar }} := 0; {{.IndexVar }} < len({{.ValueExpression }}); {{.IndexVar }}++ { 120 {{- with .Items }} 121 {{- if and .IsNullable (not .Required) (not .IsMapNullOverride) }} 122 {{- if .IsInterface }} 123 if {{ .ValueExpression }} == nil { // not required 124 {{- else }} 125 if swag.IsZero({{ .ValueExpression }}) { // not required 126 {{- end }} 127 continue 128 } 129 {{- end }} 130 {{ template "propertyvalidator" . }} 131 {{- end }} 132 } 133 {{- end }} 134 {{- else }} 135 {{- if and (or .IsAliased (ne .ValueExpression .ReceiverName) (not .SkipExternalValidation)) }}{{/* prevents generated code to call itself: this is reserved for aliased types */}} 136 {{- if and .IsNullable (not .IsMapNullOverride) }} 137 if {{ .ValueExpression }} != nil { 138 {{- end }} 139 if err := {{.ValueExpression }}.Validate(formats); err != nil { 140 if ve, ok := err.(*errors.Validation); ok { 141 return ve.ValidateName({{ path . }}) 142 } else if ce, ok := err.(*errors.CompositeError); ok { 143 return ce.ValidateName({{ path . }}) 144 } 145 return err 146 } 147 {{- if and .IsNullable (not .IsMapNullOverride) }} 148 } 149 {{- end }} 150 {{- end }} 151 {{- end }} 152 {{ end }} 153 {{ define "mapcontextvalidator" }} 154 {{- if and .Required }} 155 {{- if or .IsNullable .IsInterface }} 156 if {{ .ReceiverName }}.{{ pascalize .Name }} == nil { 157 return errors.Required({{ path . }}, {{ printf "%q" .Location }}, nil) 158 } 159 {{- else }} 160 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{ .ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 161 return err 162 } 163 {{- end }} 164 {{- end }} 165 {{ if .HasAdditionalProperties }} 166 {{- if .AdditionalProperties.HasContextValidations }} 167 {{- $validatedValues := .ValueExpression }}{{ $keyVar := .AdditionalProperties.KeyVar }} 168 for {{ .AdditionalProperties.KeyVar }} := range {{ .ValueExpression }} { 169 {{ with .AdditionalProperties }} 170 {{/*Don't need to add context validate directly here since we are recursing*/}} 171 {{- if .IsPrimitive }} 172 {{- if .IsAliased }} 173 {{- if not .IsAnonymous }} 174 if val, ok := {{ $validatedValues }}[{{ $keyVar }}]; ok { 175 {{- if and .IsNullable (not .IsMapNullOverride) }} 176 if val != nil { 177 {{- end }} 178 if err := val.ContextValidate(ctx, formats); err != nil { 179 return err 180 } 181 {{- if and .IsNullable (not .IsMapNullOverride) }} 182 } 183 {{- end }} 184 } 185 {{- else }}{{/* validation of anonymous objects */}} 186 {{ range .AllOf }} 187 {{ range .Properties }} 188 {{ template "propertycontextvalidator" . }} 189 {{ end }} 190 {{- end }} 191 {{ range .Properties }} 192 {{ template "propertycontextvalidator" . }} 193 {{ end }} 194 {{- end }} 195 {{ if and .IsTuple .AdditionalItems }} 196 // TODO: context validating additional items should go here, if you see this raise an issue{{/* TODO(fred): investigate the case to remove that comment: AdditionalItems shouldn't come in maps. Upstream validation is needed to guard against this */}} 197 // at https://github.com/thetreep/go-swagger/issues 198 {{ end }} 199 {{ else }} 200 {{ template "primitivefieldcontextvalidator" . }} 201 {{ end }} 202 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 203 {{- if and (not .IsStream) (not .IsBase64) }}{{/* TODO: IsStream and CustomFormattershould be mutually exclusive in type resolver */}} 204 // TODO: context validating custom formatter items should go here, if you see this raise an issue 205 // at https://github.com/thetreep/go-swagger/issues 206 {{/* 207 {{ template "validationCustomformat" . }} 208 */}} 209 {{- end }} 210 {{- else if .IsArray }} 211 {{ template "slicecontextvalidator" . }} 212 {{- else if and .IsMap (not .IsInterface) }} 213 {{ template "mapcontextvalidator" . }} 214 {{- else if and .IsMap .IsInterface }} 215 {{ if .Enum }} 216 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}ValueEnum({{ path . }}, {{ printf "%q" .Location }}, {{ $validatedValues }}[{{ $keyVar }}]); err != nil { 217 return err 218 } 219 {{- end }} 220 {{- else if or .IsComplexObject .IsTuple .IsAdditionalProperties .IsAliased }} 221 {{- if not .IsAnonymous }} 222 if val, ok := {{ $validatedValues }}[{{ $keyVar }}]; ok { 223 {{- if and .IsNullable (not .IsMapNullOverride) }} 224 if val != nil { 225 {{- end }} 226 if err := val.ContextValidate(ctx, formats); err != nil { 227 return err 228 } 229 {{- if and .IsNullable (not .IsMapNullOverride) }} 230 } 231 {{- end }} 232 } 233 {{- else }} 234 {{ range .AllOf }} 235 {{ range .Properties }} 236 {{ template "propertycontextvalidator" . }} 237 {{ end }} 238 {{- end }} 239 {{ range .Properties }} 240 {{ template "propertycontextvalidator" . }} 241 {{- end }} 242 {{- end }} 243 {{ if and .IsTuple .AdditionalItems }} 244 // TODO: context validating additional items should go here, if you see this raise an issue 245 // at https://github.com/thetreep/go-swagger/issues 246 {{ end }} 247 {{- end }} 248 } 249 {{ end }} 250 {{ end }} 251 {{- else if .IsAliased }} 252 {{- if and .IsMap .HasValidations }}{{/* validation of aliased maps but does not know about AdditionalProperties: e.g. it comes from a $ref */}} 253 {{- if not .IsAnonymous }} 254 {{- if $.IsMap }}{{/* we come from a map range */}} 255 if val, ok := {{ .ValueExpression }}; ok { 256 {{- end }} 257 {{- if and .IsNullable (not .IsMapNullOverride) }} 258 {{- if $.IsMap }} 259 if val != nil { 260 {{- else }} 261 if {{ .ValueExpression }} != nil { 262 {{- end }} 263 {{- end }} 264 if err := {{ if $.IsMap }}val{{ else }}{{ .ValueExpression }}{{ end }}.ContextValidate(ctx, formats); err != nil { 265 return err 266 } 267 {{- if and .IsNullable (not .IsMapNullOverride) }} 268 } 269 {{- end }} 270 {{- if or $.IsMap }} 271 } 272 {{- end }} 273 {{- end }} 274 {{- end }} 275 {{- end }} 276 {{ end }} {{/*mapcontextvalidator*/}} 277 {{ define "mapvalidator" }}{{/* validates additionalProperties */}} 278 {{- if and .Required }} 279 {{- if or .IsNullable .IsInterface }} 280 if {{ .ReceiverName }}.{{ pascalize .Name }} == nil { 281 return errors.Required({{ path . }}, {{ printf "%q" .Location }}, nil) 282 } 283 {{- else }} 284 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{ .ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 285 return err 286 } 287 {{- end }} 288 {{- end }} 289 {{ if .HasAdditionalProperties }} 290 {{- if and .AdditionalProperties.HasValidations (not .AdditionalProperties.SkipExternalValidation) }} 291 {{- $validatedValues := .ValueExpression }}{{ $keyVar := .AdditionalProperties.KeyVar }} 292 for {{ .AdditionalProperties.KeyVar }} := range {{ .ValueExpression }} { 293 {{ with .AdditionalProperties }} 294 {{- if and (not .Required) .IsNullable }}{{/* skip when nul type is accepted */}} 295 {{- if .IsInterface }} 296 if {{ $validatedValues }}[{{ $keyVar }}] == nil { // not required 297 {{- else }} 298 if swag.IsZero({{ .ValueExpression }}) { // not required 299 {{- end }} 300 continue 301 } 302 {{- else if and (.Required) (not .IsArray) }}{{/* Required slice is processed below */}} 303 if err := validate.Required({{ path . }}, {{ printf "%q" .Location }}, {{ $validatedValues }}[{{ $keyVar }}]); err != nil { 304 return err 305 } 306 {{- end }} 307 {{- if and .IsPrimitive (not .SkipExternalValidation ) }} 308 {{- if .IsAliased }} 309 {{- if not .IsAnonymous }} 310 if val, ok := {{ $validatedValues }}[{{ $keyVar }}]; ok { 311 {{- if and .IsNullable (not .IsMapNullOverride) }} 312 if val != nil { 313 {{- end }} 314 if err := val.Validate(formats); err != nil { 315 if ve, ok := err.(*errors.Validation); ok { 316 return ve.ValidateName({{ path . }}) 317 } else if ce, ok := err.(*errors.CompositeError); ok { 318 return ce.ValidateName({{ path . }}) 319 } 320 return err 321 } 322 {{- if and .IsNullable (not .IsMapNullOverride) }} 323 } 324 {{- end }} 325 } 326 {{- else }}{{/* validation of anonymous objects */}} 327 {{ range .AllOf }} 328 {{ range .Properties }} 329 {{ template "propertyvalidator" . }} 330 {{ end }} 331 {{- end }} 332 {{ range .Properties }} 333 {{ template "propertyvalidator" . }} 334 {{ end }} 335 {{- end }} 336 {{ if and .IsTuple .AdditionalItems }} 337 // TODO: validating additional items should go here, if you see this raise an issue{{/* TODO(fred): investigate the case to remove that comment: AdditionalItems shouldn't come in maps. Upstream validation is needed to guard against this */}} 338 // at https://github.com/thetreep/go-swagger/issues 339 {{ end }} 340 {{- else }} 341 {{ template "primitivefieldvalidator" . }} 342 {{- end }} 343 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 344 {{- if .Required }} 345 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 346 return err 347 } 348 {{- end }} 349 {{- if and (not .IsStream) (not .IsBase64) (not .SkipExternalValidation) }}{{/* TODO: IsStream and CustomFormattershould be mutually exclusive in type resolver */}} 350 {{ template "validationCustomformat" . }} 351 {{- end }} 352 {{- else if .IsArray }} 353 {{ template "slicevalidator" . }} 354 {{- else if and .IsMap (not .IsInterface) }} 355 {{ template "minmaxProperties" .}} 356 {{ template "mapvalidator" . }} 357 {{ if .Enum }} 358 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}ValueEnum({{ path . }}, {{ printf "%q" .Location }}, {{ $validatedValues }}[{{ $keyVar }}]); err != nil { 359 return err 360 } 361 {{- end }} 362 {{- else if and .IsMap .IsInterface }} 363 {{ if .Enum }} 364 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}ValueEnum({{ path . }}, {{ printf "%q" .Location }}, {{ $validatedValues }}[{{ $keyVar }}]); err != nil { 365 return err 366 } 367 {{- end }} 368 {{- else if or .IsComplexObject .IsTuple .IsAdditionalProperties .IsAliased }} 369 {{- if and (not .IsAnonymous) (not .SkipExternalValidation) }} 370 if val, ok := {{ $validatedValues }}[{{ $keyVar }}]; ok { 371 {{- if and .IsNullable (not .IsMapNullOverride) }} 372 if val != nil { 373 {{- end }} 374 if err := val.Validate(formats); err != nil { 375 if ve, ok := err.(*errors.Validation); ok { 376 return ve.ValidateName({{ path . }}) 377 } else if ce, ok := err.(*errors.CompositeError); ok { 378 return ce.ValidateName({{ path . }}) 379 } 380 return err 381 } 382 {{- if and .IsNullable (not .IsMapNullOverride) }} 383 } 384 {{- end }} 385 } 386 {{- else }} 387 {{ range .AllOf }} 388 {{ range .Properties }} 389 {{ template "propertyvalidator" . }} 390 {{ end }} 391 {{- end }} 392 {{ range .Properties }} 393 {{ template "propertyvalidator" . }} 394 {{- end }} 395 {{- end }} 396 {{ if and .IsTuple .AdditionalItems }} 397 // TODO: validating additional items should go here, if you see this raise an issue 398 // at https://github.com/thetreep/go-swagger/issues 399 {{ end }} 400 {{ end }} 401 } 402 {{- end }} 403 {{ end }} 404 {{ if .Enum }} 405 // from map 406 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Enum({{ path . }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}); err != nil { 407 return err 408 } 409 {{ end }} 410 {{- else if .IsAliased }} 411 {{- if and .IsMap .HasValidations (not .SkipExternalValidation) }}{{/* validation of aliased maps but does not know about AdditionalProperties: e.g. it comes from a $ref */}} 412 {{- if not .IsAnonymous }} 413 {{- if $.IsMap }}{{/* we come from a map range */}} 414 if val, ok := {{ .ValueExpression }}; ok { 415 {{- end }} 416 {{- if and .IsNullable (not .IsMapNullOverride) }} 417 {{- if $.IsMap }} 418 if val != nil { 419 {{- else }} 420 if {{ .ValueExpression }} != nil { 421 {{- end }} 422 {{- end }} 423 if err := {{ if $.IsMap }}val{{ else }}{{ .ValueExpression }}{{ end }}.Validate(formats); err != nil { 424 return err 425 } 426 {{- if and .IsNullable (not .IsMapNullOverride) }} 427 } 428 {{- end }} 429 {{- if or $.IsMap }} 430 } 431 {{- end }} 432 {{- end }} 433 {{- end }} 434 {{- else if .Enum }} 435 // from map without additionalProperties 436 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Enum({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}); err != nil { 437 return err 438 } 439 {{- end }} 440 {{ end }} 441 442 {{define "objectcontextvalidator" }} 443 {{/* Debug 444 // .Name: {{ .Name }} 445 // .IsAliased: {{ .IsAliased }} 446 // .IsAnonymous: {{ .IsAnonymous }} 447 // .IsNullable: {{ .IsNullable }} 448 // .Required: {{ .Required }} 449 // .ReadOnly: {{ .ReadOnly }} 450 // .HasContextValidations {{ .HasContextValidations }} 451 // .IsBaseType: {{ .IsBaseType }} 452 // .ValueExpression: {{ .ValueExpression }} 453 // .ReceiverName: {{ .ReceiverName }} 454 */}} 455 {{- if not .IsAnonymous }} 456 {{- if or .IsAliased (ne .ValueExpression .ReceiverName) }}{{/* prevents generated code to call itself: case of aliased types */}} 457 {{- if and .IsNullable (not .IsMapNullOverride) }} 458 if {{ .ValueExpression }} != nil { 459 {{- end }} 460 {{ if not .Required }} 461 {{- if .IsInterface }} 462 if {{ .ValueExpression }} == nil { // not required 463 {{- else }} 464 if swag.IsZero({{ .ValueExpression }}) { // not required 465 {{- end }} 466 return nil 467 } 468 {{ end }} 469 if err := {{.ValueExpression }}.ContextValidate(ctx, formats); err != nil { 470 if ve, ok := err.(*errors.Validation); ok { 471 return ve.ValidateName({{ path . }}) 472 } else if ce, ok := err.(*errors.CompositeError); ok { 473 return ce.ValidateName({{ path . }}) 474 } 475 return err 476 } 477 {{- if and .IsNullable (not .IsMapNullOverride) }} 478 } 479 {{- end }} 480 {{- end }} 481 {{- else }} 482 {{ range .AllOf }} 483 {{ range .Properties }} 484 {{ template "propertycontextvalidator" . }} 485 {{ end }} 486 {{ end }} 487 {{ range .Properties }} 488 {{ template "propertycontextvalidator" . }} 489 {{ end }} 490 {{- end }} 491 {{- if and .IsTuple .AdditionalItems }} 492 // TODO: context validating additional items should go here, if you see this raise an issue 493 // at https://github.com/thetreep/go-swagger/issues 494 {{- end }} 495 {{ end }} 496 {{ define "minmaxProperties" }} 497 {{- if and (or .IsMap (and .IsAdditionalProperties .HasAdditionalProperties)) (or .MinProperties .MaxProperties) }} 498 {{- if and (not .IsAdditionalProperties) (not .IsInterface) (eq (len .Properties) 0) }}{{/* map only */}} 499 nprops := len({{ if and .IsMap (not .IsAliased) .HasAdditionalProperties (not .IsElem) (not .IsProperty) }}{{ .ReceiverName }}{{ else }}{{ .ValueExpression }}{{ end }}) 500 {{- else }}{{/* object with properties */}} 501 {{- if and .IsNullable .MinProperties }} 502 {{- if gt0 .MinProperties }} 503 504 // short circuits minProperties > 0 505 if {{ .ReceiverName }} == nil { 506 return errors.TooFewProperties({{ path . }}, {{ printf "%q" .Location }}, {{ .MinProperties }}) 507 } 508 {{- end }} 509 {{- end }} 510 511 props := make(map[string]json.RawMessage, {{ len .Properties }}{{ if .HasAdditionalProperties }}+ 10{{ end }}) 512 j, err := swag.WriteJSON({{ .ReceiverName }}) 513 if err != nil { 514 return err 515 } 516 517 if err = swag.ReadJSON(j, &props) ; err != nil { 518 return err 519 } 520 521 nprops := len(props) 522 {{- end }} 523 {{ if .MinProperties }} 524 // minProperties: {{ .MinProperties }} 525 if nprops < {{ .MinProperties }} { 526 return errors.TooFewProperties({{ path . }}, {{ printf "%q" .Location }}, {{ .MinProperties }}) 527 } 528 {{- end }} 529 {{ if .MaxProperties }} 530 // maxProperties: {{ .MaxProperties }} 531 if nprops > {{ .MaxProperties }} { 532 return errors.TooManyProperties({{ path . }}, {{ printf "%q" .Location }}, {{ .MaxProperties }}) 533 } 534 {{- end }} 535 {{- end }} 536 {{- end }} 537 {{define "objectvalidator" }}{{/* // DEBUG 538 // .Name: {{ .Name }} 539 // .IsAliased: {{ .IsAliased }} 540 // .IsAnonymous: {{ .IsAnonymous }} 541 // .IsNullable: {{ .IsNullable }} 542 // .Required: {{ .Required }} 543 // .ReadOnly: {{ .ReadOnly }} 544 // .HasValidations {{ .HasValidations }} 545 // .HasContextValidations {{ .HasContextValidations }} 546 // .IsBaseType: {{ .IsBaseType }} 547 // .ValueExpression: {{ .ValueExpression }} 548 // .ReceiverName: {{ .ReceiverName }} 549 // .IsAdditionalProperties: {{ .IsAdditionalProperties }} 550 // .IsInterface: {{ .IsInterface }} 551 // .IsMap: {{ .IsMap }} 552 // .IsArray: {{ .IsArray }} 553 // .IsMapNullOverride: {{ .IsMapNullOverride }} 554 */}} 555 {{- if not .IsAnonymous }} 556 {{- if and .Required (or .IsNullable .IsBaseType .IsMap) }} 557 if err := validate.Required({{ path . }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 558 return err 559 } 560 {{- if and (not .Required) .IsBaseType }} 561 if {{ .ValueExpression }} == nil { 562 return nil 563 } 564 {{- end }} 565 {{ end }} 566 {{- if and (or .IsAliased (ne .ValueExpression .ReceiverName)) (not .SkipExternalValidation) }}{{/* prevents generated code to call itself: case of aliased types */}} 567 {{- if or (and (or .IsNullable) (not .IsMapNullOverride)) .IsMap .IsArray }} 568 if {{ .ValueExpression }} != nil { 569 {{- end }} 570 if err := {{.ValueExpression }}.Validate(formats); err != nil { 571 if ve, ok := err.(*errors.Validation); ok { 572 return ve.ValidateName({{ path . }}) 573 } else if ce, ok := err.(*errors.CompositeError); ok { 574 return ce.ValidateName({{ path . }}) 575 } 576 return err 577 } 578 {{- if or (and (or .IsNullable) (not .IsMapNullOverride)) .IsMap .IsArray }} 579 } 580 {{- end }} 581 {{- end }} 582 {{- else }} 583 {{ template "minmaxProperties" .}} 584 {{ range .AllOf }} 585 {{ range .Properties }} 586 {{ template "propertyvalidator" . }} 587 {{ end }} 588 {{ end }} 589 {{ range .Properties }} 590 {{ template "propertyvalidator" . }} 591 {{ end }} 592 {{- end }} 593 {{- if and .IsTuple .AdditionalItems }} 594 // TODO: validating additional items should go here, if you see this raise an issue 595 // at https://github.com/thetreep/go-swagger/issues 596 {{- end }} 597 {{ end }} 598 599 {{define "propertycontextvalidator"}} 600 {{- if .IsPrimitive }} 601 {{- if .IsAliased }} 602 {{ template "objectcontextvalidator" . }} 603 {{- else }} 604 {{ template "primitivefieldcontextvalidator" . }} 605 {{- end }} 606 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 607 // TODO: context validating primitive with custom formatter should go here, if you see this raise an issue 608 // at https://github.com/thetreep/go-swagger/issues 609 {{- if .ReadOnly }} 610 611 if err := validate.ReadOnly{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 612 return err 613 } 614 {{- end }} 615 {{- if and (not .IsStream) (not .IsBase64) }} 616 // TODO: context validating properties with custom formatter should go here, if you see this raise an issue 617 // at https://github.com/thetreep/go-swagger/issues 618 {{/*{{ template "validationCustomformat" . }}*/}} 619 {{- end }} 620 {{- else if .IsArray }} 621 {{ template "slicecontextvalidator" . }} 622 {{- else if and .IsMap (or (not .IsAliased) (and .IsAliased .IsInterface)) }}{{/* except for interface, the renderinf for aliased maps is performed by objectvalidator */}} 623 {{ template "mapcontextvalidator" . }} 624 {{- else if or .IsComplexObject .IsTuple .IsAdditionalProperties .IsAliased }} 625 {{ template "objectcontextvalidator" . }} 626 {{- end }} 627 628 {{end}} 629 630 {{define "propertyvalidator" }} 631 {{- if .IsPrimitive }} 632 {{- if .IsAliased }} 633 {{- if and .Required (not .IsAnonymous) }} 634 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 635 return err 636 } 637 {{- end }} 638 {{ template "objectvalidator" . }} 639 {{- else }} 640 {{ template "primitivefieldvalidator" . }} 641 {{- end }} 642 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 643 {{- if .Required }} 644 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 645 return err 646 } 647 {{- end }} 648 {{- if and (not .IsStream) (not .IsBase64) }} 649 {{ template "validationCustomformat" . }} 650 {{- end }} 651 {{- else if .IsArray }} 652 {{ template "slicevalidator" . }} 653 {{- else if and .IsMap (or (not .IsAliased) (and .IsAliased .IsInterface)) }} 654 {{ template "minmaxProperties" . }} 655 {{ template "mapvalidator" . }} 656 {{- else if or .IsComplexObject .IsTuple .IsAdditionalProperties .IsAliased }} 657 {{- if and .IsAdditionalProperties .Required (not .IsAliased) }} 658 {{- if or .IsNullable .IsInterface }} 659 if {{ .ValueExpression }} == nil { 660 return errors.Required({{ path . }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}) 661 } 662 {{- else }} 663 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 664 return err 665 } 666 {{- end }} 667 {{- end }} 668 {{ template "objectvalidator" . }} 669 {{- else if and .IsExternal .Required }} 670 {{- if or .IsNullable .IsInterface }} 671 if {{ .ValueExpression }} == nil { 672 return errors.Required({{ path . }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}) 673 } 674 {{- else }} 675 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 676 return err 677 } 678 {{- end }} 679 {{- end }} 680 {{ end }} 681 682 {{define "fieldcontextvalidator" }} 683 {{- if .IsPrimitive }} 684 {{ template "primitivefieldcontextvalidator" . }} 685 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 686 {{- if and (not .IsStream) (not .IsBase64) }} 687 // TODO: context validating properties with custom formatter should go here, if you see this raise an issue 688 // at https://github.com/thetreep/go-swagger/issues 689 {{/* 690 {{ template "validationCustomformat" . }} 691 */}} 692 {{- end }} 693 {{- else if .IsArray }} 694 {{ template "slicecontextvalidator" . }} 695 {{- else if .IsMap }} 696 {{ template "mapcontextvalidator" . }} 697 {{- end }} 698 699 {{ end }} 700 701 {{ define "fieldvalidator"}} 702 {{- if .IsPrimitive }} 703 {{ template "primitivefieldvalidator" . }} 704 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 705 {{- if .Required }} 706 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ path . }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 707 return err 708 } 709 {{- end }} 710 {{- if and (not .IsStream) (not .IsBase64) }} 711 {{ template "validationCustomformat" . }} 712 {{- end }} 713 {{- else if .IsArray }} 714 {{ template "slicevalidator" . }} 715 {{- else if .IsMap }} 716 {{ template "mapvalidator" . }} 717 {{- end }} 718 {{ end }} 719 720 {{define "schemacontextvalidator" }} 721 // ContextValidate validate this {{ humanize .Name }} based on the context it is used 722 func ({{.ReceiverName }} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if .Discriminates }}{{ camelize .Name }}{{ else if .IsExported }}{{ pascalize .Name }}{{ else }}{{ .Name }}{{ end }}) ContextValidate(ctx context.Context, formats strfmt.Registry) error { 723 var res []error 724 {{ range .AllOf }} 725 {{- if not .Properties }} 726 // validation for a type composition with {{ .GoType }} 727 {{- end }} 728 {{- if and (or .IsInterface .IsAnonymous .IsBaseType) (or .HasContextValidations) }} 729 {{ template "fieldcontextvalidator" . }} 730 {{ range .Properties }} 731 {{ if and (ne $.DiscriminatorField .Name) (or .HasContextValidations) }} 732 if err := {{.ReceiverName }}.contextValidate{{ pascalize .Name }}(ctx, formats); err != nil { 733 res = append(res, err) 734 } 735 {{- end }} 736 {{- end }} 737 {{- if and .HasAdditionalProperties (not .IsMap) }}{{/* validates additionalProperties in an object which is not itself a map */}} 738 {{ template "mapcontextvalidator" . }} 739 {{- end }} 740 {{ if and .IsTuple .AdditionalItems }} 741 // TODO: context validating additional items should go here, if you see this raise an issue 742 // at https://github.com/thetreep/go-swagger/issues 743 {{/* 744 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Items(formats); err != nil { 745 res = append(res, err) 746 } 747 */}} 748 {{ end }} 749 {{- else if (or .HasContextValidations) }} 750 if err := {{ .ReceiverName }}.{{ pascalize (dropPackage .GoType) }}.ContextValidate(ctx, formats); err != nil { 751 res = append(res, err) 752 } 753 {{- end }} 754 {{- end }} {{/*end AllOf*/}} 755 {{ template "fieldcontextvalidator" . }} 756 {{ range .Properties }} 757 {{ if .HasContextValidations }} {{/* complex obj always has cv*/}} 758 if err := {{.ReceiverName }}.contextValidate{{ pascalize .Name }}(ctx, formats); err != nil { 759 res = append(res, err) 760 } 761 {{ end }} 762 {{ end }} 763 {{- if and .HasAdditionalProperties (not .IsMap) }}{{/* validates additionalProperties in an object which is not itself a map */}} 764 {{ template "mapcontextvalidator" . }} 765 {{- end }} 766 if len(res) > 0 { 767 return errors.CompositeValidationError(res...) 768 } 769 return nil 770 } 771 772 773 {{ range .Properties }} 774 {{ if .HasContextValidations }} 775 func ({{.ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) contextValidate{{ pascalize .Name }}(ctx context.Context, formats strfmt.Registry) error { 776 {{template "propertycontextvalidator" . }} 777 return nil 778 } 779 {{ end }} 780 {{ end }} {{/*Properties*/}} 781 {{ range .AllOf }} 782 {{ range .Properties }} 783 {{ if .HasContextValidations }} 784 func ({{.ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) contextValidate{{ pascalize .Name }}(ctx context.Context, formats strfmt.Registry) error { 785 {{template "propertycontextvalidator" . }} 786 return nil 787 } 788 {{ end }} 789 {{ end }} 790 {{ end }} {{/*AllOf*/}} 791 {{end}} {{/*schemacontextvalidator*/}} 792 793 {{define "schemavalidator" }} 794 {{ if .Enum }} 795 {{ if (eq .SwaggerType "string") }} 796 {{ $gotype := .GoType }} 797 const ( 798 {{ range .Enum }} 799 {{- $variant := print $gotype (pascalize (cleanupEnumVariant .)) }} 800 // {{ $variant }} captures enum value {{ printf "%q" . }} 801 {{ $variant }} {{ $gotype }} = {{ printf "%q" . }} 802 {{ end }} 803 ) 804 {{ end }} 805 806 // for schema 807 var {{ camelize .Name }}Enum []interface{} 808 func init() { 809 var res []{{ template "dereffedSchemaType" . }} 810 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 811 panic(err) 812 } 813 for _, v := range res { 814 {{ camelize .Name }}Enum = append({{ camelize .Name }}Enum, v) 815 } 816 } 817 818 func ({{ .ReceiverName }} {{ if not .IsPrimitive }}*{{ end }}{{ if .IsExported }}{{ pascalize .Name }}{{ else }}{{ .Name }}{{ end }}) validate{{ pascalize .Name }}Enum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" . }}) error { 819 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}Enum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 820 return err 821 } 822 return nil 823 } 824 {{ end }} 825 {{ if .ItemsEnum }} 826 var {{ camelize .Name }}ItemsEnum []interface{} 827 828 func init() { 829 var res []{{ template "dereffedSchemaType" .Items }} 830 if err := json.Unmarshal([]byte(`{{ json .ItemsEnum }}`), &res); err != nil { 831 panic(err) 832 } 833 for _, v := range res { 834 {{ camelize .Name }}ItemsEnum = append({{ camelize .Name }}ItemsEnum, v) 835 } 836 } 837 838 func ({{ .ReceiverName }} *{{ if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}ItemsEnum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" .Items }}) error { 839 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 840 return err 841 } 842 return nil 843 } 844 {{ end }} 845 {{ with .AdditionalProperties }} 846 {{ if .Enum }} 847 // for additional props 848 var {{ camelize .Name }}ValueEnum []interface{} 849 850 func init() { 851 var res []{{ template "dereffedSchemaType" . }} 852 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 853 panic(err) 854 } 855 for _, v := range res { 856 {{ camelize .Name }}ValueEnum = append({{ camelize .Name }}ValueEnum, v) 857 } 858 } 859 860 func ({{ .ReceiverName }} *{{ if .IsExported }}{{ pascalize .Name }}{{ else }}{{ .Name }}{{ end }}) validate{{ pascalize .Name }}ValueEnum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" . }}) error { 861 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}ValueEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 862 return err 863 } 864 return nil 865 } 866 {{- end }} 867 {{ end }} 868 // Validate validates this {{ humanize .Name }} 869 func ({{.ReceiverName }} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if .Discriminates }}{{ camelize .Name }}{{ else if .IsExported }}{{ pascalize .Name }}{{ else }}{{ .Name }}{{ end }}) Validate(formats strfmt.Registry) error { 870 var res []error 871 {{ template "minmaxProperties" .}} 872 {{ range .AllOf }} 873 {{- if not .Properties }} 874 // validation for a type composition with {{ .GoType }} 875 {{- end }} 876 {{- if and (or .IsInterface .IsAnonymous .IsBaseType) (or .Required .HasValidations) }} 877 {{ template "fieldvalidator" . }} 878 879 {{ range .Properties }} 880 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 881 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}(formats); err != nil { 882 res = append(res, err) 883 } 884 {{- end }} 885 {{- end }} 886 {{- if and .HasAdditionalProperties (not .IsMap) }}{{/* validates additionalProperties in an object which is not itself a map */}} 887 {{ template "mapvalidator" . }} 888 {{- end }} 889 {{ if and .IsTuple .AdditionalItems }} 890 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Items(formats); err != nil { 891 res = append(res, err) 892 } 893 {{ end }} 894 {{- else if (or .Required .HasValidations) }} 895 if err := {{ .ReceiverName }}.{{ pascalize (dropPackage .GoType) }}.Validate(formats); err != nil { 896 res = append(res, err) 897 } 898 {{- end }} 899 {{- end }} 900 {{ template "fieldvalidator" . }} 901 {{ range .Properties }} 902 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 903 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}(formats); err != nil { 904 res = append(res, err) 905 } 906 {{ end }} 907 {{ end }} 908 {{- if and .HasAdditionalProperties (not .IsMap) }}{{/* validates additionalProperties in an object which is not itself a map */}} 909 {{ template "mapvalidator" . }} 910 {{- end }} 911 {{ if and .IsTuple .AdditionalItems }}{{/* validates additionalItems in a tuple */}} 912 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Items(formats); err != nil { 913 res = append(res, err) 914 } 915 {{ end }} 916 {{ if and .Enum (not .IsPrimitive) (not .IsMap) }} 917 // value enum 918 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Enum("", "body", {{ .ReceiverName }}); err != nil { 919 res = append(res, err) 920 } 921 {{ end }} 922 923 if len(res) > 0 { 924 return errors.CompositeValidationError(res...) 925 } 926 return nil 927 } 928 929 {{ range .Properties }} 930 {{ if or .Required .HasValidations }} 931 {{ if .Enum }} 932 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 933 934 func init() { 935 var res []{{ template "dereffedSchemaType" . }} 936 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 937 panic(err) 938 } 939 for _, v := range res { 940 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 941 } 942 } 943 944 {{ if (eq .SwaggerType "string") }} 945 {{ $gotype := .GoType }} 946 {{ $propname := .Name }} 947 const ( 948 {{ range .Enum }} 949 {{- $variant := print (pascalize $.Name) (pascalize $propname) (pascalize (cleanupEnumVariant .)) }} 950 // {{ $variant }} captures enum value {{ printf "%q" . }} 951 {{ $variant }} {{ $gotype }} = {{ printf "%q" . }} 952 {{ end }} 953 ) 954 {{ end }} 955 956 // prop value enum 957 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}Enum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" . }}) error { 958 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 959 return err 960 } 961 return nil 962 } 963 {{ end }} 964 {{ if .ItemsEnum }} 965 var {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum []interface{} 966 func init() { 967 var res []{{ template "dereffedSchemaType" .Items }} 968 if err := json.Unmarshal([]byte(`{{ json .ItemsEnum }}`), &res); err != nil { 969 panic(err) 970 } 971 for _, v := range res { 972 {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, v) 973 } 974 } 975 976 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}ItemsEnum(path, location string, value {{ if or .Items.IsTuple .Items.IsComplexObject .Items.IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" .Items }}) error { 977 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 978 return err 979 } 980 return nil 981 } 982 {{ end }} 983 {{ if .AdditionalItems }} 984 {{ if .AdditionalItems.Enum }} 985 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 986 987 func init() { 988 var res []{{ template "dereffedSchemaType" .AdditionalItems }} 989 if err := json.Unmarshal([]byte(`{{ json .AdditionalItems.Enum }}`), &res); err != nil { 990 panic(err) 991 } 992 for _, v := range res { 993 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 994 } 995 } 996 997 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}Enum(path, location string, value {{ if or .AdditionalItems.IsTuple .AdditionalItems.IsComplexObject .AdditionalItems.IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" .AdditionalItems }}) error { 998 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 999 return err 1000 } 1001 return nil 1002 } 1003 {{ end }} 1004 {{ end }} 1005 {{ with .AdditionalProperties }} 1006 {{ if .Enum }} 1007 // additional properties value enum 1008 var {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum []interface{} 1009 1010 func init() { 1011 var res []{{ template "dereffedSchemaType" . }} 1012 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 1013 panic(err) 1014 } 1015 for _, v := range res { 1016 {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, v) 1017 } 1018 } 1019 1020 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}ValueEnum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" . }}) error { 1021 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 1022 return err 1023 } 1024 return nil 1025 } 1026 {{ end }} 1027 {{ end }} 1028 1029 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 1030 func ({{.ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}(formats strfmt.Registry) error { 1031 {{- if not .Required }} 1032 {{- if .IsInterface }} 1033 if .ValueExpression == nil { // not required 1034 {{- else }} 1035 if swag.IsZero({{ .ValueExpression }}) { // not required 1036 {{- end }} 1037 return nil 1038 } 1039 {{- end }} 1040 {{- if and $.IsTuple .IsMap .Required }} 1041 {{- if .IsInterface }} 1042 if {{ .ValueExpression }} == nil { 1043 return errors.Required({{ path . }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}) 1044 } 1045 {{- else }} 1046 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}( 1047 {{ path . }}, {{ printf "%q" .Location }}, 1048 {{- if and (eq .GoType "string") (not (or .IsAnonymous .IsNullable)) }}{{ .GoType }}({{ end }} 1049 {{- .ValueExpression }} 1050 {{- if and (eq .GoType "string") (not (or .IsAnonymous .IsNullable)) }}){{ end }}); err != nil { 1051 return err 1052 } 1053 {{- end }} 1054 {{- end }} 1055 {{template "propertyvalidator" . }} 1056 1057 return nil 1058 } 1059 {{ end }} 1060 {{ end }} 1061 {{ end }} 1062 {{ range .AllOf }} 1063 {{ range .Properties }} 1064 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 1065 {{ if .Enum }} 1066 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 1067 1068 func init() { 1069 var res []{{ template "dereffedSchemaType" . }} 1070 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 1071 panic(err) 1072 } 1073 for _, v := range res { 1074 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 1075 } 1076 } 1077 1078 // property enum 1079 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}Enum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" . }}) error { 1080 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 1081 return err 1082 } 1083 return nil 1084 } 1085 {{ end }} 1086 {{ if .ItemsEnum }} 1087 var {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum []interface{} 1088 1089 func init() { 1090 var res []{{ template "dereffedSchemaType" .Items }} 1091 if err := json.Unmarshal([]byte(`{{ json .ItemsEnum }}`), &res); err != nil { 1092 panic(err) 1093 } 1094 for _, v := range res { 1095 {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, v) 1096 } 1097 } 1098 1099 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}ItemsEnum(path, location string, value {{ if or .Items.IsTuple .Items.IsComplexObject .Items.IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" .Items }}) error { 1100 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 1101 return err 1102 } 1103 return nil 1104 } 1105 {{ end }} 1106 {{ if .AdditionalItems }} 1107 {{ if .AdditionalItems.Enum }} 1108 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 1109 1110 func init() { 1111 var res []{{ template "dereffedSchemaType" .AdditionalItems }} 1112 if err := json.Unmarshal([]byte(`{{ json .AdditionalItems.Enum }}`), &res); err != nil { 1113 panic(err) 1114 } 1115 for _, v := range res { 1116 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 1117 } 1118 } 1119 1120 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}Enum(path, location string, value {{ if or .AdditionalItems.IsTuple .AdditionalItems.IsComplexObject .AdditionalItems.IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" .AdditionalItems }}) error { 1121 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 1122 return err 1123 } 1124 return nil 1125 } 1126 {{ end }} 1127 {{ end }} 1128 {{ with .AdditionalProperties }} 1129 {{ if .Enum }} 1130 var {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum []interface{} 1131 func init() { 1132 var res []{{ template "dereffedSchemaType" . }} 1133 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 1134 panic(err) 1135 } 1136 for _, v := range res { 1137 {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, v) 1138 } 1139 } 1140 1141 // additional properties value enum 1142 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}ValueEnum(path, location string, value {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" . }}) error { 1143 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 1144 return err 1145 } 1146 return nil 1147 } 1148 {{ end }} 1149 {{ end }} 1150 1151 1152 func ({{.ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}(formats strfmt.Registry) error { 1153 {{ if not .Required }} 1154 {{- if .IsInterface }} 1155 if {{ .ValueExpression }} == nil { // not required 1156 {{- else }} 1157 if swag.IsZero({{ .ValueExpression }}) { // not required 1158 {{- end }} 1159 return nil 1160 } 1161 {{ end }} 1162 {{template "propertyvalidator" . }} 1163 1164 return nil 1165 } 1166 {{ end }} 1167 {{ end }} 1168 {{ end }} 1169 1170 {{ if .HasAdditionalItems }} 1171 {{ if .AdditionalItems.Enum }} 1172 var {{ camelize .Name }}ItemsEnum []interface{} 1173 1174 func init() { 1175 var res []{{ template "dereffedSchemaType" .AdditionalItems }} 1176 if err := json.Unmarshal([]byte(`{{ json .AdditionalItems.Enum }}`), &res); err != nil { 1177 panic(err) 1178 } 1179 for _, v := range res { 1180 {{ camelize .Name }}ItemsEnum = append({{ camelize .Name }}ItemsEnum, v) 1181 } 1182 } 1183 1184 func ({{ .ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}ItemsEnum(path, location string, value {{ if or .AdditionalItems.IsTuple .AdditionalItems.IsComplexObject .AdditionalItems.IsAdditionalProperties }}*{{ end }}{{ template "dereffedSchemaType" .AdditionalItems }}) error { 1185 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 1186 return err 1187 } 1188 return nil 1189 } 1190 {{ end }} 1191 func ({{.ReceiverName }} *{{ pascalize .Name }}) validate{{ pascalize .Name }}Items(formats strfmt.Registry) error { 1192 {{ if and (or .AdditionalItems.Required .AdditionalItems.HasValidations) (not .AdditionalItems.SkipExternalValidation) }} 1193 for {{ .IndexVar }} := range {{ .ValueExpression }}.{{ pascalize .Name }}Items { 1194 {{template "propertyvalidator" .AdditionalItems }} 1195 } 1196 {{ end }} 1197 return nil 1198 } 1199 {{ end }} 1200 {{ end }}