Clean up implementation in chartutil

Signed-off-by: Ian Howell <ian.howell0@gmail.com>
pull/5350/head
Ian Howell 7 years ago
parent 1ad4a26a96
commit a77a6586fc

@ -1,45 +0,0 @@
title: Values
type: object
properties:
firstname:
description: First name
type: string
lastname:
type: string
likesCoffee:
type: boolean
age:
description: Age
type: integer
minimum: 0
employmentInfo:
type: object
properties:
salary:
type: number
minimum: 0
title:
type: string
required:
- salary
addresses:
description: List of addresses
type: array
items:
type: object
properties:
city:
type: string
street:
type: string
number:
type: number
phoneNumbers:
type: array
items:
type: string
required:
- firstname
- lastname
- addresses
- employmentInfo

@ -22,7 +22,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"strings" "strings"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
@ -145,18 +144,6 @@ func ReadValuesFile(filename string) (Values, error) {
if err != nil { if err != nil {
return map[string]interface{}{}, err return map[string]interface{}{}, err
} }
schemaPath := strings.Replace(filename, ".yaml", ".schema.yaml", 1)
_, err = os.Stat(schemaPath)
if err == nil {
schemaData, err := ioutil.ReadFile(schemaPath)
if err != nil {
return map[string]interface{}{}, err
}
return ReadSchematizedValues(data, schemaData)
}
return ReadValues(data) return ReadValues(data)
} }
@ -169,38 +156,28 @@ func ReadSchemaFile(filename string) (Schema, error) {
return ReadSchema(data) return ReadSchema(data)
} }
// ReadSchematizedValues parses a YAML file and asserts that it matches the schema // ValidateAgainstSchema checks that values does not violate the structure laid out in schema
func ReadSchematizedValues(data, schemaData []byte) (Values, error) { func ValidateAgainstSchema(values Values, schema Schema) error {
values, err := ReadValues(data)
if err != nil {
return Values{}, err
}
valuesJSON := convertToJSON(values) valuesJSON := convertToJSON(values)
schema, err := ReadSchema(schemaData)
if err != nil {
return Values{}, err
}
schemaJSON := convertToJSON(schema) schemaJSON := convertToJSON(schema)
schemaLoader := gojsonschema.NewStringLoader(string(schemaJSON)) schemaLoader := gojsonschema.NewStringLoader(string(schemaJSON))
valuesLoader := gojsonschema.NewStringLoader(string(valuesJSON)) valuesLoader := gojsonschema.NewStringLoader(string(valuesJSON))
result, err := gojsonschema.Validate(schemaLoader, valuesLoader) result, err := gojsonschema.Validate(schemaLoader, valuesLoader)
if err != nil { if err != nil {
return Values{}, err return err
} }
if !result.Valid() { if !result.Valid() {
var sb strings.Builder var sb strings.Builder
sb.WriteString("The values.yaml is not valid. see errors :\n") sb.WriteString(".Values does not meet the specification of values.schema.yaml . see errors :\n")
for _, desc := range result.Errors() { for _, desc := range result.Errors() {
sb.WriteString(fmt.Sprintf("- %s\n", desc)) sb.WriteString(fmt.Sprintf("- %s\n", desc))
} }
return Values{}, errors.New(sb.String()) return errors.New(sb.String())
} }
return values, nil return nil
} }
// GenerateSchema will create a JSON Schema (in YAML format) for the given values // GenerateSchema will create a JSON Schema (in YAML format) for the given values
@ -468,6 +445,12 @@ func ToRenderValues(chrt *chart.Chart, chrtVals map[string]interface{}, options
return top, err return top, err
} }
if chrt.Schema != nil {
if err := ValidateAgainstSchema(vals, chrt.Schema); err != nil {
return top, err
}
}
top["Values"] = vals top["Values"] = vals
return top, nil return top, nil
} }

@ -169,23 +169,40 @@ func TestReadSchemaFile(t *testing.T) {
matchSchema(t, data) matchSchema(t, data)
} }
func TestReadSchematizedValues(t *testing.T) { func TestValidateAgainstSchema(t *testing.T) {
_, err := ReadValuesFile("./testdata/test-values.yaml") values, err := ReadValuesFile("./testdata/test-values.yaml")
if err != nil { if err != nil {
t.Errorf("Got the following unexpected error while reading schematized values:\n%v", err) t.Fatalf("Error reading YAML file: %s", err)
}
schema, err := ReadSchemaFile("./testdata/test-values.schema.yaml")
if err != nil {
t.Fatalf("Error reading YAML file: %s", err)
}
if err := ValidateAgainstSchema(values, schema); err != nil {
t.Errorf("Error validating Values against Schema: %s", err)
} }
} }
func TestReadSchematizedValuesNegative(t *testing.T) { func TestValidateAgainstSchemaNegative(t *testing.T) {
_, err := ReadValuesFile("./testdata/test-values-negative.yaml") values, err := ReadValuesFile("./testdata/test-values-negative.yaml")
if err != nil {
t.Fatalf("Error reading YAML file: %s", err)
}
schema, err := ReadSchemaFile("./testdata/test-values.schema.yaml")
if err != nil {
t.Fatalf("Error reading YAML file: %s", err)
}
if err == nil { var errString string
t.Errorf("Expected an error, but got none") if err := ValidateAgainstSchema(values, schema); err == nil {
t.Errorf("Expected an error, but got nil")
} else {
errString = err.Error()
} }
errString := err.Error() if !strings.Contains(errString, ".Values does not meet the specification of values.schema.yaml . see errors :") {
if !strings.Contains(errString, "The values.yaml is not valid. see errors :") { t.Errorf("Error string does not contain expected string: \".Values does not meet the specification of values.schema.yaml . see errors :\"")
t.Errorf("Error string does not contain expected string: \"The values.yaml is not valid. see errors :\"")
} }
if !strings.Contains(errString, "- (root): employmentInfo is required") { if !strings.Contains(errString, "- (root): employmentInfo is required") {
t.Errorf("Error string does not contain expected string: \"- (root): employmentInfo is required\"") t.Errorf("Error string does not contain expected string: \"- (root): employmentInfo is required\"")

Loading…
Cancel
Save