Debian Codegraph @

pkg/validation/validate/formats.go

raw symbols SCIP
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 "k8s.io/kube-openapi/pkg/validation/spec"
21 "k8s.io/kube-openapi/pkg/validation/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.Schema:
42 return kind == reflect.String && f.KnownFormats.ContainsName(source.Format)
43 }
44 return false
45 }
46 r := doit()
47 debugLog("format validator for %q applies %t for %T (kind: %v)\n", f.Path, r, source, kind)
48 return r
49 }
50
51 func (f *formatValidator) Validate(val interface{}) *Result {
52 result := new(Result)
53 debugLog("validating \"%v\" against format: %s", val, f.Format)
54
55 if err := FormatOf(f.Path, f.In, f.Format, val.(string), f.KnownFormats); err != nil {
56 result.AddErrors(err)
57 }
58
59 if result.HasErrors() {
60 return result
61 }
62 return nil
63 }