podman
105 строк · 3.0 Кб
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
15package validate
16
17import (
18"fmt"
19"reflect"
20
21"github.com/go-openapi/spec"
22"github.com/go-openapi/strfmt"
23)
24
25type schemaSliceValidator struct {
26Path string
27In string
28MaxItems *int64
29MinItems *int64
30UniqueItems bool
31AdditionalItems *spec.SchemaOrBool
32Items *spec.SchemaOrArray
33Root interface{}
34KnownFormats strfmt.Registry
35Options SchemaValidatorOptions
36}
37
38func (s *schemaSliceValidator) SetPath(path string) {
39s.Path = path
40}
41
42func (s *schemaSliceValidator) Applies(source interface{}, kind reflect.Kind) bool {
43_, ok := source.(*spec.Schema)
44r := ok && kind == reflect.Slice
45return r
46}
47
48func (s *schemaSliceValidator) Validate(data interface{}) *Result {
49result := new(Result)
50if data == nil {
51return result
52}
53val := reflect.ValueOf(data)
54size := val.Len()
55
56if s.Items != nil && s.Items.Schema != nil {
57validator := NewSchemaValidator(s.Items.Schema, s.Root, s.Path, s.KnownFormats, s.Options.Options()...)
58for i := 0; i < size; i++ {
59validator.SetPath(fmt.Sprintf("%s.%d", s.Path, i))
60value := val.Index(i)
61result.mergeForSlice(val, i, validator.Validate(value.Interface()))
62}
63}
64
65itemsSize := 0
66if s.Items != nil && len(s.Items.Schemas) > 0 {
67itemsSize = len(s.Items.Schemas)
68for i := 0; i < itemsSize; i++ {
69validator := NewSchemaValidator(&s.Items.Schemas[i], s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats, s.Options.Options()...)
70if val.Len() <= i {
71break
72}
73result.mergeForSlice(val, i, validator.Validate(val.Index(i).Interface()))
74}
75}
76if s.AdditionalItems != nil && itemsSize < size {
77if s.Items != nil && len(s.Items.Schemas) > 0 && !s.AdditionalItems.Allows {
78result.AddErrors(arrayDoesNotAllowAdditionalItemsMsg())
79}
80if s.AdditionalItems.Schema != nil {
81for i := itemsSize; i < size-itemsSize+1; i++ {
82validator := NewSchemaValidator(s.AdditionalItems.Schema, s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats, s.Options.Options()...)
83result.mergeForSlice(val, i, validator.Validate(val.Index(i).Interface()))
84}
85}
86}
87
88if s.MinItems != nil {
89if err := MinItems(s.Path, s.In, int64(size), *s.MinItems); err != nil {
90result.AddErrors(err)
91}
92}
93if s.MaxItems != nil {
94if err := MaxItems(s.Path, s.In, int64(size), *s.MaxItems); err != nil {
95result.AddErrors(err)
96}
97}
98if s.UniqueItems {
99if err := UniqueItems(s.Path, s.In, val.Interface()); err != nil {
100result.AddErrors(err)
101}
102}
103result.Inc()
104return result
105}
106