Skip to content

Commit 52306d5

Browse files
committed
Add documentation for requiredfields linter
1 parent 2fd6632 commit 52306d5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,28 @@ The `optionalorrequired` linter can automatically fix fields that are using the
179179

180180
It will also remove the secondary marker where both the preferred and secondary marker are present on a field.
181181

182+
## RequiredFields
183+
184+
The `requiredfields` linter checks that fields that are marked as required, follow the convention of not being pointers,
185+
and not having an `omitempty` value in their `json` tag.
186+
187+
### Configuration
188+
189+
```yaml
190+
lintersConfig:
191+
requiredFields:
192+
pointerPolicy: Warn | SuggestFix # The policy for pointers in required fields. Defaults to `SuggestFix`.
193+
```
194+
195+
### Fixes (via standalone binary only)
196+
197+
The `requiredfields` linter can automatically fix fields that are marked as required, but are pointers.
198+
199+
It will suggest to remove the pointer from the field, and update the `json` tag to remove the `omitempty` value.
200+
201+
If you prefer not to suggest fixes for pointers in required fields, you can change the `pointerPolicy` to `Warn`.
202+
The linter will then only suggest to remove the `omitempty` value from the `json` tag.
203+
182204
# Contributing
183205

184206
New linters can be added by following the [New Linter][new-linter] guide.

pkg/analysis/requiredfields/doc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
requiredFields is a linter to check that fields that are marked as required are not pointers, and do not have the omitempty tag.
3+
The linter will check for fields that are marked as required using the +required marker, or the +kubebuilder:validation:Required marker.
4+
5+
The linter will suggest to remove the omitempty tag from fields that are marked as required, but have the omitempty tag.
6+
The linter will suggest to remove the pointer type from fields that are marked as required.
7+
8+
If you have a large, existing codebase, you may not want to automatically fix all of the pointer issues.
9+
In this case, you can configure the linter not to suggest fixing the pointer issues by setting the `pointerPolicy` option to `Warn`.
10+
*/
11+
package requiredfields

0 commit comments

Comments
 (0)