Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: new validation rule interface #399

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hauler-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ metadata:
spec:
files:
- name: validatorctl
path: https://github.com/validator-labs/validatorctl/releases/download/v0.1.1/validator-linux-ARCH
path: https://github.com/validator-labs/validatorctl/releases/download/v0.1.2/validator-linux-ARCH
12 changes: 6 additions & 6 deletions pkg/validationresult/validation_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
Patch(ctx context.Context, obj client.Object, opts ...patch.Option) error
}

// ValidationRule is an interface for validation rules.
type ValidationRule interface {
// Validator is an interface for building ValidationResults.
type Validator interface {
client.Object
GetKind() string
PluginCode() string
ResultCount() int
}

// Build creates a new ValidationResult for a specific ValidationRule.
func Build(r ValidationRule) *v1alpha1.ValidationResult {
// Build creates a new ValidationResult for a specific Validator.
func Build(r Validator) *v1alpha1.ValidationResult {

Check warning on line 38 in pkg/validationresult/validation_result.go

View check run for this annotation

Codecov / codecov/patch

pkg/validationresult/validation_result.go#L38

Added line #L38 was not covered by tests
return &v1alpha1.ValidationResult{
TypeMeta: metav1.TypeMeta{
APIVersion: v1alpha1.APIVersion,
Expand All @@ -61,8 +61,8 @@
}
}

// Name returns the name of a ValidationResult for a specific ValidationRule.
func Name(r ValidationRule) string {
// Name returns the name of a ValidationResult for a specific Validator.
func Name(r Validator) string {

Check warning on line 65 in pkg/validationresult/validation_result.go

View check run for this annotation

Codecov / codecov/patch

pkg/validationresult/validation_result.go#L65

Added line #L65 was not covered by tests
name := fmt.Sprintf("validator-plugin-%s-%s", r.PluginCode(), r.GetName())
return util.Sanitize(name)
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/validationrule/validation_rule.go
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

Check warning on line 22 in pkg/validationrule/validation_rule.go

View check run for this annotation

Codecov / codecov/patch

pkg/validationrule/validation_rule.go#L21-L22

Added lines #L21 - L22 were not covered by tests
}

// 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

Check warning on line 32 in pkg/validationrule/validation_rule.go

View check run for this annotation

Codecov / codecov/patch

pkg/validationrule/validation_rule.go#L31-L32

Added lines #L31 - L32 were not covered by tests
}

// SetName is a no-op because the rule does not support manually specifying its name.
func (AutomaticallyNamed) SetName(string) {

Check warning on line 36 in pkg/validationrule/validation_rule.go

View check run for this annotation

Codecov / codecov/patch

pkg/validationrule/validation_rule.go#L36

Added line #L36 was not covered by tests
// no-op
}