github.com/6543-forks/go-swagger@v0.26.0/generator/templates/schemavalidator.gotmpl (about) 1 {{ define "primitivefieldvalidator" }} 2 {{ if .Required }} 3 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ 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 {{ if .MinLength }} 8 if err := validate.MinLength({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, string({{ if .IsNullable }}*{{ end }}{{.ValueExpression }}), {{.MinLength }}); err != nil { 9 return err 10 } 11 {{ end }} 12 {{ if .MaxLength }} 13 if err := validate.MaxLength({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, string({{ if .IsNullable }}*{{ end }}{{.ValueExpression }}), {{.MaxLength }}); err != nil { 14 return err 15 } 16 {{ end }} 17 {{ if .Pattern }} 18 if err := validate.Pattern({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, string({{ if .IsNullable }}*{{ end }}{{.ValueExpression }}), `{{.Pattern }}`); err != nil { 19 return err 20 } 21 {{ end }} 22 {{ if .Minimum }} 23 if err := validate.Minimum{{ if eq .SwaggerType "integer" }}Int{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if eq .SwaggerType "integer" }}int{{ else }}float{{ end }}64({{ if .IsNullable }}*{{ end }}{{.ValueExpression }}), {{.Minimum }}, {{.ExclusiveMinimum }}); err != nil { 24 return err 25 } 26 {{ end }} 27 {{ if .Maximum }} 28 if err := validate.Maximum{{ if eq .SwaggerType "integer" }}Int{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if eq .SwaggerType "integer" }}int{{ else }}float{{ end }}64({{ if .IsNullable }}*{{ end }}{{.ValueExpression }}), {{.Maximum }}, {{.ExclusiveMaximum }}); err != nil { 29 return err 30 } 31 {{ end }} 32 {{ if .MultipleOf }} 33 if err := validate.MultipleOf({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, float64({{ if .IsNullable }}*{{ end }}{{.ValueExpression }}), {{.MultipleOf }}); err != nil { 34 return err 35 } 36 {{ end }} 37 {{ if .Enum }} 38 // value enum 39 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}{{ .Suffix }}Enum({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if .IsNullable }}*{{ end }}{{.ValueExpression }}); err != nil { 40 return err 41 } 42 {{ end }} 43 {{- if and .IsCustomFormatter (not .IsStream) (not .IsBase64) }} 44 {{ template "validationCustomformat" . }} 45 {{ end }} 46 {{ end }} 47 48 {{define "slicevalidator" }} 49 {{ if .Required }} 50 if err := validate.Required({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}); err != nil { 51 return err 52 } 53 {{ end }} 54 {{ if or .MinItems .MaxItems }} 55 {{ .IndexVar }}{{ pascalize .Name }}Size := int64(len({{.ValueExpression }})) 56 {{ end }} 57 {{ if .MinItems }} 58 if err := validate.MinItems({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ .IndexVar }}{{ pascalize .Name }}Size, {{.MinItems }}); err != nil { 59 return err 60 } 61 {{ end }} 62 {{ if .MaxItems }} 63 if err := validate.MaxItems({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ .IndexVar }}{{ pascalize .Name }}Size, {{.MaxItems }}); err != nil { 64 return err 65 } 66 {{ end }} 67 {{ if .UniqueItems }} 68 if err := validate.UniqueItems({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 69 return err 70 } 71 {{ end }} 72 {{ if .Enum }} 73 // for slice 74 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}Enum({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 75 return err 76 } 77 {{ end }} 78 {{ if .Items }} 79 {{- if and (or .Items.Required .Items.HasValidations .Items.IsBaseType .Items.IsAliased) (not .Items.IsInterface) (not .Items.IsStream) }} 80 for {{.IndexVar }} := 0; {{.IndexVar }} < len({{.ValueExpression }}); {{.IndexVar }}++ { 81 {{- with .Items }} 82 {{- if and .IsNullable (not .Required) (not .IsMapNullOverride) }} 83 if swag.IsZero({{ .ValueExpression }}) { // not required 84 continue 85 } 86 {{- end }} 87 {{ template "propertyvalidator" . }} 88 {{- end }} 89 } 90 {{- end }} 91 {{- else }} 92 {{- if or .IsAliased (ne .ValueExpression .ReceiverName) }}{{/* prevents generated code to call itself: this is reserved for aliased types */}} 93 {{- if and .IsNullable (not .IsMapNullOverride) }} 94 if {{ .ValueExpression }} != nil { 95 {{- end }} 96 if err := {{.ValueExpression }}.Validate(formats); err != nil { 97 if ve, ok := err.(*errors.Validation); ok { 98 return ve.ValidateName({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}) 99 } 100 return err 101 } 102 {{- if and .IsNullable (not .IsMapNullOverride) }} 103 } 104 {{- end }} 105 {{- end }} 106 {{- end }} 107 {{ end }} 108 {{ define "mapvalidator" }}{{/* validates additionalProperties */}} 109 {{ if .HasAdditionalProperties }} 110 {{- if .AdditionalProperties.HasValidations }} 111 {{- $validatedValues := .ValueExpression }}{{ $keyVar := .AdditionalProperties.KeyVar }} 112 for {{ .AdditionalProperties.KeyVar }} := range {{ .ValueExpression }} { 113 {{ with .AdditionalProperties }} 114 {{- if and (not .Required) .IsNullable }}{{/* skip when nul type is accepted */}} 115 if swag.IsZero({{ $validatedValues }}[{{ $keyVar }}]) { // not required 116 continue 117 } 118 {{- else if and (.Required) (not .IsArray) }}{{/* Required slice is processed below */}} 119 if err := validate.Required({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ $validatedValues }}[{{ $keyVar }}]); err != nil { 120 return err 121 } 122 {{- end }} 123 {{- if .IsPrimitive }} 124 {{- if .IsAliased }} 125 {{- if not .IsAnonymous }} 126 if val, ok := {{ $validatedValues }}[{{ $keyVar }}]; ok { 127 {{- if and .IsNullable (not .IsMapNullOverride) }} 128 if val != nil { 129 {{- end }} 130 if err := val.Validate(formats); err != nil { 131 return err 132 } 133 {{- if and .IsNullable (not .IsMapNullOverride) }} 134 } 135 {{- end }} 136 } 137 {{- else }}{{/* validation of anonymous objects */}} 138 {{ range .AllOf }} 139 {{ range .Properties }} 140 {{ template "propertyvalidator" . }} 141 {{ end }} 142 {{- end }} 143 {{ range .Properties }} 144 {{ template "propertyvalidator" . }} 145 {{ end }} 146 {{- end }} 147 {{ if and .IsTuple .AdditionalItems }} 148 // 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 */}} 149 // at https://github.com/go-swagger/go-swagger/issues 150 {{ end }} 151 {{ else }} 152 {{ template "primitivefieldvalidator" . }} 153 {{ end }} 154 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 155 {{- if .Required }} 156 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 157 return err 158 } 159 {{- end }} 160 {{- if and (not .IsStream) (not .IsBase64) }}{{/* TODO: IsStream and CustomFormattershould be mutually exclusive in type resolver */}} 161 {{ template "validationCustomformat" . }} 162 {{- end }} 163 {{ else if .IsArray }} 164 {{ template "slicevalidator" . }} 165 {{ else if .IsMap }} 166 {{ template "mapvalidator" . }} 167 {{ if .Enum }} 168 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}ValueEnum({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ $validatedValues }}[{{ $keyVar }}]); err != nil { 169 return err 170 } 171 {{- end }} 172 {{ else if or .IsComplexObject .IsTuple .IsAdditionalProperties .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.Validate(formats); err != nil { 179 return err 180 } 181 {{- if and .IsNullable (not .IsMapNullOverride) }} 182 } 183 {{- end }} 184 } 185 {{- else }} 186 {{ range .AllOf }} 187 {{ range .Properties }} 188 {{ template "propertyvalidator" . }} 189 {{ end }} 190 {{- end }} 191 {{ range .Properties }} 192 {{ template "propertyvalidator" . }} 193 {{- end }} 194 {{- end }} 195 {{ if and .IsTuple .AdditionalItems }} 196 // TODO: validating additional items should go here, if you see this raise an issue 197 // at https://github.com/go-swagger/go-swagger/issues 198 {{ end }} 199 {{ end }} 200 } 201 {{ end }} 202 {{ end }} 203 {{ if .Enum }} 204 // from map 205 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Enum({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ .ValueExpression }}); err != nil { 206 return err 207 } 208 {{ end }} 209 {{- else if .IsAliased }} 210 {{- if and .Required .IsInterface }} 211 if err := validate.Required({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 212 return err 213 } 214 {{- end }} 215 {{- if and .IsMap .HasValidations }}{{/* validation of aliased maps but does not know about AdditionalProperties: e.g. it comes from a $ref */}} 216 {{- if not .IsAnonymous }} 217 {{- if $.IsMap }}{{/* we come from a map range */}} 218 if val, ok := {{ .ValueExpression }}; ok { 219 {{- end }} 220 {{- if and .IsNullable (not .IsMapNullOverride) }} 221 {{- if $.IsMap }} 222 if val != nil { 223 {{- else }} 224 if {{ .ValueExpression }} != nil { 225 {{- end }} 226 {{- end }} 227 if err := {{ if $.IsMap }}val{{ else }}{{ .ValueExpression }}{{ end }}.Validate(formats); err != nil { 228 return err 229 } 230 {{- if and .IsNullable (not .IsMapNullOverride) }} 231 } 232 {{- end }} 233 {{- if or $.IsMap }} 234 } 235 {{- end }} 236 {{- end }} 237 {{- end }} 238 {{- end }} 239 {{ end }} 240 241 {{define "objectvalidator" }} 242 {{/* // DEBUG 243 // .Name: {{ .Name }} 244 // .IsAliased: {{ .IsAliased }} 245 // .IsAnonymous: {{ .IsAnonymous }} 246 // .IsNullable: {{ .IsNullable }} 247 // .Required: {{ .Required }} 248 // .IsBaseType: {{ .IsBaseType }} 249 // .ValueExpression: {{ .ValueExpression }} 250 // .ReceiverName: {{ .ReceiverName }} */}} 251 {{- if not .IsAnonymous }} 252 {{- if and .Required (or .IsNullable .IsBaseType) }} 253 if err := validate.Required({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{.ValueExpression }}); err != nil { 254 return err 255 } 256 {{- if and (not .Required) .IsBaseType }} 257 if {{ .ValueExpression }} == nil { 258 return nil 259 } 260 {{- end }} 261 {{ end }} 262 {{- if or .IsAliased (ne .ValueExpression .ReceiverName) }}{{/* prevents generated code to call itself: case of aliased types */}} 263 {{- if and .IsNullable (not .IsMapNullOverride) }} 264 if {{ .ValueExpression }} != nil { 265 {{- end }} 266 if err := {{.ValueExpression }}.Validate(formats); err != nil { 267 if ve, ok := err.(*errors.Validation); ok { 268 return ve.ValidateName({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}) 269 } 270 return err 271 } 272 {{- if and .IsNullable (not .IsMapNullOverride) }} 273 } 274 {{- end }} 275 {{- end }} 276 {{- else }} 277 {{ range .AllOf }} 278 {{ range .Properties }} 279 {{ template "propertyvalidator" . }} 280 {{ end }} 281 {{ end }} 282 {{ range .Properties }} 283 {{ template "propertyvalidator" . }} 284 {{ end }} 285 {{- end }} 286 {{- if and .IsTuple .AdditionalItems }} 287 // TODO: validating additional items should go here, if you see this raise an issue 288 // at https://github.com/go-swagger/go-swagger/issues 289 {{- end }} 290 {{ end }} 291 292 {{define "propertyvalidator" }} 293 {{- if .IsPrimitive }} 294 {{- if .IsAliased }} 295 {{ template "objectvalidator" . }} 296 {{- else }} 297 {{ template "primitivefieldvalidator" . }} 298 {{- end }} 299 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 300 {{- if .Required }} 301 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 302 return err 303 } 304 {{- end }} 305 {{- if and (not .IsStream) (not .IsBase64) }} 306 {{ template "validationCustomformat" . }} 307 {{- end }} 308 {{- else if .IsArray }} 309 {{ template "slicevalidator" . }} 310 {{- else if and .IsMap (or (not .IsAliased) (and .IsAliased .IsInterface)) }}{{/* except for interface, the renderinf for aliased maps is performed by objectvalidator */}} 311 {{ template "mapvalidator" . }} 312 {{- else if or .IsComplexObject .IsTuple .IsAdditionalProperties .IsAliased }} 313 {{ template "objectvalidator" . }} 314 {{- end }} 315 {{ end }} 316 317 {{ define "fieldvalidator"}} 318 {{- if .IsPrimitive }} 319 {{ template "primitivefieldvalidator" . }} 320 {{- else if and .IsCustomFormatter (or .HasValidations .Required) }}{{/* custom format not captured as primitive */}} 321 {{- if .Required }} 322 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 323 return err 324 } 325 {{- end }} 326 {{- if and (not .IsStream) (not .IsBase64) }} 327 {{ template "validationCustomformat" . }} 328 {{- end }} 329 {{- else if .IsArray }} 330 {{ template "slicevalidator" . }} 331 {{- else if .IsMap }} 332 {{ template "mapvalidator" . }} 333 {{- end }} 334 {{ end }} 335 336 {{define "schemavalidator" }} 337 {{ if .Enum }} 338 {{ if (eq .SwaggerType "string") }} 339 {{ $gotype := .GoType }} 340 const ( 341 {{ range .Enum }} 342 // {{ $gotype }}{{ pascalize . }} captures enum value {{ printf "%q" . }} 343 {{ $gotype }}{{ pascalize . }} {{ $gotype }} = {{ printf "%q" . }} 344 {{ end }} 345 ) 346 {{ end }} 347 348 // for schema 349 var {{ camelize .Name }}Enum []interface{} 350 func init() { 351 var res []{{ template "dereffedSchemaType" . }} 352 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 353 panic(err) 354 } 355 for _, v := range res { 356 {{ camelize .Name }}Enum = append({{ camelize .Name }}Enum, v) 357 } 358 } 359 360 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 { 361 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}Enum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 362 return err 363 } 364 return nil 365 } 366 {{ end }} 367 {{ if .ItemsEnum }} 368 var {{ camelize .Name }}ItemsEnum []interface{} 369 370 func init() { 371 var res []{{ template "dereffedSchemaType" .Items }} 372 if err := json.Unmarshal([]byte(`{{ json .ItemsEnum }}`), &res); err != nil { 373 panic(err) 374 } 375 for _, v := range res { 376 {{ camelize .Name }}ItemsEnum = append({{ camelize .Name }}ItemsEnum, v) 377 } 378 } 379 380 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 { 381 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 382 return err 383 } 384 return nil 385 } 386 {{ end }} 387 {{ with .AdditionalProperties }} 388 {{ if .Enum }} 389 // for additional props 390 var {{ camelize .Name }}ValueEnum []interface{} 391 392 func init() { 393 var res []{{ template "dereffedSchemaType" . }} 394 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 395 panic(err) 396 } 397 for _, v := range res { 398 {{ camelize .Name }}ValueEnum = append({{ camelize .Name }}ValueEnum, v) 399 } 400 } 401 402 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 { 403 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}ValueEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 404 return err 405 } 406 return nil 407 } 408 {{- end }} 409 {{ end }} 410 // Validate validates this {{ humanize .Name }} 411 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 { 412 var res []error 413 414 {{ range .AllOf }} 415 {{- if not .Properties }} 416 // validation for a type composition with {{ .GoType }} 417 {{- end }} 418 {{- if and (or .IsInterface .IsAnonymous .IsBaseType) (or .Required .HasValidations) }} 419 {{ template "fieldvalidator" . }} 420 421 {{ range .Properties }} 422 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 423 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}(formats); err != nil { 424 res = append(res, err) 425 } 426 {{- end }} 427 {{- end }} 428 {{- if and .HasAdditionalProperties (not .IsMap) }}{{/* validates additionalProperties in an object which is not itself a map */}} 429 {{ template "mapvalidator" . }} 430 {{- end }} 431 {{ if and .IsTuple .AdditionalItems }} 432 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Items(formats); err != nil { 433 res = append(res, err) 434 } 435 {{ end }} 436 {{- else if (or .Required .HasValidations) }} 437 if err := {{ .ReceiverName }}.{{ pascalize (dropPackage .GoType) }}.Validate(formats); err != nil { 438 res = append(res, err) 439 } 440 {{- end }} 441 {{- end }} 442 {{ template "fieldvalidator" . }} 443 {{ range .Properties }} 444 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 445 if err := {{.ReceiverName }}.validate{{ pascalize .Name }}(formats); err != nil { 446 res = append(res, err) 447 } 448 {{ end }} 449 {{ end }} 450 {{- if and .HasAdditionalProperties (not .IsMap) }}{{/* validates additionalProperties in an object which is not itself a map */}} 451 {{ template "mapvalidator" . }} 452 {{- end }} 453 {{ if and .IsTuple .AdditionalItems }}{{/* validates additionalItems in a tuple */}} 454 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Items(formats); err != nil { 455 res = append(res, err) 456 } 457 {{ end }} 458 {{ if and .Enum (not .IsPrimitive) (not .IsMap) }} 459 // value enum 460 if err := {{ .ReceiverName }}.validate{{ pascalize .Name }}Enum("", "body", {{ .ReceiverName }}); err != nil { 461 res = append(res, err) 462 } 463 {{ end }} 464 465 if len(res) > 0 { 466 return errors.CompositeValidationError(res...) 467 } 468 return nil 469 } 470 471 {{ range .Properties }} 472 {{ if or .Required .HasValidations }} 473 {{ if .Enum }} 474 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 475 476 func init() { 477 var res []{{ template "dereffedSchemaType" . }} 478 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 479 panic(err) 480 } 481 for _, v := range res { 482 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 483 } 484 } 485 486 {{ if (eq .SwaggerType "string") }} 487 {{ $gotype := .GoType }} 488 {{ $propname := .Name }} 489 const ( 490 {{ range .Enum }} 491 // {{ pascalize $.Name }}{{ pascalize $propname }}{{ pascalize . }} captures enum value {{ printf "%q" . }} 492 {{ pascalize $.Name }}{{ pascalize $propname }}{{ pascalize . }} {{ $gotype }} = {{ printf "%q" . }} 493 {{ end }} 494 ) 495 {{ end }} 496 497 // prop value enum 498 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 { 499 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 500 return err 501 } 502 return nil 503 } 504 {{ end }} 505 {{ if .ItemsEnum }} 506 var {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum []interface{} 507 func init() { 508 var res []{{ template "dereffedSchemaType" .Items }} 509 if err := json.Unmarshal([]byte(`{{ json .ItemsEnum }}`), &res); err != nil { 510 panic(err) 511 } 512 for _, v := range res { 513 {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, v) 514 } 515 } 516 517 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 { 518 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 519 return err 520 } 521 return nil 522 } 523 {{ end }} 524 {{ if .AdditionalItems }} 525 {{ if .AdditionalItems.Enum }} 526 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 527 528 func init() { 529 var res []{{ template "dereffedSchemaType" .AdditionalItems }} 530 if err := json.Unmarshal([]byte(`{{ json .AdditionalItems.Enum }}`), &res); err != nil { 531 panic(err) 532 } 533 for _, v := range res { 534 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 535 } 536 } 537 538 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 { 539 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 540 return err 541 } 542 return nil 543 } 544 {{ end }} 545 {{ end }} 546 {{ with .AdditionalProperties }} 547 {{ if .Enum }} 548 // additional properties value enum 549 var {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum []interface{} 550 551 func init() { 552 var res []{{ template "dereffedSchemaType" . }} 553 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 554 panic(err) 555 } 556 for _, v := range res { 557 {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, v) 558 } 559 } 560 561 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 { 562 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 563 return err 564 } 565 return nil 566 } 567 {{ end }} 568 {{ end }} 569 570 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 571 func ({{.ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}(formats strfmt.Registry) error { 572 {{ if not .Required }} 573 if swag.IsZero({{ .ValueExpression }}) { // not required 574 return nil 575 } 576 {{ end }} 577 {{- if and $.IsTuple .IsMap .Required }} 578 if err := validate.Required{{ if and (eq .GoType "string") (not .IsNullable) }}String{{ end }}({{ if .Path }}{{ .Path }}{{ else }}""{{ end }}, {{ printf "%q" .Location }}, {{ if not (or .IsAnonymous .IsNullable) }}{{ .GoType }}({{ end }}{{.ValueExpression }}{{ if not (or .IsAnonymous .IsNullable) }}){{ end }}); err != nil { 579 return err 580 } 581 {{- end }} 582 {{template "propertyvalidator" . }} 583 584 return nil 585 } 586 {{ end }} 587 {{ end }} 588 {{ end }} 589 {{ range .AllOf }} 590 {{ range .Properties }} 591 {{ if and (ne $.DiscriminatorField .Name) (or .Required .HasValidations) }} 592 {{ if .Enum }} 593 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 594 595 func init() { 596 var res []{{ template "dereffedSchemaType" . }} 597 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 598 panic(err) 599 } 600 for _, v := range res { 601 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 602 } 603 } 604 605 // property enum 606 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 { 607 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 608 return err 609 } 610 return nil 611 } 612 {{ end }} 613 {{ if .ItemsEnum }} 614 var {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum []interface{} 615 616 func init() { 617 var res []{{ template "dereffedSchemaType" .Items }} 618 if err := json.Unmarshal([]byte(`{{ json .ItemsEnum }}`), &res); err != nil { 619 panic(err) 620 } 621 for _, v := range res { 622 {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, v) 623 } 624 } 625 626 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 { 627 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 628 return err 629 } 630 return nil 631 } 632 {{ end }} 633 {{ if .AdditionalItems }} 634 {{ if .AdditionalItems.Enum }} 635 var {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum []interface{} 636 637 func init() { 638 var res []{{ template "dereffedSchemaType" .AdditionalItems }} 639 if err := json.Unmarshal([]byte(`{{ json .AdditionalItems.Enum }}`), &res); err != nil { 640 panic(err) 641 } 642 for _, v := range res { 643 {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum = append({{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, v) 644 } 645 } 646 647 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 { 648 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}Type{{ pascalize .Name }}PropEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 649 return err 650 } 651 return nil 652 } 653 {{ end }} 654 {{ end }} 655 {{ with .AdditionalProperties }} 656 {{ if .Enum }} 657 var {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum []interface{} 658 func init() { 659 var res []{{ template "dereffedSchemaType" . }} 660 if err := json.Unmarshal([]byte(`{{ json .Enum }}`), &res); err != nil { 661 panic(err) 662 } 663 for _, v := range res { 664 {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum = append({{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, v) 665 } 666 } 667 668 // additional properties value enum 669 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 { 670 if err := validate.EnumCase(path, location, value, {{ camelize $.Name }}{{ pascalize .Name }}ValueEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 671 return err 672 } 673 return nil 674 } 675 {{ end }} 676 {{ end }} 677 678 679 func ({{.ReceiverName }} *{{ if $.Discriminates }}{{ camelize $.Name }}{{ else if $.IsExported }}{{ pascalize $.Name }}{{ else }}{{ $.Name }}{{ end }}) validate{{ pascalize .Name }}(formats strfmt.Registry) error { 680 {{ if not .Required }} 681 if swag.IsZero({{ .ValueExpression }}) { // not required 682 return nil 683 } 684 {{ end }} 685 {{template "propertyvalidator" . }} 686 687 return nil 688 } 689 {{ end }} 690 {{ end }} 691 {{ end }} 692 693 {{ if .HasAdditionalItems }} 694 {{ if .AdditionalItems.Enum }} 695 var {{ camelize .Name }}ItemsEnum []interface{} 696 697 func init() { 698 var res []{{ template "dereffedSchemaType" .AdditionalItems }} 699 if err := json.Unmarshal([]byte(`{{ json .AdditionalItems.Enum }}`), &res); err != nil { 700 panic(err) 701 } 702 for _, v := range res { 703 {{ camelize .Name }}ItemsEnum = append({{ camelize .Name }}ItemsEnum, v) 704 } 705 } 706 707 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 { 708 if err := validate.EnumCase(path, location, value, {{ camelize .Name }}ItemsEnum, {{ if .IsEnumCI }}false{{ else }}true{{ end }}); err != nil { 709 return err 710 } 711 return nil 712 } 713 {{ end }} 714 func ({{.ReceiverName }} *{{ pascalize .Name }}) validate{{ pascalize .Name }}Items(formats strfmt.Registry) error { 715 {{ if or .AdditionalItems.Required .AdditionalItems.HasValidations }} 716 for {{ .IndexVar }} := range {{ .ValueExpression }}.{{ pascalize .Name }}Items { 717 {{template "propertyvalidator" .AdditionalItems }} 718 } 719 {{ end }} 720 return nil 721 } 722 {{ end }} 723 {{ end }}