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
+41-1Lines changed: 41 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -325,6 +325,35 @@ OpenAPI schema types map to Go types as follows:
325
325
- `array`: []T, [N]T (slices and arrays)
326
326
- `object`: struct, map[K]V
327
327
328
+
#### Strict Type Constraints
329
+
330
+
For markers with `AnyScope` and type constraints, the `strictTypeConstraint` flag controls where the marker should be declared when used with named types:
331
+
332
+
- When `strictTypeConstraint` is `false` (default): The marker can be declared on either the field or the type definition.
333
+
- When `strictTypeConstraint` is `true`: The marker must be declared on the type definition, not on fields using that type.
334
+
335
+
Example with `strictTypeConstraint: true`:
336
+
337
+
```go
338
+
// ✅ Valid: marker on type definition
339
+
// +kubebuilder:validation:Minimum=0
340
+
type Port int32
341
+
342
+
type Service struct {
343
+
Port Port `json:"port"`
344
+
}
345
+
346
+
// ❌ Invalid: marker on field using named type
347
+
type Port int32
348
+
349
+
type Service struct {
350
+
// +kubebuilder:validation:Minimum=0 // Error: should be on Port type definition
351
+
Port Port `json:"port"`
352
+
}
353
+
```
354
+
355
+
Most built-in kubebuilder validation markers use `strictTypeConstraint: true` to encourage consistent marker placement on type definitions.
356
+
328
357
### Default Marker Rules
329
358
330
359
The linter includes built-in rules for all standard kubebuilder markers and k8s declarative validation markers. Examples:
@@ -356,6 +385,7 @@ You can customize marker rules or add support for custom markers:
356
385
lintersConfig:
357
386
markerscope:
358
387
policy: Warn | SuggestFix # The policy for marker scope violations. Defaults to `Warn`.
388
+
allowDangerousTypes: false # Allow dangerous number types (float32, float64). Defaults to `false`.
359
389
markerRules:
360
390
# Override default rule for a built-in marker
361
391
"optional":
@@ -368,6 +398,7 @@ lintersConfig:
368
398
# Add a custom marker with scope and type constraints
369
399
"mycompany:validation:NumericLimit":
370
400
scope: any
401
+
strictTypeConstraint: true # Require declaration on type definition for named types
371
402
typeConstraint:
372
403
allowedSchemaTypes:
373
404
- integer
@@ -392,13 +423,22 @@ lintersConfig:
392
423
**Type constraint fields:**
393
424
- `allowedSchemaTypes`: List of allowed OpenAPI schema types (`integer`, `number`, `string`, `boolean`, `array`, `object`)
394
425
- `elementConstraint`: Nested constraint for array element types (only valid when `allowedSchemaTypes` includes `array`)
426
+
- `strictTypeConstraint`: When `true`, markers with `AnyScope` and type constraints applied to fields using named types must be declared on the type definition instead of the field. Defaults to `false`.
395
427
396
428
If a marker is not in `markerRules` and not in the default rules, no validation is performed for that marker.
397
429
If a marker is in both `markerRules` and the default rules, your configuration takes precedence.
398
430
399
431
### Fixes
400
432
401
-
The `markerscope` linter does not currently provide automatic fixes. It reports violations as warnings or errors based on the configured policy.
433
+
When the `policy` is set to `SuggestFix`, the `markerscope` linter provides automatic fix suggestions for marker violations:
434
+
435
+
1. **Scope violations**: For markers applied to the wrong scope (field vs type), the linter suggests moving the marker to the correct location.
436
+
437
+
2. **Type constraint violations**: For markers applied to incompatible types, the linter suggests removing the invalid marker.
438
+
439
+
3. **Named type violations**: For AnyScope markers with type constraints applied to fields using named types, the linter suggests moving the marker to the type definition if the underlying type is compatible with the marker's type constraints.
440
+
441
+
When the `policy` is set to `Warn`, violations are reported as warnings without suggesting fixes.
402
442
403
443
**Note**: This linter is not enabled by default and must be explicitly enabled in the configuration.
0 commit comments