forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoder: validate required attributes more selectively (hashicorp#1485)
* deps: pin hcl-lang to 8254793 * decoder: validate required attributes more selectively * deps: pin hcl-lang to c89603e93360
- Loading branch information
1 parent
b100eaa
commit 9ae6980
Showing
4 changed files
with
75 additions
and
4 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
70 changes: 70 additions & 0 deletions
70
internal/decoder/validations/missing_required_attribute.go
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,70 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl-lang/schemacontext" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
type MissingRequiredAttribute struct{} | ||
|
||
func (mra MissingRequiredAttribute) Visit(ctx context.Context, node hclsyntax.Node, nodeSchema schema.Schema) (context.Context, hcl.Diagnostics) { | ||
var diags hcl.Diagnostics | ||
if HasUnknownRequiredAttributes(ctx) { | ||
return ctx, diags | ||
} | ||
|
||
switch nodeType := node.(type) { | ||
case *hclsyntax.Block: | ||
// Providers are excluded from the validation for the time being | ||
// due to complexity around required attributes with dynamic defaults | ||
// See https://github.com/hashicorp/vscode-terraform/issues/1616 | ||
nestingLvl, nestingOk := schemacontext.BlockNestingLevel(ctx) | ||
if nodeType.Type == "provider" && (nestingOk && nestingLvl == 0) { | ||
ctx = WithUnknownRequiredAttributes(ctx) | ||
} | ||
case *hclsyntax.Body: | ||
if nodeSchema == nil { | ||
return ctx, diags | ||
} | ||
|
||
bodySchema := nodeSchema.(*schema.BodySchema) | ||
if bodySchema.Attributes == nil { | ||
return ctx, diags | ||
} | ||
|
||
for name, attr := range bodySchema.Attributes { | ||
if attr.IsRequired { | ||
_, ok := nodeType.Attributes[name] | ||
if !ok { | ||
diags = append(diags, &hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: fmt.Sprintf("Required attribute %q not specified", name), | ||
Detail: fmt.Sprintf("An attribute named %q is required here", name), | ||
Subject: nodeType.SrcRange.Ptr(), | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ctx, diags | ||
} | ||
|
||
type unknownRequiredAttrsCtxKey struct{} | ||
|
||
func HasUnknownRequiredAttributes(ctx context.Context) bool { | ||
_, ok := ctx.Value(unknownRequiredAttrsCtxKey{}).(bool) | ||
return ok | ||
} | ||
|
||
func WithUnknownRequiredAttributes(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, unknownRequiredAttrsCtxKey{}, true) | ||
} |
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