1
0
mirror of https://github.com/rancher/os.git synced 2025-09-01 14:48:55 +00:00

Add command to validate configuration

This commit is contained in:
Josh Curl
2016-11-06 13:41:46 -08:00
parent 33b6145162
commit 25e5ca5e4c
7 changed files with 534 additions and 12 deletions

42
config/validate.go Normal file
View File

@@ -0,0 +1,42 @@
package config
import (
yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/xeipuuv/gojsonschema"
)
// TODO: use this function from libcompose
func convertKeysToStrings(item interface{}) interface{} {
switch typedDatas := item.(type) {
case map[string]interface{}:
for key, value := range typedDatas {
typedDatas[key] = convertKeysToStrings(value)
}
return typedDatas
case map[interface{}]interface{}:
newMap := make(map[string]interface{})
for key, value := range typedDatas {
stringKey := key.(string)
newMap[stringKey] = convertKeysToStrings(value)
}
return newMap
case []interface{}:
for i, value := range typedDatas {
typedDatas[i] = append(typedDatas, convertKeysToStrings(value))
}
return typedDatas
default:
return item
}
}
func Validate(bytes []byte) (*gojsonschema.Result, error) {
var rawCfg map[string]interface{}
if err := yaml.Unmarshal([]byte(bytes), &rawCfg); err != nil {
return nil, err
}
rawCfg = convertKeysToStrings(rawCfg).(map[string]interface{})
loader := gojsonschema.NewGoLoader(rawCfg)
schemaLoader := gojsonschema.NewStringLoader(schema)
return gojsonschema.Validate(schemaLoader, loader)
}