github.com/sri09kanth/helm@v3.0.0-beta.3+incompatible/pkg/chartutil/jsonschema.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package chartutil
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/xeipuuv/gojsonschema"
    26  	"sigs.k8s.io/yaml"
    27  
    28  	"helm.sh/helm/pkg/chart"
    29  )
    30  
    31  // ValidateAgainstSchema checks that values does not violate the structure laid out in schema
    32  func ValidateAgainstSchema(chrt *chart.Chart, values map[string]interface{}) error {
    33  	var sb strings.Builder
    34  	if chrt.Schema != nil {
    35  		err := ValidateAgainstSingleSchema(values, chrt.Schema)
    36  		if err != nil {
    37  			sb.WriteString(fmt.Sprintf("%s:\n", chrt.Name()))
    38  			sb.WriteString(err.Error())
    39  		}
    40  	}
    41  
    42  	// For each dependency, recurively call this function with the coalesced values
    43  	for _, subchrt := range chrt.Dependencies() {
    44  		subchrtValues := values[subchrt.Name()].(map[string]interface{})
    45  		if err := ValidateAgainstSchema(subchrt, subchrtValues); err != nil {
    46  			sb.WriteString(err.Error())
    47  		}
    48  	}
    49  
    50  	if sb.Len() > 0 {
    51  		return errors.New(sb.String())
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // ValidateAgainstSingleSchema checks that values does not violate the structure laid out in this schema
    58  func ValidateAgainstSingleSchema(values Values, schemaJSON []byte) error {
    59  	valuesData, err := yaml.Marshal(values)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	valuesJSON, err := yaml.YAMLToJSON(valuesData)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	if bytes.Equal(valuesJSON, []byte("null")) {
    68  		valuesJSON = []byte("{}")
    69  	}
    70  	schemaLoader := gojsonschema.NewBytesLoader(schemaJSON)
    71  	valuesLoader := gojsonschema.NewBytesLoader(valuesJSON)
    72  
    73  	result, err := gojsonschema.Validate(schemaLoader, valuesLoader)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	if !result.Valid() {
    79  		var sb strings.Builder
    80  		for _, desc := range result.Errors() {
    81  			sb.WriteString(fmt.Sprintf("- %s\n", desc))
    82  		}
    83  		return errors.New(sb.String())
    84  	}
    85  
    86  	return nil
    87  }