podman

Форк
0
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

15
package validate
16

17
import (
18
	"fmt"
19
	"reflect"
20

21
	"github.com/go-openapi/spec"
22
	"github.com/go-openapi/strfmt"
23
)
24

25
type schemaSliceValidator struct {
26
	Path            string
27
	In              string
28
	MaxItems        *int64
29
	MinItems        *int64
30
	UniqueItems     bool
31
	AdditionalItems *spec.SchemaOrBool
32
	Items           *spec.SchemaOrArray
33
	Root            interface{}
34
	KnownFormats    strfmt.Registry
35
	Options         SchemaValidatorOptions
36
}
37

38
func (s *schemaSliceValidator) SetPath(path string) {
39
	s.Path = path
40
}
41

42
func (s *schemaSliceValidator) Applies(source interface{}, kind reflect.Kind) bool {
43
	_, ok := source.(*spec.Schema)
44
	r := ok && kind == reflect.Slice
45
	return r
46
}
47

48
func (s *schemaSliceValidator) Validate(data interface{}) *Result {
49
	result := new(Result)
50
	if data == nil {
51
		return result
52
	}
53
	val := reflect.ValueOf(data)
54
	size := val.Len()
55

56
	if s.Items != nil && s.Items.Schema != nil {
57
		validator := NewSchemaValidator(s.Items.Schema, s.Root, s.Path, s.KnownFormats, s.Options.Options()...)
58
		for i := 0; i < size; i++ {
59
			validator.SetPath(fmt.Sprintf("%s.%d", s.Path, i))
60
			value := val.Index(i)
61
			result.mergeForSlice(val, i, validator.Validate(value.Interface()))
62
		}
63
	}
64

65
	itemsSize := 0
66
	if s.Items != nil && len(s.Items.Schemas) > 0 {
67
		itemsSize = len(s.Items.Schemas)
68
		for i := 0; i < itemsSize; i++ {
69
			validator := NewSchemaValidator(&s.Items.Schemas[i], s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats, s.Options.Options()...)
70
			if val.Len() <= i {
71
				break
72
			}
73
			result.mergeForSlice(val, i, validator.Validate(val.Index(i).Interface()))
74
		}
75
	}
76
	if s.AdditionalItems != nil && itemsSize < size {
77
		if s.Items != nil && len(s.Items.Schemas) > 0 && !s.AdditionalItems.Allows {
78
			result.AddErrors(arrayDoesNotAllowAdditionalItemsMsg())
79
		}
80
		if s.AdditionalItems.Schema != nil {
81
			for i := itemsSize; i < size-itemsSize+1; i++ {
82
				validator := NewSchemaValidator(s.AdditionalItems.Schema, s.Root, fmt.Sprintf("%s.%d", s.Path, i), s.KnownFormats, s.Options.Options()...)
83
				result.mergeForSlice(val, i, validator.Validate(val.Index(i).Interface()))
84
			}
85
		}
86
	}
87

88
	if s.MinItems != nil {
89
		if err := MinItems(s.Path, s.In, int64(size), *s.MinItems); err != nil {
90
			result.AddErrors(err)
91
		}
92
	}
93
	if s.MaxItems != nil {
94
		if err := MaxItems(s.Path, s.In, int64(size), *s.MaxItems); err != nil {
95
			result.AddErrors(err)
96
		}
97
	}
98
	if s.UniqueItems {
99
		if err := UniqueItems(s.Path, s.In, val.Interface()); err != nil {
100
			result.AddErrors(err)
101
		}
102
	}
103
	result.Inc()
104
	return result
105
}
106

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.