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
+19-20Lines changed: 19 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -399,29 +399,28 @@ The linter defines different scope types for markers:
399
399
400
400
- **FieldScope**: Can only be applied to struct fields (e.g., `optional`, `required`, `nullable`)
401
401
- **TypeScope**: Can only be applied to type definitions (e.g., `kubebuilder:validation:items:ExactlyOneOf`)
402
-
- **AnyScope**: Can be applied to either fields or type definitions (e.g., `kubebuilder:validation:Minimum`, `kubebuilder:validation:Pattern`)
402
+
- **Field and Type**: Markers that can be applied to both fields and type definitions (e.g., `kubebuilder:validation:Minimum`, `kubebuilder:validation:Pattern`)
403
403
404
404
### Type Constraints
405
405
406
406
The linter validates that markers are applied to compatible OpenAPI schema types:
407
407
408
-
- **Numeric markers** (`Minimum`, `Maximum`, `MultipleOf`): Only for `integer` or `number` types
408
+
- **Numeric markers** (`Minimum`, `Maximum`, `MultipleOf`): Only for `integer` types
409
409
- **String markers** (`Pattern`, `MinLength`, `MaxLength`): Only for `string` types
410
410
- **Array markers** (`MinItems`, `MaxItems`, `UniqueItems`): Only for `array` types
411
411
- **Object markers** (`MinProperties`, `MaxProperties`): Only for `object` types (struct/map)
412
412
- **Array items markers** (`items:Minimum`, `items:Pattern`, etc.): Apply constraints to array element types
For markers with `AnyScope` and type constraints, the `strictTypeConstraint` flag controls where the marker should be declared when used with named types:
423
+
For markers that can be applied to both fields and types with type constraints, the `strictTypeConstraint` flag controls where the marker should be declared when used with named types:
425
424
426
425
- When `strictTypeConstraint` is `false` (default): The marker can be declared on either the field or the type definition.
427
426
- When `strictTypeConstraint` is `true`: The marker must be declared on the type definition, not on fields using that type.
@@ -461,13 +460,13 @@ The linter includes built-in rules for all standard kubebuilder markers and k8s
@@ -476,50 +475,50 @@ The linter includes built-in rules for all standard kubebuilder markers and k8s
476
475
You can customize marker rules or add support for custom markers.
477
476
478
477
**Scope values:**
479
-
-`Field`: Marker can only be applied to struct fields
480
-
-`Type`: Marker can only be applied to type definitions
481
-
-`Any`: Marker can be applied to either fields or type definitions
478
+
479
+
The `scopes` field accepts an array of scope constraints:
480
+
-`[Field]`: Marker can only be applied to struct fields
481
+
-`[Type]`: Marker can only be applied to type definitions
482
+
-`[Field, Type]`: Marker can be applied to both fields and type definitions
482
483
483
484
**Type constraints:**
484
485
485
-
The `typeConstraint` field allows you to restrict which Go types a marker can be applied to. This ensures that markers are only used with compatible data types (e.g., numeric markers like `Minimum` are only applied to integer/number types).
486
+
The `typeConstraint` field allows you to restrict which Go types a marker can be applied to. This ensures that markers are only used with compatible data types (e.g., numeric markers like `Minimum` are only applied to integer types).
486
487
487
488
**Type constraint fields:**
488
-
-`allowedSchemaTypes`: List of allowed OpenAPI schema types (`integer`, `number`, `string`, `boolean`, `array`, `object`)
489
+
-`allowedSchemaTypes`: List of allowed OpenAPI schema types (`integer`, `string`, `boolean`, `array`, `object`)
489
490
-`elementConstraint`: Nested constraint for array element types (only valid when `allowedSchemaTypes` includes `array`)
490
-
-`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`.
491
+
-`strictTypeConstraint`: When `true`, markers that can be applied to both fields and types with type constraints applied to fields using named types must be declared on the type definition instead of the field. Defaults to `false`.
491
492
492
493
**Configuration example:**
493
494
494
495
```yaml
495
496
lintersConfig:
496
497
markerscope:
497
498
policy: Warn | SuggestFix # The policy for marker scope violations. Defaults to `Warn`.
498
-
allowDangerousTypes: false # Allow dangerous number types (float32, float64). Defaults to `false`.
499
499
500
500
# Override default rules for built-in markers
501
501
overrideMarkers:
502
502
- identifier: "optional"
503
-
scope: Field #or: Type, Any
503
+
scopes: [Field]#Can specify [Field], [Type], or [Field, Type]
504
504
505
505
# Add rules for custom markers
506
506
customMarkers:
507
507
# Custom marker with scope constraint only
508
508
- identifier: "mycompany:validation:CustomMarker"
509
-
scope: Any
509
+
scopes: [Field, Type]
510
510
511
511
# Custom marker with scope and type constraints
512
512
- identifier: "mycompany:validation:NumericLimit"
513
-
scope: Any
513
+
scopes: [Field, Type]
514
514
strictTypeConstraint: true # Require declaration on type definition for named types
515
515
typeConstraint:
516
516
allowedSchemaTypes:
517
517
- integer
518
-
- number
519
518
520
519
# Custom array items marker with element type constraint
@@ -541,7 +540,7 @@ When the `policy` is set to `SuggestFix`, the `markerscope` linter provides auto
541
540
542
541
2. **Type constraint violations**: For markers applied to incompatible types, the linter suggests removing the invalid marker.
543
542
544
-
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.
543
+
3. **Named type violations**: For markers that can be applied to both fields and types 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.
545
544
546
545
When the `policy` is set to `Warn`, violations are reported as warnings without suggesting fixes.
0 commit comments