diff --git a/functions/core/truthy.go b/functions/core/truthy.go index 1633d645..d5e3dfef 100644 --- a/functions/core/truthy.go +++ b/functions/core/truthy.go @@ -60,24 +60,25 @@ func (t *Truthy) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) } if !utils.IsNodeMap(fieldNode) && !utils.IsNodeArray(fieldNodeValue) && !utils.IsNodeMap(fieldNodeValue) { - origin := context.Index.FindNodeOrigin(node) - if origin.Line > 1 { - nm := context.Index.GetNodeMap() - var keys []int - for k := range nm { - keys = append(keys, k) - } - - // Sort the keys slice. - sort.Ints(keys) - - np := nm[origin.Line-1][keys[0]] - - if np != nil { - node = np + if context.Index != nil { + origin := context.Index.FindNodeOrigin(node) + if origin != nil && origin.Line > 1 { + nm := context.Index.GetNodeMap() + var keys []int + for k := range nm { + keys = append(keys, k) + } + + // Sort the keys slice. + sort.Ints(keys) + + np := nm[origin.Line-1][keys[0]] + + if np != nil { + node = np + } } } - results = append(results, model.RuleFunctionResult{ Message: vacuumUtils.SuppliedOrDefault(message, fmt.Sprintf("%s: `%s` must be set", ruleMessage, context.RuleAction.Field)), diff --git a/motor/rule_applicator_test.go b/motor/rule_applicator_test.go index 92cd2819..67f6b3d1 100644 --- a/motor/rule_applicator_test.go +++ b/motor/rule_applicator_test.go @@ -646,7 +646,8 @@ func TestApplyRules_Length_Description_BadConfig(t *testing.T) { } results := ApplyRulesToRuleSet(rse) assert.Len(t, results.Errors, 0) - assert.Len(t, results.Results, 0) + assert.Len(t, results.Results, 1) + assert.Equal(t, "'length' needs 'min' or 'max' (or both) properties being set to operate: minimum property number not met (1)", results.Results[0].Message) } @@ -2058,7 +2059,7 @@ custom: assert.Len(t, results.Results, 1) assert.Equal(t, "name 'hello' and id '1234' are not 'some_name' or 'some_id'", results.Results[0].Message) - assert.Equal(t, "my-custom-js-rule", results.Results[0].RuleId) + assert.Equal(t, "my-custom-js-rule", results.Results[0].Rule.Id) assert.Equal(t, 3, results.Results[0].Range.Start.Line) } @@ -2104,7 +2105,7 @@ notCustom: true" assert.Len(t, results.Results, 2) assert.Equal(t, "core me up: `custom` must be set", results.Results[0].Message) - assert.Equal(t, "my-custom-js-rule", results.Results[0].RuleId) + assert.Equal(t, "my-custom-js-rule", results.Results[0].Rule.Id) assert.Equal(t, "this is a message, added after truthy was called", results.Results[1].Message) assert.Equal(t, 2, results.Results[0].Range.Start.Line) } @@ -2202,7 +2203,7 @@ notCustom: true" assert.Len(t, results.Results, 1) assert.Equal(t, "someOption is set to someValue", results.Results[0].Message) - assert.Equal(t, "my-custom-js-rule", results.Results[0].RuleId) + assert.Equal(t, "my-custom-js-rule", results.Results[0].Rule.Id) assert.Equal(t, 2, results.Results[0].Range.Start.Line) }