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.

105 lines
3.0 KiB

  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. Options SchemaValidatorOptions
  32. }
  33. func (s *schemaSliceValidator) SetPath(path string) {
  34. s.Path = path
  35. }
  36. func (s *schemaSliceValidator) Applies(source interface{}, kind reflect.Kind) bool {
  37. _, ok := source.(*spec.Schema)
  38. r := ok && kind == reflect.Slice
  39. return r
  40. }
  41. func (s *schemaSliceValidator) Validate(data interface{}) *Result {
  42. result := new(Result)
  43. if data == nil {
  44. return result
  45. }
  46. val := reflect.ValueOf(data)
  47. size := val.Len()
  48. if s.Items != nil && s.Items.Schema != nil {
  49. validator := NewSchemaValidator(s.Items.Schema, s.Root, s.Path, s.KnownFormats, s.Options.Options()...)
  50. for i := 0; i < size; i++ {
  51. validator.SetPath(fmt.Sprintf("%s.%d", s.Path, i))
  52. value := val.Index(i)
  53. result.mergeForSlice(val, i, validator.Validate(value.Interface()))
  54. }
  55. }
  56. itemsSize := 0
  57. if s.Items != nil && len(s.Items.Schemas) > 0 {
  58. itemsSize = len(s.Items.Schemas)
  59. for i := 0; i < itemsSize; i++ {
  60. validator := NewSchemaValidator(&s.Items.Schemas[i], s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats, s.Options.Options()...)
  61. if val.Len() <= i {
  62. break
  63. }
  64. result.mergeForSlice(val, i, validator.Validate(val.Index(i).Interface()))
  65. }
  66. }
  67. if s.AdditionalItems != nil && itemsSize < size {
  68. if s.Items != nil && len(s.Items.Schemas) > 0 && !s.AdditionalItems.Allows {
  69. result.AddErrors(arrayDoesNotAllowAdditionalItemsMsg())
  70. }
  71. if s.AdditionalItems.Schema != nil {
  72. for i := itemsSize; i < size-itemsSize+1; i++ {
  73. validator := NewSchemaValidator(s.AdditionalItems.Schema, s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats, s.Options.Options()...)
  74. result.mergeForSlice(val, i, validator.Validate(val.Index(i).Interface()))
  75. }
  76. }
  77. }
  78. if s.MinItems != nil {
  79. if err := MinItems(s.Path, s.In, int64(size), *s.MinItems); err != nil {
  80. result.AddErrors(err)
  81. }
  82. }
  83. if s.MaxItems != nil {
  84. if err := MaxItems(s.Path, s.In, int64(size), *s.MaxItems); err != nil {
  85. result.AddErrors(err)
  86. }
  87. }
  88. if s.UniqueItems {
  89. if err := UniqueItems(s.Path, s.In, val.Interface()); err != nil {
  90. result.AddErrors(err)
  91. }
  92. }
  93. result.Inc()
  94. return result
  95. }