Skip to content

Commit 13d00d0

Browse files
committed
fix documentation about requiredfields
1 parent 0660253 commit 13d00d0

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

docs/linters.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,25 +289,39 @@ It will also remove the secondary marker where both the preferred and secondary
289289

290290
## RequiredFields
291291

292-
The `requiredfields` linter checks that fields that are marked as required, follow the convention of not being pointers,
293-
and not having an `omitempty` value in their `json` tag.
292+
The `requiredfields` linter checks that all fields marked as required adhere to having `omitempty` tags and being pointers where the zero value is an acceptable value.
293+
Currently `omitzero` is handled only for fields with struct type.
294+
295+
If you prefer to avoid pointers where possible, the linter can be configured to determine, based on the validation constraints for the field, whether the field should be a pointer or not.
296+
For example:
297+
- a required string with a non-zero minimum length does not need to be a pointer, as the zero value is not valid, and it is safe for the Go marshaller to omit the empty value.
298+
- a required struct having omitzero json tag with validation constraints that make the zero value invalid does not need to be a pointer, as the zero value is not valid, and it is safe for the Go marshaller to omit the empty value.
299+
300+
In certain use cases, it can be desirable to not omit required fields from the serialized form of the object.
301+
In this case, the `omitempty` policy can be set to `Ignore`, and the linter will ensure that the zero value of the object is an acceptable value for the field.
294302

295303
### Configuration
296304

297305
```yaml
298306
lintersConfig:
299307
requiredfields:
300-
pointerPolicy: Warn | SuggestFix # The policy for pointers in required fields. Defaults to `SuggestFix`.
308+
pointers:
309+
policy: SuggestFix | Warn # The policy for pointers in required fields. Defaults to `SuggestFix`.
310+
omitempty:
311+
policy: SuggestFix | Warn | Ignore # The policy for omitempty in required fields. Defaults to `SuggestFix`.
312+
omitzero:
313+
policy: SuggestFix | Warn | Forbid # The policy for omitzero in required fields. Defaults to `SuggestFix`.
301314
```
302315
303316
### Fixes
304317
305-
The `requiredfields` linter can automatically fix fields that are marked as required, but are pointers.
318+
The `requiredfields` linter can automatically fix fields that are marked as required, that are either not pointers when they should be or do not have the `omitempty` or `omitzero` value in their `json` tag.
319+
It will suggest to add the pointer to the field, and update the `json` tag to include the `omitempty` value or, for struct fields specifically, it will suggest to remove the pointer to the field, and update the `json` tag to include the `omitzero` value.
306320

307-
It will suggest to remove the pointer from the field, and update the `json` tag to remove the `omitempty` value.
321+
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointers.policy` to `Warn`.
308322

309-
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointerPolicy` to `Warn`.
310-
The linter will then only suggest to remove the `omitempty` value from the `json` tag.
323+
If you prefer not to suggest fixes for `omitempty` in required fields, you can change the `omitempty.policy` to `Warn` or `Ignore`.
324+
If you prefer not to suggest fixes for `omitzero` in required fields, you can change the `omitzero.policy` to `Warn` and also not to consider `omitzero` policy at all, it can be set to `Forbid`.
311325

312326
## SSATags
313327

pkg/analysis/requiredfields/doc.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ limitations under the License.
1515
*/
1616

1717
/*
18-
requiredFields is a linter to check that fields that are marked as required are not pointers, and do not have the omitempty tag.
19-
The linter will check for fields that are marked as required using the +required marker, or the +kubebuilder:validation:Required marker.
18+
requiredfields is a linter to check that fields that are marked as required are marshalled properly depending on the configured policies.
2019
21-
The linter will suggest to remove the omitempty tag from fields that are marked as required, but have the omitempty tag.
22-
The linter will suggest to remove the pointer type from fields that are marked as required.
20+
By default, all required fields should have omitempty tags and be pointers where the zero value is an acceptable value. The exception to this would be where the zero value for a field is not a valid value, in which case the field does not need to be a pointer.
2321
24-
If you have a large, existing codebase, you may not want to automatically fix all of the pointer issues.
25-
In this case, you can configure the linter not to suggest fixing the pointer issues by setting the `pointerPolicy` option to `Warn`.
22+
However, where the zero value for a field is a valid value (e.g. the empty string, or 0), the field should be a pointer to distinguish between unset and zero value states.
23+
24+
Required fields should always have omitempty tags to prevent "mess" in the encoded object, regardless of whether they are pointers.
2625
*/
2726
package requiredfields

0 commit comments

Comments
 (0)