You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

slice_validator.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package validate
  15. import (
  16. "fmt"
  17. "reflect"
  18. "github.com/go-openapi/spec"
  19. "github.com/go-openapi/strfmt"
  20. )
  21. type schemaSliceValidator struct {
  22. Path string
  23. In string
  24. MaxItems *int64
  25. MinItems *int64
  26. UniqueItems bool
  27. AdditionalItems *spec.SchemaOrBool
  28. Items *spec.SchemaOrArray
  29. Root interface{}
  30. KnownFormats strfmt.Registry
  31. }
  32. func (s *schemaSliceValidator) SetPath(path string) {
  33. s.Path = path
  34. }
  35. func (s *schemaSliceValidator) Applies(source interface{}, kind reflect.Kind) bool {
  36. _, ok := source.(*spec.Schema)
  37. r := ok && kind == reflect.Slice
  38. return r
  39. }
  40. func (s *schemaSliceValidator) Validate(data interface{}) *Result {
  41. result := new(Result)
  42. if data == nil {
  43. return result
  44. }
  45. val := reflect.ValueOf(data)
  46. size := val.Len()
  47. if s.Items != nil && s.Items.Schema != nil {
  48. validator := NewSchemaValidator(s.Items.Schema, s.Root, s.Path, s.KnownFormats)
  49. for i := 0; i < size; i++ {
  50. validator.SetPath(fmt.Sprintf("%s.%d", s.Path, i))
  51. value := val.Index(i)
  52. result.mergeForSlice(val, i, validator.Validate(value.Interface()))
  53. }
  54. }
  55. itemsSize := 0
  56. if s.Items != nil && len(s.Items.Schemas) > 0 {
  57. itemsSize = len(s.Items.Schemas)
  58. for i := 0; i < itemsSize; i++ {
  59. validator := NewSchemaValidator(&s.Items.Schemas[i], s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats)
  60. if val.Len() <= i {
  61. break
  62. }
  63. result.mergeForSlice(val, int(i), validator.Validate(val.Index(i).Interface()))
  64. }
  65. }
  66. if s.AdditionalItems != nil && itemsSize < size {
  67. if s.Items != nil && len(s.Items.Schemas) > 0 && !s.AdditionalItems.Allows {
  68. result.AddErrors(arrayDoesNotAllowAdditionalItemsMsg())
  69. }
  70. if s.AdditionalItems.Schema != nil {
  71. for i := itemsSize; i < size-itemsSize+1; i++ {
  72. validator := NewSchemaValidator(s.AdditionalItems.Schema, s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats)
  73. result.mergeForSlice(val, int(i), validator.Validate(val.Index(int(i)).Interface()))
  74. }
  75. }
  76. }
  77. if s.MinItems != nil {
  78. if err := MinItems(s.Path, s.In, int64(size), *s.MinItems); err != nil {
  79. result.AddErrors(err)
  80. }
  81. }
  82. if s.MaxItems != nil {
  83. if err := MaxItems(s.Path, s.In, int64(size), *s.MaxItems); err != nil {
  84. result.AddErrors(err)
  85. }
  86. }
  87. if s.UniqueItems {
  88. if err := UniqueItems(s.Path, s.In, val.Interface()); err != nil {
  89. result.AddErrors(err)
  90. }
  91. }
  92. result.Inc()
  93. return result
  94. }