Skip to content

Commit

Permalink
helper/schema: Allow and require self references with AllowOneOf and …
Browse files Browse the repository at this point in the history
…ExactlyOneOf in InternalValidate

Reference: #407
Reference: #416
  • Loading branch information
bflad authored and appilon committed Apr 30, 2020
1 parent 04d0c8a commit a370adc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
23 changes: 17 additions & 6 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,28 +742,28 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
}

if len(v.ConflictsWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.ConflictsWith, topSchemaMap, v, false)
if err != nil {
return fmt.Errorf("ConflictsWith: %+v", err)
}
}

if len(v.RequiredWith) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.RequiredWith, topSchemaMap, v, false)
if err != nil {
return fmt.Errorf("RequiredWith: %+v", err)
}
}

if len(v.ExactlyOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.ExactlyOneOf, topSchemaMap, v, true)
if err != nil {
return fmt.Errorf("ExactlyOneOf: %+v", err)
}
}

if len(v.AtLeastOneOf) > 0 {
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v)
err := checkKeysAgainstSchemaFlags(k, v.AtLeastOneOf, topSchemaMap, v, true)
if err != nil {
return fmt.Errorf("AtLeastOneOf: %+v", err)
}
Expand Down Expand Up @@ -889,7 +889,9 @@ func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) erro
return nil
}

func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema) error {
func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap, self *Schema, requireSelfReference bool) error {
var foundSelfReference bool

for _, key := range keys {
parts := strings.Split(key, ".")
sm := topSchemaMap
Expand Down Expand Up @@ -929,7 +931,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
}

if target == self {
return fmt.Errorf("%s cannot reference self (%s)", k, key)
if !requireSelfReference {
return fmt.Errorf("%s cannot reference self (%s)", k, key)
}

foundSelfReference = true
}

if target.Required {
Expand All @@ -940,6 +946,11 @@ func checkKeysAgainstSchemaFlags(k string, keys []string, topSchemaMap schemaMap
return fmt.Errorf("%s cannot contain Computed(When) attribute (%s)", k, key)
}
}

if requireSelfReference && !foundSelfReference {
return fmt.Errorf("%s must reference self for AtLeastOneOf/ExactlyOneOf", k)
}

return nil
}

Expand Down
18 changes: 10 additions & 8 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3315,6 +3315,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
"config_block_attr": {
Type: TypeList,
Optional: true,
MaxItems: 1,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Expand All @@ -3326,7 +3327,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
true, // TODO: AtLeastOneOf self reference is necessary
false,
},

"AtLeastOneOf list index syntax with list configuration block existing attribute": {
Expand All @@ -3350,7 +3351,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"config_block_attr.0.nested_attr"},
},
},
false, // TODO: AtLeastOneOf self reference is necessary
true,
},

"AtLeastOneOf list index syntax with list configuration block missing attribute": {
Expand Down Expand Up @@ -3558,7 +3559,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"map_attr"},
},
},
false, // TODO: AtLeastOneOf self reference is necessary
true,
},

"AtLeastOneOf string syntax with self reference": {
Expand All @@ -3569,7 +3570,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"test"},
},
},
true, // TODO: AtLeastOneOf self reference is necessary
false,
},

"ConflictsWith list index syntax with self reference": {
Expand Down Expand Up @@ -3839,6 +3840,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
"config_block_attr": {
Type: TypeList,
Optional: true,
MaxItems: 1,
Elem: &Resource{
Schema: map[string]*Schema{
"nested_attr": {
Expand All @@ -3850,7 +3852,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
},
},
},
true, // TODO: ExactlyOneOf self reference is necessary
false,
},

"ExactlyOneOf list index syntax with list configuration block existing attribute": {
Expand All @@ -3874,7 +3876,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ExactlyOneOf: []string{"config_block_attr.0.nested_attr"},
},
},
false, // TODO: ExactlyOneOf self reference is necessary
true,
},

"ExactlyOneOf list index syntax with list configuration block missing attribute": {
Expand Down Expand Up @@ -4082,7 +4084,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
ExactlyOneOf: []string{"map_attr"},
},
},
false, // TODO: ExactlyOneOf self reference is necessary
true,
},

"ExactlyOneOf string syntax with self reference": {
Expand All @@ -4093,7 +4095,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
AtLeastOneOf: []string{"test"},
},
},
true, // TODO: ExactlyOneOf self reference is necessary
false,
},

"Sub-resource invalid": {
Expand Down

0 comments on commit a370adc

Please sign in to comment.