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