MCPcopy Create free account
hub / github.com/flant/shell-operator / ValidateOperationSpec

Function ValidateOperationSpec

pkg/kube/object_patch/validation.go:188–215  ·  view source on GitHub ↗

See https://github.com/kubernetes/apiextensions-apiserver/blob/1bb376f70aa2c6f2dec9a8c7f05384adbfac7fbb/pkg/apiserver/validation/validation.go#L47

(obj any, s *spec.Schema, rootName string)

Source from the content-addressed store, hash-verified

186
187// See https://github.com/kubernetes/apiextensions-apiserver/blob/1bb376f70aa2c6f2dec9a8c7f05384adbfac7fbb/pkg/apiserver/validation/validation.go#L47
188func ValidateOperationSpec(obj any, s *spec.Schema, rootName string) error {
189 if s == nil {
190 return fmt.Errorf("validate kubernetes patch spec: schema is not provided")
191 }
192
193 validator := validate.NewSchemaValidator(s, nil, rootName, strfmt.Default)
194
195 result := validator.Validate(obj)
196 if result.IsValid() {
197 return nil
198 }
199
200 allErrs := &multierror.Error{Errors: make([]error, 1)}
201 for _, err := range result.Errors {
202 allErrs = multierror.Append(allErrs, err)
203 }
204 // NOTE: no validation errors, but kubernetes patch spec is not valid!
205 if allErrs.Len() == 1 {
206 allErrs = multierror.Append(allErrs, fmt.Errorf("kubernetes patch spec is not valid"))
207 }
208
209 if allErrs.Len() > 1 {
210 yamlObj, _ := yaml.Marshal(obj)
211 allErrs.Errors[0] = fmt.Errorf("can't validate document:\n%s", yamlObj)
212 }
213
214 return allErrs
215}

Callers 1

ParseOperationsFunction · 0.85

Calls 2

IsValidMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected