diff --git a/pkg/chartutil/values.go b/pkg/chartutil/values.go index acc5990cf..29acd57eb 100644 --- a/pkg/chartutil/values.go +++ b/pkg/chartutil/values.go @@ -185,73 +185,6 @@ func ValidateAgainstSchema(values Values, schema Schema) error { return nil } -// GenerateSchema will create a JSON Schema (in YAML format) for the given values -func GenerateSchema(values Values) Schema { - schema := Schema{ - gojsonschema.KEY_TYPE: gojsonschema.TYPE_OBJECT, - gojsonschema.KEY_TITLE: "Values", - } - if len(values) > 0 { - schema[gojsonschema.STRING_PROPERTIES] = parsePropertiesFromValues(values) - } - return schema -} - -func parsePropertiesFromValues(values Values) map[string]map[string]interface{} { - properties := make(map[string]map[string]interface{}) - for k, v := range values { - // If the value is null, then there's no way to determine the properties attributes - if v == nil || v == "" { - continue - } - - properties[k] = make(map[string]interface{}) - // the following types are the only types possible from unmarshalling - switch v := v.(type) { - case bool: - properties[k][gojsonschema.KEY_TYPE] = gojsonschema.TYPE_BOOLEAN - case float64: - properties[k][gojsonschema.KEY_TYPE] = gojsonschema.TYPE_NUMBER - case string: - properties[k][gojsonschema.KEY_TYPE] = gojsonschema.TYPE_STRING - case []interface{}: - properties[k][gojsonschema.KEY_TYPE] = gojsonschema.TYPE_ARRAY - properties[k][gojsonschema.KEY_ITEMS] = parseItemsFromValues(v) - case map[string]interface{}: - properties[k][gojsonschema.KEY_TYPE] = gojsonschema.TYPE_OBJECT - object := parsePropertiesFromValues(v) - if len(object) > 0 { - properties[k][gojsonschema.TYPE_OBJECT] = object - } - } - } - return properties -} - -func parseItemsFromValues(items []interface{}) map[string]interface{} { - properties := make(map[string]interface{}) - v := items[0] - // the following types are the only types possible from unmarshalling - switch v := v.(type) { - case bool: - properties[gojsonschema.KEY_TYPE] = gojsonschema.TYPE_BOOLEAN - case float64: - properties[gojsonschema.KEY_TYPE] = gojsonschema.TYPE_NUMBER - case string: - properties[gojsonschema.KEY_TYPE] = gojsonschema.TYPE_STRING - case []interface{}: - properties[gojsonschema.KEY_TYPE] = gojsonschema.TYPE_ARRAY - properties[gojsonschema.KEY_ITEMS] = parseItemsFromValues(v) - case map[string]interface{}: - properties[gojsonschema.KEY_TYPE] = gojsonschema.TYPE_OBJECT - object := parsePropertiesFromValues(v) - if len(object) > 0 { - properties[gojsonschema.TYPE_OBJECT] = object - } - } - return properties -} - // CoalesceValues coalesces all of the values in a chart (and its subcharts). // // Values are coalesced together using the following rules: diff --git a/pkg/chartutil/values_test.go b/pkg/chartutil/values_test.go index 5dd5693b3..3ad9caebc 100644 --- a/pkg/chartutil/values_test.go +++ b/pkg/chartutil/values_test.go @@ -574,51 +574,6 @@ func matchSchema(t *testing.T, data Schema) { assertEqualProperty(t, ".required", "[firstname lastname addresses employmentInfo]", data) } -func TestGenerateSchema(t *testing.T) { - doc := `# Test YAML parse -firstname: John -middlename: null -lastname: Doe -age: 25 -likesCoffee: true -employmentInfo: - title: Software Developer - salary: 100000 -addresses: - - city: Springfield - street: Main - number: 12345 - - city: New York - street: Broadway - number: 67890 -phoneNumbers: - - "(888) 888-8888" - - "(555) 555-5555" -` - - values, err := ReadValues([]byte(doc)) - if err != nil { - t.Fatalf("Error reading values: %s", err) - } - schema := GenerateSchema(values) - assertEqualProperty(t, ".title", "Values", schema) - assertEqualProperty(t, ".type", "object", schema) - assertEqualProperty(t, ".properties.firstname.type", "string", schema) - assertEqualProperty(t, ".properties.lastname.type", "string", schema) - assertEqualProperty(t, ".properties.age.type", "number", schema) - assertEqualProperty(t, ".properties.likesCoffee.type", "boolean", schema) - assertEqualProperty(t, ".properties.employmentInfo.type", "object", schema) - assertEqualProperty(t, ".properties.employmentInfo.object.title.type", "string", schema) - assertEqualProperty(t, ".properties.employmentInfo.object.salary.type", "number", schema) - assertEqualProperty(t, ".properties.addresses.type", "array", schema) - assertEqualProperty(t, ".properties.addresses.items.type", "object", schema) - assertEqualProperty(t, ".properties.addresses.items.object.street.type", "string", schema) - assertEqualProperty(t, ".properties.addresses.items.object.number.type", "number", schema) - assertEqualProperty(t, ".properties.addresses.items.object.city.type", "string", schema) - assertEqualProperty(t, ".properties.phoneNumbers.type", "array", schema) - assertEqualProperty(t, ".properties.phoneNumbers.items.type", "string", schema) -} - func assertEqualProperty(t *testing.T, property, expected string, data map[string]interface{}) { if o, err := ttpl("{{"+property+"}}", data); err != nil { t.Errorf("%s: %s", property, err)