You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contributors/devel/sig-architecture/api_changes.md
+44Lines changed: 44 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -706,6 +706,50 @@ Users will need to write go unit tests similar to what is done for hand-written
706
706
707
707
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`.
708
708
709
+
#### When to Use Declarative Validation vs Hand-Written Validation
710
+
711
+
**Use Declarative Validation when:**
712
+
- The validation rule has an existing stable declarative validation tag which can be used in a straightforward way. See the catalog of stable tags [here](https://kubernetes.io/docs/reference/using-api/declarative-validation/).
713
+
- You are working on a new API or adding new fields to an existing API that already uses declarative validation.
714
+
- The validation rule is simple and linear (e.g., min/max values, string length, format checks).
715
+
- The validation can be expressed as a property of the data schema.
716
+
- The validation pattern is reusable across multiple APIs.
717
+
718
+
**Continue Using Hand-Written Validation when:**
719
+
- The validation requires complex logic or conditional branching based on multiple fields.
720
+
- The validation needs to consider external state beyond the object itself.
721
+
- Custom, detailed error messages are required for different failure conditions.
722
+
- The validation logic is unique to a specific API and unlikely to be reused.
723
+
724
+
725
+
**Guidelines for Creating New Declarative Validation Tags:**
726
+
727
+
Before creating a new declarative validation tag, consider these principles:
728
+
729
+
1) Any tag added should be as simple and intuitive as possible to read.
730
+
2) Any tag added should plausibly be applicable to many APIs.
731
+
3) Any tag added should represent our current understanding of the best way to express an idea.
732
+
4) Any tag we add should be something we are OK with people using in new APIs. (We may eventually add tags that capture legacy semantics which break this rule).
733
+
5) With rare and clear exceptions, tags should not be interdependent.
734
+
6) Tags should usually represent properties of the data (schema) and the API operation (e.g. update), rather than the act of validation.
735
+
7) Tags are written from the POV of an API client.
736
+
8) Tags should be as orthogonal as possible.
737
+
9) Tags should be linear and (with rare exceptions) not represent complex logic.
738
+
10) Tags should not need to consider anything other than the object(s) in question and configured options (e.g. feature gates).
739
+
740
+
A new tag is worth creating when:
741
+
- The validation pattern appears in multiple places.
742
+
- The tag name and usage would be self-explanatory.
743
+
- The validation can be expressed with simple parameters.
744
+
- It would reduce code duplication meaningfully.
745
+
746
+
Avoid creating tags for:
747
+
- Complex regex patterns (prefer named formats like `+k8s:format=isDNS...`).
0 commit comments