|
9 | 9 | | [DefaultOrRequired](#defaultorrequired) | Ensures fields marked as required do not have default values | True | Native, CRD | |
10 | 10 | | [Defaults](#defaults) | Checks that fields with default markers are configured correctly | True | Native, CRD | |
11 | 11 | | [DependentTags](#dependenttags) | Enforces dependencies between markers | False | Native, CRD | |
| 12 | +| [DiscriminatedUnions](#discriminatedunions) | Validates discriminated union marker structure | False | Native, CRD | |
12 | 13 | | [DuplicateMarkers](#duplicatemarkers) | Checks for exact duplicates of markers | True | Native, CRD | |
13 | 14 | | [ForbiddenMarkers](#forbiddenmarkers) | Checks that no forbidden markers are present on types/fields. | False | Native, CRD | |
14 | 15 | | [Integers](#integers) | Validates usage of supported integer types | True | Native, CRD | |
@@ -138,6 +139,38 @@ This linter only checks for the presence or absence of markers; it does not insp |
138 | 139 | - **Fixes:** This linter does not provide automatic fixes. It only reports violations. |
139 | 140 | - **Same/Different Values:** Whether you want the same or different values between dependent markers is outside the scope of this linter. You would need other validation mechanisms (e.g., CEL validation) to enforce value-based dependencies. |
140 | 141 |
|
| 142 | +## DiscriminatedUnions |
| 143 | + |
| 144 | +The `discriminatedunions` linter validates discriminated union definitions across legacy markers (`+union`, `+unionDiscriminator`, `+unionMember`) and declarative markers (`+k8s:unionDiscriminator`, `+k8s:unionMember`). |
| 145 | +Union detection is triggered when a struct has either: |
| 146 | +- A type-level `+union` marker. |
| 147 | +- One or more union field markers (`+unionDiscriminator`/`+unionMember` or `+k8s:unionDiscriminator`/`+k8s:unionMember`). |
| 148 | + |
| 149 | +The linter enforces: |
| 150 | + |
| 151 | +- Exactly one discriminator field. |
| 152 | +- A required discriminator field. |
| 153 | +- Member fields marked optional. |
| 154 | +- Optional forbidding of non-member fields. |
| 155 | + |
| 156 | +The legacy `+unionMember,optional` marker is union membership metadata. It does not replace field optionality markers such as `+optional` or `+k8s:optional`. |
| 157 | +Diagnostics for missing optional markers recommend the configured `preferredOptionalMarker`. |
| 158 | +Declarative union markers do not have a type-level `+k8s:union` marker; declarative unions are detected from `+k8s:unionDiscriminator` and `+k8s:unionMember` field markers. |
| 159 | + |
| 160 | +### Configuration |
| 161 | + |
| 162 | +```yaml |
| 163 | +lintersConfig: |
| 164 | + discriminatedunions: |
| 165 | + nonMemberFields: Forbid | Allow # Defaults to `Forbid`. |
| 166 | + preferredOptionalMarker: optional # optional | kubebuilder:validation:Optional | k8s:optional |
| 167 | +``` |
| 168 | +
|
| 169 | +### Behavior |
| 170 | +
|
| 171 | +- **Default:** Disabled by default; enable explicitly. |
| 172 | +- **Scope:** Structure-only validation in this linter implementation. |
| 173 | +
|
141 | 174 | ## CommentStart |
142 | 175 |
|
143 | 176 | The `commentstart` linter checks that all comments in the API types start with the serialized form of the type they are commenting on. |
|
0 commit comments