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

Add a warning/error option to schema validation #745

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 25 additions & 8 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ type Schema struct {
AtLeastOneOf []string
RequiredWith []string

// ConditionsMode specifies the behavior of the SDK when one of the
// above conditions are met (ConflictsWith, ExactlyOneOf, etc).
// The default value is SchemaConditionsModeError, which returns an error.
// Set ConditionsMode to SchemaConditionsModeWarning to return a warning instead.
//
// ConditionsModeMessage can optionally be used to display a message
// to the user when one of the above conditions are met.
ConditionsMode SchemaConditionsMode
ConditionsMessage string

// When Deprecated is set, this attribute is deprecated.
//
// A deprecated field still works, but will probably stop working in near
Expand Down Expand Up @@ -255,6 +265,13 @@ const (
SchemaConfigModeBlock
)

type SchemaConditionsMode uint8

const (
SchemaConditionsModeError SchemaConditionsMode = 0
SchemaConditionsModeWarning SchemaConditionsMode = 1
)

// SchemaDiffSuppressFunc is a function which can be used to determine
// whether a detected diff on a schema element is "valid" or not, and
// suppress it from the plan if necessary.
Expand Down Expand Up @@ -1452,19 +1469,19 @@ func (m schemaMap) validate(
err := validateExactlyOneAttribute(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: diag.Severity(schema.ConditionsMode),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to nitpick, but I'd feel much better about explicitly converting these rather than type casting, because if either value ever changes, I think the results would be surprising.

Maybe we could have a SchemaConditionsMode.AsDiagSeverity method that returned the diag.Severity for that mode? The method could be as simple as:

func (s SchemaConditionsMode) AsDiagSeverity() diag.Severity {
  switch s {
    case SchemaConditionsModeWarning:
      return diag.Warning
    default:
      return diag.Error
  }
}

for example.

Summary: "ExactlyOne",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}

err = validateAtLeastOneAttribute(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: diag.Severity(schema.ConditionsMode),
Summary: "AtLeastOne",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}
Expand Down Expand Up @@ -1494,9 +1511,9 @@ func (m schemaMap) validate(
err = validateRequiredWithAttribute(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: diag.Severity(schema.ConditionsMode),
Summary: "RequiredWith",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}
Expand All @@ -1521,9 +1538,9 @@ func (m schemaMap) validate(
err = validateConflictingAttributes(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: diag.Severity(schema.ConditionsMode),
Summary: "ConflictsWith",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}
Expand Down