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
+11-8Lines changed: 11 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -288,14 +288,13 @@ The `optionalorrequired` linter can automatically fix fields that are using the
288
288
It will also remove the secondary marker where both the preferred and secondary marker are present on a field.
289
289
290
290
## RequiredFields
291
-
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.
291
+
The `requiredfields` linter checks that all fields marked as required adhere to having `omitempty` or `omitzero` values in their `json` tags.
293
292
Currently `omitzero` is handled only for fields with struct type.
294
293
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.
294
+
Required fields should have omitempty tags to prevent "mess" in the encoded object.
295
+
Fields are not typically pointers.
296
+
A field doesn't need to be a pointer if its zero value is not a valid value, as this zero value could never be accepted.
297
+
However, if the zero value is valid, the field should be a pointer to differentiate between an unset state and a valid zero value.
299
298
300
299
In certain use cases, it can be desirable to not omit required fields from the serialized form of the object.
301
300
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.
@@ -315,8 +314,12 @@ lintersConfig:
315
314
316
315
### Fixes
317
316
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.
317
+
The `requiredfields` linter can automatically fix fields marked as required. It does this by checking if the field should be a pointer and if its `json` tag is set correctly with `omitempty` or `omitzero`.
318
+
319
+
If a field's zero value is valid, the linter will suggest to fix it to be a pointer type and add `omitempty` to its JSON tag.
320
+
321
+
If a field's zero value is not valid, the field doesn't need to be a pointer. However, to prevent unnecessary data from being encoded in the JSON, the linter will suggest to add `omitempty` to its JSON tag.
322
+
For a struct field with an invalid zero value, the linter will suggest to add `omitzero` to the JSON tag.
320
323
321
324
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointers.policy` to `Warn`.
Copy file name to clipboardExpand all lines: pkg/analysis/requiredfields/doc.go
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -15,12 +15,14 @@ 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 marshalled properly depending on the configured policies.
18
+
requiredfields is a linter to check that fields that are marked as required are marshalled properly.
19
+
The linter will check for fields that are marked as required using the +required marker, or the +kubebuilder:validation:Required marker.
19
20
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.
21
+
Required fields should have omitempty or omitzero tags to prevent "mess" in the encoded object.
22
+
omitzero is handled only for fields with struct type.
21
23
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.
24
+
Fields are not typically pointers.
25
+
A field doesn't need to be a pointer if its zero value is not a valid value, as this zero value could never be accepted.
26
+
However, if the zero value is valid, the field should be a pointer to differentiate between an unset state and a valid zero value.
0 commit comments