podman

Форк
0
69 строк · 1.9 Кб
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
	"reflect"
19

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

24
type formatValidator struct {
25
	Format       string
26
	Path         string
27
	In           string
28
	KnownFormats strfmt.Registry
29
}
30

31
func (f *formatValidator) SetPath(path string) {
32
	f.Path = path
33
}
34

35
func (f *formatValidator) Applies(source interface{}, kind reflect.Kind) bool {
36
	doit := func() bool {
37
		if source == nil {
38
			return false
39
		}
40
		switch source := source.(type) {
41
		case *spec.Items:
42
			return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
43
		case *spec.Parameter:
44
			return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
45
		case *spec.Schema:
46
			return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
47
		case *spec.Header:
48
			return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
49
		}
50
		return false
51
	}
52
	r := doit()
53
	debugLog("format validator for %q applies %t for %T (kind: %v)\n", f.Path, r, source, kind)
54
	return r
55
}
56

57
func (f *formatValidator) Validate(val interface{}) *Result {
58
	result := new(Result)
59
	debugLog("validating \"%v\" against format: %s", val, f.Format)
60

61
	if err := FormatOf(f.Path, f.In, f.Format, val.(string), f.KnownFormats); err != nil {
62
		result.AddErrors(err)
63
	}
64

65
	if result.HasErrors() {
66
		return result
67
	}
68
	return nil
69
}
70

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

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

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

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