doc.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. Copyright 2024 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package validate holds API validation functions which are designed for use
  14. // with the k8s.io/code-generator/cmd/validation-gen tool. Each validation
  15. // function has a similar fingerprint:
  16. //
  17. // func <Name>(ctx context.Context,
  18. // op operation.Operation,
  19. // fldPath *field.Path,
  20. // value, oldValue <nilable type>,
  21. // <other args...>) field.ErrorList
  22. //
  23. // The value and oldValue arguments will always be a nilable type. If the
  24. // original value was a string, these will be a *string. If the original value
  25. // was a slice or map, these will be the same slice or map type.
  26. //
  27. // For a CREATE operation, the oldValue will always be nil. For an UPDATE
  28. // operation, either value or oldValue may be nil, e.g. when adding or removing
  29. // a value in a list-map. Validators which care about UPDATE operations should
  30. // look at the opCtx argument to know which operation is being executed.
  31. //
  32. // Tightened validation (also known as ratcheting validation) is supported by
  33. // defining a new validation function. For example:
  34. //
  35. // func TightenedMaxLength(ctx context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *string) field.ErrorList {
  36. // if oldValue != nil && len(MaxLength(ctx, op, fldPath, oldValue, nil)) > 0 {
  37. // // old value is not valid, so this value skips the tightened validation
  38. // return nil
  39. // }
  40. // return MaxLength(ctx, op, fldPath, value, nil)
  41. // }
  42. //
  43. // In general, we cannot distinguish a non-specified slice or map from one that
  44. // is specified but empty. Validators should not rely on nil values, but use
  45. // len() instead.
  46. package validate