podman
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
15package validate
16
17import (
18"reflect"
19
20"github.com/go-openapi/spec"
21"github.com/go-openapi/strfmt"
22)
23
24type formatValidator struct {
25Format string
26Path string
27In string
28KnownFormats strfmt.Registry
29}
30
31func (f *formatValidator) SetPath(path string) {
32f.Path = path
33}
34
35func (f *formatValidator) Applies(source interface{}, kind reflect.Kind) bool {
36doit := func() bool {
37if source == nil {
38return false
39}
40switch source := source.(type) {
41case *spec.Items:
42return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
43case *spec.Parameter:
44return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
45case *spec.Schema:
46return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
47case *spec.Header:
48return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
49}
50return false
51}
52r := doit()
53debugLog("format validator for %q applies %t for %T (kind: %v)\n", f.Path, r, source, kind)
54return r
55}
56
57func (f *formatValidator) Validate(val interface{}) *Result {
58result := new(Result)
59debugLog("validating \"%v\" against format: %s", val, f.Format)
60
61if err := FormatOf(f.Path, f.In, f.Format, val.(string), f.KnownFormats); err != nil {
62result.AddErrors(err)
63}
64
65if result.HasErrors() {
66return result
67}
68return nil
69}
70