|
17 | 17 | - [NoDurations](#nodurations) - Prevents usage of duration types |
18 | 18 | - [NoFloats](#nofloats) - Prevents usage of floating-point types |
19 | 19 | - [Nomaps](#nomaps) - Restricts usage of map types |
| 20 | +- [NonPointerStructs](#nonpointerstructs) - Ensures non-pointer structs are marked correctly with required/optional markers |
20 | 21 | - [NoNullable](#nonullable) - Prevents usage of the nullable marker |
21 | 22 | - [Nophase](#nophase) - Prevents usage of 'Phase' fields |
22 | 23 | - [Notimestamp](#notimestamp) - Prevents usage of 'TimeStamp' fields |
@@ -533,6 +534,39 @@ lintersConfig: |
533 | 534 | policy: Enforce | AllowStringToStringMaps | Ignore # Determines how the linter should handle maps of simple types. Defaults to AllowStringToStringMaps. |
534 | 535 | ``` |
535 | 536 |
|
| 537 | +## NonPointerStructs |
| 538 | + |
| 539 | +The `nonpointerstructs` linter checks that non-pointer structs that contain required fields are marked as required. |
| 540 | +Non-pointer structs that contain no required fields are marked as optional. |
| 541 | + |
| 542 | +This linter is important for types validated in Go as there is no way to validate the optionality of the fields at runtime, |
| 543 | +aside from checking the fields within them. |
| 544 | + |
| 545 | +This linter is NOT intended to be used to check for CRD types. |
| 546 | +The advice of this linter may be applied to CRD types, but it is not necessary for CRD types due to optionality being validated by openapi and no native Go code. |
| 547 | +For CRD types, the optionalfields and requiredfields linters should be used instead. |
| 548 | + |
| 549 | +If a struct is marked required, this can only be validated by having a required field within it. |
| 550 | +If there are no required fields, the struct is implicitly optional and must be marked as so. |
| 551 | + |
| 552 | +To have an optional struct field that includes required fields, the struct must be a pointer. |
| 553 | +To have a required struct field that includes no required fields, the struct must be a pointer. |
| 554 | + |
| 555 | +### Configuration |
| 556 | + |
| 557 | +```yaml |
| 558 | +lintersConfig: |
| 559 | + nonpointerstructs: |
| 560 | + preferredRequiredMarker: required | kubebuilder:validation:Required | k8s:required # The preferred required marker to use for required fields when providing fixes. Defaults to `required`. |
| 561 | + preferredOptionalMarker: optional | kubebuilder:validation:Optional | k8s:optional # The preferred optional marker to use for optional fields when providing fixes. Defaults to `optional`. |
| 562 | +``` |
| 563 | +
|
| 564 | +### Fixes |
| 565 | +
|
| 566 | +The `nonpointerstructs` linter can automatically fix non-pointer struct fields that are not marked as required or optional. |
| 567 | + |
| 568 | +It will suggest to mark the field as required or optional, depending on the fields within the non-pointer struct. |
| 569 | + |
536 | 570 | ## NoNullable |
537 | 571 |
|
538 | 572 | The `nonullable` linter ensures that types and fields do not have the `nullable` marker. |
|
0 commit comments