Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Small changes to validation error messages for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sandusky committed Feb 21, 2018
1 parent 70615bf commit 4c95a3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
12 changes: 7 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 30 additions & 13 deletions rules/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (v *validator) validateMappingRules(mrv map[string]*rules.MappingRuleView)
for _, view := range mrv {
// Validate that no rules with the same name exist.
if _, exists := namesSeen[view.Name]; exists {
return errors.NewRuleConflictError(fmt.Sprintf("mapping rule %s already exists", view.Name))
return errors.NewRuleConflictError(fmt.Sprintf("mapping rule '%s' already exists", view.Name))
}
namesSeen[view.Name] = struct{}{}

Expand All @@ -103,7 +103,11 @@ func (v *validator) validateMappingRules(mrv map[string]*rules.MappingRuleView)
return err
}
if len(types) == 0 {
return fmt.Errorf("mapping rule %s does not match any allowed metric types, filter=%s", view.Name, view.Filter)
return fmt.Errorf(
"mapping rule '%s' does not match any allowed metric types, filter=%s",
view.Name,
view.Filter,
)
}

// Validate that the policies are valid.
Expand All @@ -122,7 +126,7 @@ func (v *validator) validateRollupRules(rrv map[string]*rules.RollupRuleView) er
for _, view := range rrv {
// Validate that no rules with the same name exist.
if _, exists := namesSeen[view.Name]; exists {
return errors.NewRuleConflictError(fmt.Sprintf("rollup rule %s already exists", view.Name))
return errors.NewRuleConflictError(fmt.Sprintf("rollup rule '%s' already exists", view.Name))
}
namesSeen[view.Name] = struct{}{}

Expand All @@ -138,7 +142,11 @@ func (v *validator) validateRollupRules(rrv map[string]*rules.RollupRuleView) er
return err
}
if len(types) == 0 {
return fmt.Errorf("rollup rule %s does not match any allowed metric types, filter=%s", view.Name, view.Filter)
return fmt.Errorf(
"rollup rule '%s' does not match any allowed metric types, filter=%s",
view.Name,
view.Filter,
)
}

for _, target := range view.Targets {
Expand All @@ -161,7 +169,13 @@ func (v *validator) validateRollupRules(rrv map[string]*rules.RollupRuleView) er
current := target.RollupTarget()
for _, seenTarget := range targetsSeen {
if current.SameTransform(seenTarget) {
return errors.NewRuleConflictError(fmt.Sprintf("rollup target with name %s and tags %s already exists", current.Name, current.Tags))
return errors.NewRuleConflictError(
fmt.Sprintf(
"rollup target with name '%s' and tags '%s' already exists",
current.Name,
current.Tags,
),
)
}
}
targetsSeen = append(targetsSeen, current)
Expand All @@ -174,12 +188,15 @@ func (v *validator) validateRollupRules(rrv map[string]*rules.RollupRuleView) er
func (v *validator) validateFilter(ruleName string, f string) (filters.TagFilterValueMap, error) {
filterValues, err := filters.ValidateTagsFilter(f)
if err != nil {
return nil, fmt.Errorf("rule %s has invalid rule filter %s: %v", ruleName, f, err)
return nil, fmt.Errorf("rule '%s' has invalid rule filter '%s': %v", ruleName, f, err)
}
for tag := range filterValues {
// Validating the filter tag name does not contain invalid chars.
if err := v.opts.CheckInvalidCharactersForTagName(tag); err != nil {
return nil, fmt.Errorf("rule %s has invalid rule filter %s: tag name %s contains invalid character, err: %v", ruleName, f, tag, err)
return nil, fmt.Errorf(
"rule '%s' has invalid rule filter '%s': tag name '%s' contains invalid character, err: %v",
ruleName, f, tag, err,
)
}
}
return filterValues, nil
Expand All @@ -195,7 +212,7 @@ func (v *validator) validatePolicies(ruleName string, policies []policy.Policy,
seen := make(map[policy.Policy]struct{}, len(policies))
for _, p := range policies {
if _, exists := seen[p]; exists {
return fmt.Errorf("rule %s has duplicate policy %s, provided policies are %v", ruleName, p.String(), policies)
return fmt.Errorf("rule '%s' has duplicate policy '%s', provided policies are %v", ruleName, p.String(), policies)
}
seen[p] = struct{}{}
}
Expand All @@ -215,15 +232,15 @@ func (v *validator) validateRollupTags(ruleName string, tags []string) error {
// Validating that all tag names have valid characters.
for _, tag := range tags {
if err := v.opts.CheckInvalidCharactersForTagName(tag); err != nil {
return fmt.Errorf("rollup rule %s has invalid rollup tag %s: %v", ruleName, tag, err)
return fmt.Errorf("rollup rule '%s' has invalid rollup tag '%s': %v", ruleName, tag, err)
}
}

// Validating that there are no duplicate rollup tags.
rollupTags := make(map[string]struct{}, len(tags))
for _, tag := range tags {
if _, exists := rollupTags[tag]; exists {
return fmt.Errorf("rollup rule %s has duplicate rollup tag: %s, provided rollup tags are %v", ruleName, tag, tags)
return fmt.Errorf("rollup rule '%s' has duplicate rollup tag: '%s', provided rollup tags are %v", ruleName, tag, tags)
}
rollupTags[tag] = struct{}{}
}
Expand All @@ -235,7 +252,7 @@ func (v *validator) validateRollupTags(ruleName string, tags []string) error {
}
for _, requiredTag := range requiredTags {
if _, exists := rollupTags[requiredTag]; !exists {
return fmt.Errorf("rollup rule %s does not have required rollup tag: %s, provided rollup tags are %v", ruleName, requiredTag, tags)
return fmt.Errorf("rollup rule '%s' does not have required rollup tag: '%s', provided rollup tags are %v", ruleName, requiredTag, tags)
}
}

Expand All @@ -245,12 +262,12 @@ func (v *validator) validateRollupTags(ruleName string, tags []string) error {
func (v *validator) validateRollupMetricName(ruleName, metricName string) error {
// Validate that rollup metric name is not empty.
if metricName == "" {
return fmt.Errorf("rollup rule %s has an empty rollup metric name", ruleName)
return fmt.Errorf("rollup rule '%s' has an empty rollup metric name", ruleName)
}

// Validate that rollup metric name has valid characters.
if err := v.opts.CheckInvalidCharactersForMetricName(metricName); err != nil {
return fmt.Errorf("rollup rule %s has an invalid rollup metric name %s: %v", ruleName, metricName, err)
return fmt.Errorf("rollup rule '%s' has an invalid rollup metric name '%s': %v", ruleName, metricName, err)
}

return nil
Expand Down

0 comments on commit 4c95a3f

Please sign in to comment.