knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/cronjob_validation.go (about) 1 /* 2 Copyright 2021 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1 18 19 import ( 20 "context" 21 22 "knative.dev/pkg/apis" 23 ) 24 25 // CronJobValidator is a callback to validate a CronJob. 26 type CronJobValidator func(context.Context, *CronJob) *apis.FieldError 27 28 // Validate implements apis.Validatable 29 func (c *CronJob) Validate(ctx context.Context) *apis.FieldError { 30 if cv := GetCronJobValidator(ctx); cv != nil { 31 return cv(ctx, c) 32 } 33 return nil 34 } 35 36 // cvKey is used for associating a CronJobValidator with a context.Context 37 type cvKey struct{} 38 39 func WithCronJobValidator(ctx context.Context, cv CronJobValidator) context.Context { 40 return context.WithValue(ctx, cvKey{}, cv) 41 } 42 43 // GetCronJobValidator extracts the CronJobValidator from the context. 44 func GetCronJobValidator(ctx context.Context) CronJobValidator { 45 untyped := ctx.Value(cvKey{}) 46 if untyped == nil { 47 return nil 48 } 49 return untyped.(CronJobValidator) 50 }