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: docs/linters.md
+21-7Lines changed: 21 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -289,25 +289,39 @@ It will also remove the secondary marker where both the preferred and secondary
289
289
290
290
## RequiredFields
291
291
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.
294
302
295
303
### Configuration
296
304
297
305
```yaml
298
306
lintersConfig:
299
307
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`.
301
314
```
302
315
303
316
### Fixes
304
317
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.
306
320
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`.
308
322
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`.
Copy file name to clipboardExpand all lines: pkg/analysis/requiredfields/doc.go
+5-6Lines changed: 5 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -15,13 +15,12 @@ limitations under the License.
15
15
*/
16
16
17
17
/*
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.
20
19
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.
23
21
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.
0 commit comments