Skip to content

Commit db91ce1

Browse files
committed
Add discriminated unions structure linter
Signed-off-by: Matteo Fari <matteofari06@gmail.com>
1 parent 39e3d06 commit db91ce1

11 files changed

Lines changed: 780 additions & 0 deletions

File tree

docs/linters.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
| [DefaultOrRequired](#defaultorrequired) | Ensures fields marked as required do not have default values | True | Native, CRD |
1010
| [Defaults](#defaults) | Checks that fields with default markers are configured correctly | True | Native, CRD |
1111
| [DependentTags](#dependenttags) | Enforces dependencies between markers | False | Native, CRD |
12+
| [DiscriminatedUnions](#discriminatedunions) | Validates discriminated union marker structure | False | Native, CRD |
1213
| [DuplicateMarkers](#duplicatemarkers) | Checks for exact duplicates of markers | True | Native, CRD |
1314
| [ForbiddenMarkers](#forbiddenmarkers) | Checks that no forbidden markers are present on types/fields. | False | Native, CRD |
1415
| [Integers](#integers) | Validates usage of supported integer types | True | Native, CRD |
@@ -138,6 +139,33 @@ This linter only checks for the presence or absence of markers; it does not insp
138139
- **Fixes:** This linter does not provide automatic fixes. It only reports violations.
139140
- **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.
140141

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+
- Optional member fields (including support for `+unionMember,optional`).
154+
- Optional forbidding of non-member fields.
155+
156+
### Configuration
157+
158+
```yaml
159+
lintersConfig:
160+
discriminatedunions:
161+
nonMemberFields: Forbid | Allow # Defaults to `Forbid`.
162+
```
163+
164+
### Behavior
165+
166+
- **Default:** Disabled by default; enable explicitly.
167+
- **Scope:** Structure-only validation in this linter implementation.
168+
141169
## CommentStart
142170

143171
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

Comments
 (0)