-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: new validation rule interface (#399)
## Description New interface to help with working with rule names in a consistent way across the ecosystem. Plugins would be required to implement it for easy integration with programs like `validatorctl`. They can do that by embedding `validationrule.ManuallyNamed` or `validationrule.AutomaticallyNamed` into their rule struct and implementing the required methods. If they embed `validationrule.AutomaticallyNamed`, they don't need to implement `SetName`. Example: ```go type MyRule struct { validationrule.ManuallyNamed `json:",inline"` ... } ``` They should also assert that their rule implements the interface. Example: ```go var _ validationrule.Interface = (*MyRule)(nil) ``` For rules that are manually named, it is expected that only pointers to rules can implement the interface because of the pointer receiver of implementations of `SetName`. Example: ```go func (r *MyRule) SetName(name string) { ... } ``` --------- Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
- Loading branch information
Showing
3 changed files
with
45 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Package validationrule describes validation rules. | ||
package validationrule | ||
|
||
// Interface defines validation rule behavior. | ||
type Interface interface { | ||
// Name returns the name of the rule. | ||
Name() string | ||
// SetName sets the name of the rule if it is a rule that requires manually specifying its name. | ||
// This should be a no-op for rules that automatically generate their name. | ||
SetName(string) | ||
// RequiresName returns whether the validation rule requires its name to be manually specified. | ||
// This should return false for rules that automatically generate their name. | ||
RequiresName() bool | ||
} | ||
|
||
// ManuallyNamed can be embedded into a rule struct to indicate that the rule requires its name to | ||
// be manually specified. | ||
type ManuallyNamed struct{} | ||
|
||
// RequiresName returns true. | ||
func (ManuallyNamed) RequiresName() bool { | ||
return true | ||
} | ||
|
||
// AutomaticallyNamed can be embedded into a rule struct to indicate that the rule does not require | ||
// its name to be manually specified because it is automatically generated from other data in the | ||
// rule. | ||
type AutomaticallyNamed struct{} | ||
|
||
// RequiresName returns false. | ||
func (AutomaticallyNamed) RequiresName() bool { | ||
return false | ||
} | ||
|
||
// SetName is a no-op because the rule does not support manually specifying its name. | ||
func (AutomaticallyNamed) SetName(string) { | ||
// no-op | ||
} |