MCPcopy
hub / github.com/determined-ai/determined / validate

Function validate

master/pkg/check/validate.go:44–86  ·  view source on GitHub ↗
(v reflect.Value, path interface{})

Source from the content-addressed store, hash-verified

42}
43
44func validate(v reflect.Value, path interface{}) []error {
45 var errs []error
46 switch v.Kind() {
47 case reflect.Ptr:
48 if v.IsNil() {
49 return nil
50 }
51 errs = append(errs, validate(v.Elem(), path)...)
52 case reflect.Slice:
53 for i := 0; i < v.Len(); i++ {
54 errs = append(errs, validate(v.Index(i), fmt.Sprintf("%s[%d]", path, i))...)
55 }
56 case reflect.Map:
57 for _, key := range v.MapKeys() {
58 errs = append(errs, validate(v.MapIndex(key),
59 fmt.Sprintf("%s[%s]", path, key.Interface()))...)
60 }
61 case reflect.Struct:
62 for i := 0; i < v.NumField(); i++ {
63 // Skip non-exported fields.
64 if !v.Field(i).CanInterface() {
65 continue
66 }
67
68 errs = append(errs, validate(v.Field(i),
69 fmt.Sprintf("%s.%s", path, v.Type().Field(i).Name))...)
70 }
71 }
72
73 if v.Kind() != reflect.Ptr {
74 vp := reflect.New(v.Type())
75 vp.Elem().Set(v)
76 if validatable, ok := vp.Interface().(Validatable); ok {
77 for _, err := range validatable.Validate() {
78 if err != nil {
79 errs = append(errs, errors.Wrapf(err, "error found at %s", path))
80 }
81 }
82 }
83 }
84
85 return errs
86}

Callers 1

ValidateFunction · 0.70

Calls 6

NewMethod · 0.80
TypeMethod · 0.65
ValidateMethod · 0.65
appendFunction · 0.50
LenMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected