Skip to content

Commit

Permalink
fix Min/Max validation during decoding
Browse files Browse the repository at this point in the history
We can only validate MinItems >= 1 (equiv to "Required") during
decoding, as dynamic blocks each only decode as a single block. MaxItems
cannot be validated at all, also because of dynamic blocks, which may
have any number of blocks in the config.
  • Loading branch information
jbardin committed Aug 20, 2019
1 parent 731d422 commit 13e2e10
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions configs/configschema/decoder_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (b *Block) DecoderSpec() hcldec.Spec {

// We can only validate 0 or 1 for MinItems, because a dynamic block
// may satisfy any number of min items while only having a single
// block in the config.
// block in the config. We cannot validate MaxItems because a
// configuration may have any number of dynamic blocks
minItems := 0
if blockS.MinItems > 1 {
minItems = 1
Expand All @@ -46,7 +47,7 @@ func (b *Block) DecoderSpec() hcldec.Spec {
ret[name] = &hcldec.BlockSpec{
TypeName: name,
Nested: childSpec,
Required: blockS.MinItems == 1 && blockS.MaxItems >= 1,
Required: blockS.MinItems == 1,
}
if blockS.Nesting == NestingGroup {
ret[name] = &hcldec.DefaultSpec{
Expand All @@ -66,14 +67,12 @@ func (b *Block) DecoderSpec() hcldec.Spec {
TypeName: name,
Nested: childSpec,
MinItems: minItems,
MaxItems: blockS.MaxItems,
}
} else {
ret[name] = &hcldec.BlockListSpec{
TypeName: name,
Nested: childSpec,
MinItems: minItems,
MaxItems: blockS.MaxItems,
}
}
case NestingSet:
Expand All @@ -86,7 +85,6 @@ func (b *Block) DecoderSpec() hcldec.Spec {
TypeName: name,
Nested: childSpec,
MinItems: minItems,
MaxItems: blockS.MaxItems,
}
case NestingMap:
// We prefer to use a list where possible, since it makes our
Expand Down
2 changes: 1 addition & 1 deletion configs/configschema/decoder_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func TestBlockDecoderSpec(t *testing.T) {
cty.EmptyObjectVal,
}),
}),
1, // too many "foo" blocks
0, // max items cannot be validated during decode
},
// dynamic blocks may fulfill MinItems, but there is only one block to
// decode.
Expand Down

0 comments on commit 13e2e10

Please sign in to comment.