MCPcopy Index your code
hub / github.com/crossplane-contrib/function-patch-and-transform / ValidatePatch

Function ValidatePatch

validate.go:163–222  ·  view source on GitHub ↗

ValidatePatch validates a ComposedPatch.

(p PatchInterface)

Source from the content-addressed store, hash-verified

161
162// ValidatePatch validates a ComposedPatch.
163func ValidatePatch(p PatchInterface) *field.Error { //nolint: gocyclo // This is a long but simple/same-y switch.
164 switch p.GetType() {
165 case v1beta1.PatchTypeFromCompositeFieldPath,
166 v1beta1.PatchTypeToCompositeFieldPath,
167 v1beta1.PatchTypeFromEnvironmentFieldPath,
168 v1beta1.PatchTypeToEnvironmentFieldPath:
169 if p.GetFromFieldPath() == "" {
170 return field.Required(field.NewPath("fromFieldPath"), fmt.Sprintf("fromFieldPath must be set for patch type %s", p.GetType()))
171 }
172 case v1beta1.PatchTypePatchSet:
173 ps, ok := p.(PatchWithPatchSetName)
174 if !ok {
175 return field.Invalid(field.NewPath("type"), p.GetType(), fmt.Sprintf("patch type %T does not support patch of type %s", p, p.GetType()))
176 }
177 if ps.GetPatchSetName() == "" {
178 return field.Required(field.NewPath("patchSetName"), fmt.Sprintf("patchSetName must be set for patch type %s", p.GetType()))
179 }
180 case v1beta1.PatchTypeCombineFromComposite,
181 v1beta1.PatchTypeCombineToComposite,
182 v1beta1.PatchTypeCombineFromEnvironment,
183 v1beta1.PatchTypeCombineToEnvironment:
184 if p.GetCombine() == nil {
185 return field.Required(field.NewPath("combine"), fmt.Sprintf("combine must be set for patch type %s", p.GetType()))
186 }
187 if p.GetToFieldPath() == "" {
188 return field.Required(field.NewPath("toFieldPath"), fmt.Sprintf("toFieldPath must be set for patch type %s", p.GetType()))
189 }
190 return WrapFieldError(ValidateCombine(p.GetCombine()), field.NewPath("combine"))
191 default:
192 // Should never happen
193 return field.Invalid(field.NewPath("type"), p.GetType(), "unknown patch type")
194 }
195 for i, t := range p.GetTransforms() {
196 if err := ValidateTransform(t); err != nil {
197 return WrapFieldError(err, field.NewPath("transforms").Index(i))
198 }
199 }
200 if pp := p.GetPolicy(); pp != nil {
201 switch pp.GetToFieldPathPolicy() {
202 case v1beta1.ToFieldPathPolicyReplace,
203 v1beta1.ToFieldPathPolicyMergeObjects,
204 v1beta1.ToFieldPathPolicyMergeObjectsAppendArrays,
205 v1beta1.ToFieldPathPolicyForceMergeObjects,
206 v1beta1.ToFieldPathPolicyForceMergeObjectsAppendArrays,
207 v1beta1.ToFieldPathPolicyMergeObject, //nolint:staticcheck // MergeObject is deprecated but we must still support it.
208 v1beta1.ToFieldPathPolicyAppendArray: //nolint:staticcheck // AppendArray is deprecated but we must still support it.
209 // ok
210 default:
211 return field.Invalid(field.NewPath("policy", "toFieldPathPolicy"), pp.GetToFieldPathPolicy(), "unknown toFieldPathPolicy")
212 }
213 switch pp.GetFromFieldPathPolicy() {
214 case v1beta1.FromFieldPathPolicyRequired,
215 v1beta1.FromFieldPathPolicyOptional:
216 // ok
217 default:
218 return field.Invalid(field.NewPath("policy", "fromFieldPathPolicy"), pp.GetFromFieldPathPolicy(), "unknown fromFieldPathPolicy")
219 }
220 }

Callers 4

ValidateComposedTemplateFunction · 0.85
ValidatePatchSetFunction · 0.85
ValidateEnvironmentFunction · 0.85
TestValidatePatchFunction · 0.85

Calls 12

WrapFieldErrorFunction · 0.85
ValidateCombineFunction · 0.85
ValidateTransformFunction · 0.85
GetToFieldPathPolicyMethod · 0.80
GetTypeMethod · 0.65
GetFromFieldPathMethod · 0.65
GetPatchSetNameMethod · 0.65
GetCombineMethod · 0.65
GetToFieldPathMethod · 0.65
GetTransformsMethod · 0.65
GetPolicyMethod · 0.65

Tested by 1

TestValidatePatchFunction · 0.68