Skip to content

Commit 2b2a132

Browse files
JemDaydaveshanley
authored andcommitted
- Address PR code coverage.
- Added more 'nil' protection
1 parent a981741 commit 2b2a132

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

helpers/schema_compiler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ func NewCompiledSchema(name string, jsonSchema []byte, o *config.ValidationOptio
5858
}
5959

6060
// Give our schema to the compiler.
61-
err = compiler.AddResource(resourceName, decodedSchema)
62-
if err != nil {
61+
if err = compiler.AddResource(resourceName, decodedSchema); err != nil {
6362
return nil, fmt.Errorf("failed to add resource to schema compiler: %w", err)
6463
}
6564

parameters/validate_parameter.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ func ValidateSingleParameterSchema(
3232
subValType string,
3333
o *config.ValidationOptions,
3434
) (validationErrors []*errors.ValidationError) {
35+
// Get the JSON Schema for the parameter definition.
36+
jsonSchema, err := buildJsonRender(schema)
37+
if err != nil {
38+
return
39+
}
40+
3541
// Attempt to compile the JSON Schema
36-
jsch, err := helpers.NewCompiledSchema(name, buildJsonRender(schema), o)
42+
jsch, err := helpers.NewCompiledSchema(name, jsonSchema, o)
3743
if err != nil {
3844
return
3945
}
@@ -48,10 +54,18 @@ func ValidateSingleParameterSchema(
4854
}
4955

5056
// buildJsonRender build a JSON render of the schema.
51-
func buildJsonRender(schema *base.Schema) []byte {
52-
renderedSchema, _ := schema.Render()
53-
jsonSchema, _ := utils.ConvertYAMLtoJSON(renderedSchema)
54-
return jsonSchema
57+
func buildJsonRender(schema *base.Schema) ([]byte, error) {
58+
if schema == nil {
59+
// Sanity Check
60+
return nil, stdError.New("buildJSONRender nil pointer")
61+
}
62+
63+
renderedSchema, err := schema.Render()
64+
if err != nil {
65+
return nil, err
66+
}
67+
68+
return utils.ConvertYAMLtoJSON(renderedSchema)
5569
}
5670

5771
// ValidateParameterSchema will validate a parameter against a raw object, or a blob of json/yaml.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package parameters
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func Test_ForceCompilerError(t *testing.T) {
10+
// Try to force a panic
11+
result := ValidateSingleParameterSchema(nil, nil, "", "", "", "", "", nil)
12+
13+
// Ideally this would result in an error response, current behavior swallows the error
14+
require.Empty(t, result)
15+
}

0 commit comments

Comments
 (0)