Skip to content

Commit

Permalink
Add a warning/error option to schema validation
Browse files Browse the repository at this point in the history
Add new schema options to configure the warning/error level of validation functions.
This allows the following functions to be used as a warning-level diag instead of a fatal error:
"ConflictsWith", "AtLeastOneOf", "ExactlyOneOf", "RequiredWith".

The ConditionsMode allows provider developers to toggle between "warning" and "error" level messages.
The ConditionsMessage is an optional additional message to be displayed to users at run time.
  • Loading branch information
dak1n1 committed Apr 21, 2021
1 parent 112e216 commit 57d4cc0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
31 changes: 23 additions & 8 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ 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).
// When set to WARNING, a warning message is displayed to the user.
// When set to ERROR (default), a fatal error is returned.
// Optionally, a message can be returned with the warning or error.
ConditionsMode string
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 @@ -1449,22 +1457,29 @@ func (m schemaMap) validate(
ok = raw != nil
}

var conditionsMode diag.Severity
if schema.ConditionsMode == strings.ToLower("warning") {
conditionsMode = diag.Warning
} else {
conditionsMode = diag.Error
}

err := validateExactlyOneAttribute(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: conditionsMode,
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: conditionsMode,
Summary: "AtLeastOne",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}
Expand Down Expand Up @@ -1494,9 +1509,9 @@ func (m schemaMap) validate(
err = validateRequiredWithAttribute(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: conditionsMode,
Summary: "RequiredWith",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}
Expand All @@ -1521,9 +1536,9 @@ func (m schemaMap) validate(
err = validateConflictingAttributes(k, schema, c)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Severity: conditionsMode,
Summary: "ConflictsWith",
Detail: err.Error(),
Detail: fmt.Sprintf("%s %s", schema.ConditionsMessage, err.Error()),
AttributePath: path,
})
}
Expand Down
38 changes: 32 additions & 6 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5991,7 +5991,33 @@ func TestSchemaMap_Validate(t *testing.T) {

Err: true,
Errors: []error{
fmt.Errorf(`Error: ConflictsWith: "blacklist": conflicts with whitelist`),
fmt.Errorf(`Error: ConflictsWith: "blacklist": conflicts with whitelist`),
},
},

"Conflicting attributes generate warning": {
Schema: map[string]*Schema{
"whitelist": {
Type: TypeString,
Optional: true,
},
"blacklist": {
Type: TypeString,
Optional: true,
ConflictsWith: []string{"whitelist"},
ConditionsMode: "warning",
ConditionsMessage: "This functionality will be removed in a later release.",
},
},

Config: map[string]interface{}{
"whitelist": "white-val",
"blacklist": "black-val",
},

Err: false,
Warnings: []string{
`Warning: ConflictsWith: This functionality will be removed in a later release. "blacklist": conflicts with whitelist`,
},
},

Expand Down Expand Up @@ -6087,8 +6113,8 @@ func TestSchemaMap_Validate(t *testing.T) {

Err: true,
Errors: []error{
fmt.Errorf(`Error: ConflictsWith: "blacklist": conflicts with greenlist`),
fmt.Errorf(`Error: ConflictsWith: "greenlist": conflicts with blacklist`),
fmt.Errorf(`Error: ConflictsWith: "blacklist": conflicts with greenlist`),
fmt.Errorf(`Error: ConflictsWith: "greenlist": conflicts with blacklist`),
},
},

Expand Down Expand Up @@ -6132,7 +6158,7 @@ func TestSchemaMap_Validate(t *testing.T) {

Err: true,
Errors: []error{
fmt.Errorf(`Error: ConflictsWith: "optional_att": conflicts with required_att`),
fmt.Errorf(`Error: ConflictsWith: "optional_att": conflicts with required_att`),
},
},

Expand All @@ -6159,8 +6185,8 @@ func TestSchemaMap_Validate(t *testing.T) {

Err: true,
Errors: []error{
fmt.Errorf(`Error: ConflictsWith: "bar_att": conflicts with foo_att`),
fmt.Errorf(`Error: ConflictsWith: "foo_att": conflicts with bar_att`),
fmt.Errorf(`Error: ConflictsWith: "bar_att": conflicts with foo_att`),
fmt.Errorf(`Error: ConflictsWith: "foo_att": conflicts with bar_att`),
},
},

Expand Down

0 comments on commit 57d4cc0

Please sign in to comment.