github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/specification/schema.go (about)

     1  package specification
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/xeipuuv/gojsonschema"
    10  )
    11  
    12  //go:generate esc -o bindata.go -pkg specification -ignore .*\.go -private -modtime=1518458244 schemas
    13  
    14  // Validate uses the jsonschema to validate the configuration
    15  func Validate(config map[string]interface{}, version string) error {
    16  	schemaData, err := _escFSByte(false, fmt.Sprintf("/schemas/metadata_schema_%s.json", version))
    17  	if err != nil {
    18  		return errors.Errorf("unsupported metadata version: %s", version)
    19  	}
    20  
    21  	schemaLoader := gojsonschema.NewStringLoader(string(schemaData))
    22  	dataLoader := gojsonschema.NewGoLoader(config)
    23  
    24  	result, err := gojsonschema.Validate(schemaLoader, dataLoader)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	if !result.Valid() {
    30  		errs := make([]string, len(result.Errors()))
    31  		for i, err := range result.Errors() {
    32  			errs[i] = fmt.Sprintf("- %s", err)
    33  		}
    34  		sort.Strings(errs)
    35  		return errors.New(strings.Join(errs, "\n"))
    36  	}
    37  
    38  	return nil
    39  
    40  }