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
+25-7Lines changed: 25 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -427,25 +427,43 @@ It will also remove the secondary marker where both the preferred and secondary
427
427
428
428
## RequiredFields
429
429
430
-
The `requiredfields` linter checks that fields that are marked as required, follow the convention of not being pointers,
431
-
and not having an `omitempty` value in their `json` tag.
430
+
The `requiredfields` linter checks that all fields marked as required adhere to having `omitempty` or `omitzero` values in their `json` tags.
431
+
Currently `omitzero` is handled only for fields with struct type.
432
+
433
+
Required fields should have omitempty tags to prevent "mess" in the encoded object.
434
+
Fields are not typically pointers.
435
+
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.
436
+
However, if the zero value is valid, the field should be a pointer to differentiate between an unset state and a valid zero value.
437
+
438
+
In certain use cases, it can be desirable to not omit required fields from the serialized form of the object.
439
+
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.
432
440
433
441
### Configuration
434
442
435
443
```yaml
436
444
lintersConfig:
437
445
requiredfields:
438
-
pointerPolicy: Warn | SuggestFix # The policy for pointers in required fields. Defaults to `SuggestFix`.
446
+
pointers:
447
+
policy: SuggestFix | Warn # The policy for pointers in required fields. Defaults to `SuggestFix`.
448
+
omitempty:
449
+
policy: SuggestFix | Warn | Ignore # The policy for omitempty in required fields. Defaults to `SuggestFix`.
450
+
omitzero:
451
+
policy: SuggestFix | Warn | Forbid # The policy for omitzero in required fields. Defaults to `SuggestFix`.
439
452
```
440
453
441
454
### Fixes
442
455
443
-
The `requiredfields` linter can automatically fix fields that are marked as required, but are pointers.
456
+
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`.
457
+
458
+
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.
459
+
460
+
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.
461
+
For a struct field with an invalid zero value, the linter will suggest to add `omitzero` to the JSON tag.
444
462
445
-
It will suggest to remove the pointer from the field, and update the `json` tag to remove the `omitempty` value.
463
+
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointers.policy` to `Warn`.
446
464
447
-
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointerPolicy` to `Warn`.
448
-
The linter will then only suggest to remove the `omitempty` value from the `json` tag.
465
+
If you prefer not to suggest fixes for `omitempty` in required fields, you can change the `omitempty.policy` to `Warn` or `Ignore`.
466
+
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`.
0 commit comments