Skip to content

Commit 27d9645

Browse files
committed
docs: add Declarative Validation Native (DV Native) to api-conventions.md and api_changes.md
1 parent 76d68e6 commit 27d9645

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

contributors/devel/sig-architecture/api-conventions.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,13 @@ type definitions, or manually, by writing validation functions. For all new
19821982
APIs, declarative validation is the preferred approach for the validation rules
19831983
it supports. For more information see the
19841984
[declarative validation documentation](api_changes.md#declarative-validation).
1985+
1986+
Additionally, "Declarative Validation Native" (DV Native) is a mode that allows
1987+
net-new API fields and their validations to be defined exclusively using Go
1988+
comment tags, without requiring a parallel hand-written implementation in Go.
1989+
See the [DV Native documentation](api_changes.md#declarative-validation-native-dv-native)
1990+
for more details.
1991+
19851992
Validation errors are flagged and returned to the caller in a `Failure` status,
19861993
usually with `reason` set to `Invalid`.
19871994

contributors/devel/sig-architecture/api_changes.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,45 @@ Users will need to write go unit tests similar to what is done for hand-written
706706

707707
While the goal is to express as much validation declaratively as possible, some complex or validation rules might still require manual implementation in `validation.go`.
708708

709+
#### Declarative Validation Native (DV Native)
710+
711+
"Declarative Validation Native" (DV Native) is a mode of operation for the declarative validation framework that allows net-new API fields and their validations to be defined exclusively using Go comment tags, without requiring a parallel hand-written implementation in Go.
712+
713+
This simplifies API development by making declarative tags the single source of truth for validation, reducing hand-written boilerplate code, and ensuring consistency between API definitions and enforcement logic.
714+
715+
To opt-in a field to DV Native mode, use the `+k8s:declarativeValidationNative` tag on the field in `types.go`. For example, if adding a net-new `Name` field and associated validation for an API, you would add the following in the associated `types.go` file. Note that this `+k8s:declarativeValidationNative` tag is added as well as the actual validation tags to be used (in this case `+k8s:required` and `+k8s:format=k8s-short-name`):
716+
717+
```go
718+
// +k8s:required
719+
// +k8s:format=k8s-short-name
720+
// +k8s:declarativeValidationNative
721+
Name string `json:"name" protobuf:"bytes,1,opt,name=name"`
722+
```
723+
724+
By default, fields marked with `+k8s:declarativeValidationNative` are only allowed to use **stable** validation tags (e.g., `+k8s:required`, `+k8s:minimum`, `+k8s:maxLength`, `+k8s:format`, `+k8s:enum`, etc.). An up to date list of each tag and their associated Stability Level can be found at the kubernetes.io [Declarative Validation Tag Catalog](https://kubernetes.io/docs/reference/using-api/declarative-validation/#catalog).
725+
726+
To enable DV Native enforcement in your API strategy, you must pass the `rest.WithDeclarativeNative()` option to the declarative validation call in your `strategy.go`.
727+
728+
```go
729+
func (myStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
730+
myObj := obj.(*myapi.MyResource)
731+
allErrs := validation.ValidateMyResource(myObj)
732+
return rest.ValidateDeclarativelyWithMigrationChecks(ctx, legacyscheme.Scheme, obj, nil, allErrs, operation.Create, rest.WithDeclarativeNative())
733+
}
734+
```
735+
736+
When writing tests for DV Native fields, you must use `.MarkDeclarativeNative()` on the expected errors for fields that are DV Native.
737+
738+
```go
739+
{
740+
name: "missing native field",
741+
obj: &MyResource{...},
742+
expectedErrs: field.ErrorList{
743+
field.Required(field.NewPath("spec", "myNativeField"), "").MarkDeclarativeNative(),
744+
},
745+
}
746+
```
747+
709748
## Edit version conversions
710749

711750
At this point you have both the versioned API changes and the internal

0 commit comments

Comments
 (0)