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

Treat empty blocks with MinItems == 0 as optional #99

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion schemamd/behaviors.go
Expand Up @@ -16,12 +16,17 @@ func childAttributeIsOptional(att *tfjson.SchemaAttribute) bool {
return att.Optional
}

// childBlockIsOptional returns true for blocks with with min items 0 and any required or optional children.
// childBlockIsOptional returns true for blocks with with min items 0
// which are either empty or have any required or optional children.
func childBlockIsOptional(block *tfjson.SchemaBlockType) bool {
if block.MinItems > 0 {
return false
}

if len(block.Block.NestedBlocks) == 0 && len(block.Block.Attributes) == 0 {
return true
}

for _, childBlock := range block.Block.NestedBlocks {
if childBlockIsRequired(childBlock) {
return true
Expand Down
10 changes: 10 additions & 0 deletions schemamd/behaviors_test.go
Expand Up @@ -324,6 +324,16 @@ func TestChildBlockIsOptional(t *testing.T) {
},
false,
},
{
"empty block",
&tfjson.SchemaBlockType{
NestingMode: tfjson.SchemaNestingModeSingle,
Block: &tfjson.SchemaBlock{
Description: "This is an empty block.",
},
},
true,
},
} {
t.Run(c.name, func(t *testing.T) {
actual := childBlockIsOptional(c.block)
Expand Down