|
7 | 7 | - [ForbiddenMarkers](#forbiddenmarkers) - Checks that no forbidden markers are present on types/fields. |
8 | 8 | - [Integers](#integers) - Validates usage of supported integer types |
9 | 9 | - [JSONTags](#jsontags) - Ensures proper JSON tag formatting |
| 10 | +- [MarkerScope](#markerscope) - Validates that markers are applied in the correct scope |
10 | 11 | - [MaxLength](#maxlength) - Checks for maximum length constraints on strings and arrays |
11 | 12 | - [NamingConventions](#namingconventions) - Ensures field names adhere to user-defined naming conventions |
12 | 13 | - [NoBools](#nobools) - Prevents usage of boolean types |
@@ -289,6 +290,44 @@ lintersConfig: |
289 | 290 | jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # Provide a custom regex, which the json tag must match. |
290 | 291 | ``` |
291 | 292 |
|
| 293 | +## MarkerScope |
| 294 | + |
| 295 | +The `markerscope` linter validates that markers are applied in the correct scope. It ensures that markers are placed on appropriate Go language constructs (types, fields) according to their intended usage. |
| 296 | + |
| 297 | +The linter defines different scope types for markers: |
| 298 | + |
| 299 | +- **Field-only markers**: Can only be applied to struct fields (e.g., `required`, `kubebuilder:validation:Required`) |
| 300 | +- **Type-only markers**: Can only be applied to type definitions |
| 301 | +- **Type or Map/Slice fields**: Can be applied to type definitions, map fields, or slice fields (e.g., `kubebuilder:validation:MinProperties`) |
| 302 | +- **Field or Type markers**: Can be applied to either fields or type definitions |
| 303 | + |
| 304 | +### Default Scope Rules |
| 305 | + |
| 306 | +By default, the linter enforces these scope rules: |
| 307 | + |
| 308 | +- `required` and `kubebuilder:validation:Required`: Field-only |
| 309 | +- `kubebuilder:validation:MinProperties`: Type definitions, map fields, or slice fields only |
| 310 | + |
| 311 | +### Configuration |
| 312 | + |
| 313 | +```yaml |
| 314 | +lintersConfig: |
| 315 | + markerscope: |
| 316 | + policy: SuggestFix | Warn # The policy for marker scope violations. Defaults to `SuggestFix`. |
| 317 | +``` |
| 318 | + |
| 319 | +### Fixes |
| 320 | + |
| 321 | +The `markerscope` linter can automatically fix scope violations when `policy` is set to `SuggestFix`: |
| 322 | + |
| 323 | +1. **Remove incorrect markers**: Suggests removing markers that are in the wrong scope |
| 324 | +2. **Move markers to correct locations**: |
| 325 | + - Move field-only markers from types to appropriate fields |
| 326 | + - Move type-only markers from fields to their corresponding type definitions |
| 327 | +3. **Preserve marker values**: When moving markers like `kubebuilder:validation:MinProperties=1` |
| 328 | + |
| 329 | +**Note**: This linter is not enabled by default and must be explicitly enabled in the configuration. |
| 330 | + |
292 | 331 | ## MaxLength |
293 | 332 |
|
294 | 333 | The `maxlength` linter checks that string and array fields in the API are bounded by a maximum length. |
|
0 commit comments