package validate
import "k8s.io/apimachinery/pkg/api/validate"
Package validate holds API validation functions which are designed for use with the k8s.io/code-generator/cmd/validation-gen tool. Each validation function has a similar fingerprint:
func <Name>(ctx context.Context, op operation.Operation, fldPath *field.Path, value, oldValue <nilable type>, <other args...>) field.ErrorList
The value and oldValue arguments will always be a nilable type. If the original value was a string, these will be a *string. If the original value was a slice or map, these will be the same slice or map type.
For a CREATE operation, the oldValue will always be nil. For an UPDATE operation, either value or oldValue may be nil, e.g. when adding or removing a value in a list-map. Validators which care about UPDATE operations should look at the opCtx argument to know which operation is being executed.
Tightened validation (also known as ratcheting validation) is supported by defining a new validation function. For example:
func TightenedMaxLength(ctx context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *string) field.ErrorList { if oldValue != nil && len(MaxLength(ctx, op, fldPath, oldValue, nil)) > 0 { // old value is not valid, so this value skips the tightened validation return nil } return MaxLength(ctx, op, fldPath, value, nil) }
In general, we cannot distinguish a non-specified slice or map from one that is specified but empty. Validators should not rely on nil values, but use len() instead.
Index ¶
- func EachMapKey[K ~string, T any](ctx context.Context, op operation.Operation, fldPath *field.Path, newMap, oldMap map[K]T, validator ValidateFunc[*K]) field.ErrorList
- func EachMapVal[K ~string, V any](ctx context.Context, op operation.Operation, fldPath *field.Path, newMap, oldMap map[K]V, validator ValidateFunc[*V]) field.ErrorList
- func EachSliceVal[T any](ctx context.Context, op operation.Operation, fldPath *field.Path, newSlice, oldSlice []T, cmp CompareFunc[T], validator ValidateFunc[*T]) field.ErrorList
- func FixedResult[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, _ T, result bool, arg string) field.ErrorList
- func ForbiddenMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList
- func ForbiddenPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
- func ForbiddenSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList
- func ForbiddenValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
- func Immutable[T comparable](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *T) field.ErrorList
- func ImmutableNonComparable[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue T) field.ErrorList
- func Minimum[T constraints.Integer](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T, min T) field.ErrorList
- func OptionalMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList
- func OptionalPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
- func OptionalSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList
- func OptionalValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
- func RequiredMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList
- func RequiredPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
- func RequiredSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList
- func RequiredValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
- func Subfield[Tstruct any, Tfield any](ctx context.Context, op operation.Operation, fldPath *field.Path, newStruct, oldStruct *Tstruct, fldName string, getField GetFieldFunc[Tstruct, Tfield], validator ValidateFunc[Tfield]) field.ErrorList
- type CompareFunc
- type GetFieldFunc
- type ValidateFunc
Functions ¶
func EachMapKey ¶
func EachMapKey[K ~string, T any](ctx context.Context, op operation.Operation, fldPath *field.Path, newMap, oldMap map[K]T, validator ValidateFunc[*K]) field.ErrorList
EachMapKey validates each element of newMap with the specified validation function. The oldMap argument is not used.
func EachMapVal ¶
func EachMapVal[K ~string, V any](ctx context.Context, op operation.Operation, fldPath *field.Path, newMap, oldMap map[K]V, validator ValidateFunc[*V]) field.ErrorList
EachMapVal validates each element of newMap with the specified validation function and, if the corresponding key is found in oldMap, the old value. The value-type of the slices is assumed to not be nilable.
func EachSliceVal ¶
func EachSliceVal[T any](ctx context.Context, op operation.Operation, fldPath *field.Path, newSlice, oldSlice []T, cmp CompareFunc[T], validator ValidateFunc[*T]) field.ErrorList
EachSliceVal validates each element of newSlice with the specified validation function. The comparison function is used to find the corresponding value in oldSlice. The value-type of the slices is assumed to not be nilable.
func FixedResult ¶
func FixedResult[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, _ T, result bool, arg string) field.ErrorList
FixedResult asserts a fixed boolean result. This is mostly useful for testing.
func ForbiddenMap ¶
func ForbiddenMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList
RequiredMap verifies that the specified map is empty.
func ForbiddenPointer ¶
func ForbiddenPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
ForbiddenPointer verifies that the specified pointer is nil.
func ForbiddenSlice ¶
func ForbiddenSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList
ForbiddenSlice verifies that the specified slice is empty.
func ForbiddenValue ¶
func ForbiddenValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
ForbiddenValue verifies that the specified value is the zero-value for its type.
func Immutable ¶
func Immutable[T comparable](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *T) field.ErrorList
Immutable verifies that the specified value has not changed in the course of an update operation. It does nothing if the old value is not provided. If the caller needs to compare types that are not trivially comparable, they should use ImmutableNonComparable instead.
func ImmutableNonComparable ¶
func ImmutableNonComparable[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue T) field.ErrorList
ImmutableNonComparable verifies that the specified value has not changed in the course of an update operation. It does nothing if the old value is not provided. Unlike Immutable, this function can be used with types that are not directly comparable, at the cost of performance.
func Minimum ¶
func Minimum[T constraints.Integer](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T, min T) field.ErrorList
Minimum verifies that the specified value is greater than or equal to min.
func OptionalMap ¶
func OptionalMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList
OptionalMap verifies that the specified map is not empty. This is identical to RequiredMap, but the caller should treat an error here as an indication that the optional value was not specified.
func OptionalPointer ¶
func OptionalPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
OptionalPointer verifies that the specified pointer is not nil. This is identical to RequiredPointer, but the caller should treat an error here as an indication that the optional value was not specified.
func OptionalSlice ¶
func OptionalSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList
OptionalSlice verifies that the specified slice is not empty. This is identical to RequiredSlice, but the caller should treat an error here as an indication that the optional value was not specified.
func OptionalValue ¶
func OptionalValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
OptionalValue verifies that the specified value is not the zero-value for its type. This is identical to RequiredValue, but the caller should treat an error here as an indication that the optional value was not specified.
func RequiredMap ¶
func RequiredMap[K comparable, T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[K]T) field.ErrorList
RequiredMap verifies that the specified map is not empty.
func RequiredPointer ¶
func RequiredPointer[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
RequiredPointer verifies that the specified pointer is not nil.
func RequiredSlice ¶
func RequiredSlice[T any](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []T) field.ErrorList
RequiredSlice verifies that the specified slice is not empty.
func RequiredValue ¶
func RequiredValue[T comparable](_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *T) field.ErrorList
RequiredValue verifies that the specified value is not the zero-value for its type.
func Subfield ¶
func Subfield[Tstruct any, Tfield any](ctx context.Context, op operation.Operation, fldPath *field.Path, newStruct, oldStruct *Tstruct, fldName string, getField GetFieldFunc[Tstruct, Tfield], validator ValidateFunc[Tfield]) field.ErrorList
Subfield validates a subfield of a struct against a validator function.
Types ¶
type CompareFunc ¶
CompareFunc is a function that compares two values of the same type.
type GetFieldFunc ¶
GetFieldFunc is a function that extracts a field from a type and returns a nilable value.
type ValidateFunc ¶
type ValidateFunc[T any] func(ctx context.Context, op operation.Operation, fldPath *field.Path, newValue, oldValue T) field.ErrorList
ValidateFunc is a function that validates a value, possibly considering the old value (if any).
Source Files ¶
common.go doc.go each.go immutable.go limits.go required.go subfield.go testing.go
Directories ¶
Path | Synopsis |
---|---|
pkg/api/validate/constraints | |
pkg/api/validate/content |
- Version
- v0.33.0 (latest)
- Published
- Apr 11, 2025
- Platform
- linux/amd64
- Imports
- 6 packages
- Last checked
- 3 hours ago –
Tools for package owners.