required.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/api/operation"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. )
  19. // RequiredValue verifies that the specified value is not the zero-value for
  20. // its type.
  21. func RequiredValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList {
  22. var zero T
  23. if *value != zero {
  24. return nil
  25. }
  26. return field.ErrorList{field.Required(fldPath, "")}
  27. }
  28. // RequiredPointer verifies that the specified pointer is not nil.
  29. func RequiredPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList {
  30. if value != nil {
  31. return nil
  32. }
  33. return field.ErrorList{field.Required(fldPath, "")}
  34. }
  35. // RequiredSlice verifies that the specified slice is not empty.
  36. func RequiredSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList {
  37. if len(value) > 0 {
  38. return nil
  39. }
  40. return field.ErrorList{field.Required(fldPath, "")}
  41. }
  42. // RequiredMap verifies that the specified map is not empty.
  43. func RequiredMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList {
  44. if len(value) > 0 {
  45. return nil
  46. }
  47. return field.ErrorList{field.Required(fldPath, "")}
  48. }
  49. // ForbiddenValue verifies that the specified value is the zero-value for its
  50. // type.
  51. func ForbiddenValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList {
  52. var zero T
  53. if *value == zero {
  54. return nil
  55. }
  56. return field.ErrorList{field.Forbidden(fldPath, "")}
  57. }
  58. // ForbiddenPointer verifies that the specified pointer is nil.
  59. func ForbiddenPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList {
  60. if value == nil {
  61. return nil
  62. }
  63. return field.ErrorList{field.Forbidden(fldPath, "")}
  64. }
  65. // ForbiddenSlice verifies that the specified slice is empty.
  66. func ForbiddenSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList {
  67. if len(value) == 0 {
  68. return nil
  69. }
  70. return field.ErrorList{field.Forbidden(fldPath, "")}
  71. }
  72. // ForbiddenMap verifies that the specified map is empty.
  73. func ForbiddenMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList {
  74. if len(value) == 0 {
  75. return nil
  76. }
  77. return field.ErrorList{field.Forbidden(fldPath, "")}
  78. }
  79. // OptionalValue verifies that the specified value is not the zero-value for
  80. // its type. This is identical to RequiredValue, but the caller should treat an
  81. // error here as an indication that the optional value was not specified.
  82. func OptionalValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList {
  83. var zero T
  84. if *value != zero {
  85. return nil
  86. }
  87. return field.ErrorList{field.Required(fldPath, "optional value was not specified")}
  88. }
  89. // OptionalPointer verifies that the specified pointer is not nil. This is
  90. // identical to RequiredPointer, but the caller should treat an error here as an
  91. // indication that the optional value was not specified.
  92. func OptionalPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList {
  93. if value != nil {
  94. return nil
  95. }
  96. return field.ErrorList{field.Required(fldPath, "optional value was not specified")}
  97. }
  98. // OptionalSlice verifies that the specified slice is not empty. This is
  99. // identical to RequiredSlice, but the caller should treat an error here as an
  100. // indication that the optional value was not specified.
  101. func OptionalSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList {
  102. if len(value) > 0 {
  103. return nil
  104. }
  105. return field.ErrorList{field.Required(fldPath, "optional value was not specified")}
  106. }
  107. // OptionalMap verifies that the specified map is not empty. This is identical
  108. // to RequiredMap, but the caller should treat an error here as an indication that
  109. // the optional value was not specified.
  110. func OptionalMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList {
  111. if len(value) > 0 {
  112. return nil
  113. }
  114. return field.ErrorList{field.Required(fldPath, "optional value was not specified")}
  115. }