fix Min/Max validation during decoding

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.
This commit is contained in:
James Bardin 2019-08-20 09:49:41 -04:00
parent 731d4226d3
commit 13e2e10577
2 changed files with 4 additions and 6 deletions

View File

@ -35,7 +35,8 @@ func (b *Block) DecoderSpec() hcldec.Spec {
// We can only validate 0 or 1 for MinItems, because a dynamic block // 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 // 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 minItems := 0
if blockS.MinItems > 1 { if blockS.MinItems > 1 {
minItems = 1 minItems = 1
@ -46,7 +47,7 @@ func (b *Block) DecoderSpec() hcldec.Spec {
ret[name] = &hcldec.BlockSpec{ ret[name] = &hcldec.BlockSpec{
TypeName: name, TypeName: name,
Nested: childSpec, Nested: childSpec,
Required: blockS.MinItems == 1 && blockS.MaxItems >= 1, Required: blockS.MinItems == 1,
} }
if blockS.Nesting == NestingGroup { if blockS.Nesting == NestingGroup {
ret[name] = &hcldec.DefaultSpec{ ret[name] = &hcldec.DefaultSpec{
@ -66,14 +67,12 @@ func (b *Block) DecoderSpec() hcldec.Spec {
TypeName: name, TypeName: name,
Nested: childSpec, Nested: childSpec,
MinItems: minItems, MinItems: minItems,
MaxItems: blockS.MaxItems,
} }
} else { } else {
ret[name] = &hcldec.BlockListSpec{ ret[name] = &hcldec.BlockListSpec{
TypeName: name, TypeName: name,
Nested: childSpec, Nested: childSpec,
MinItems: minItems, MinItems: minItems,
MaxItems: blockS.MaxItems,
} }
} }
case NestingSet: case NestingSet:
@ -86,7 +85,6 @@ func (b *Block) DecoderSpec() hcldec.Spec {
TypeName: name, TypeName: name,
Nested: childSpec, Nested: childSpec,
MinItems: minItems, MinItems: minItems,
MaxItems: blockS.MaxItems,
} }
case NestingMap: case NestingMap:
// We prefer to use a list where possible, since it makes our // We prefer to use a list where possible, since it makes our

View File

@ -354,7 +354,7 @@ func TestBlockDecoderSpec(t *testing.T) {
cty.EmptyObjectVal, 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 // dynamic blocks may fulfill MinItems, but there is only one block to
// decode. // decode.