...
1
2
3
4
5
6
7
8
9
10
11
12
13
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 }
64
View as plain text