1
0
mirror of https://github.com/rancher/os.git synced 2025-04-29 11:54:18 +00:00
os/config/validate.go

48 lines
1.3 KiB
Go
Raw Permalink Normal View History

2016-11-06 21:41:46 +00:00
package config
import (
yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/xeipuuv/gojsonschema"
)
2016-11-29 01:55:05 +00:00
// ConvertKeysToStrings is temporarily copied from libcompose
// TODO: just import this in the future
2016-11-27 07:27:42 +00:00
func ConvertKeysToStrings(item interface{}) interface{} {
2016-11-06 21:41:46 +00:00
switch typedDatas := item.(type) {
case map[string]interface{}:
for key, value := range typedDatas {
2016-11-27 07:27:42 +00:00
typedDatas[key] = ConvertKeysToStrings(value)
2016-11-06 21:41:46 +00:00
}
return typedDatas
case map[interface{}]interface{}:
newMap := make(map[string]interface{})
for key, value := range typedDatas {
stringKey := key.(string)
2016-11-27 07:27:42 +00:00
newMap[stringKey] = ConvertKeysToStrings(value)
2016-11-06 21:41:46 +00:00
}
return newMap
case []interface{}:
for i, value := range typedDatas {
2016-11-27 07:27:42 +00:00
typedDatas[i] = ConvertKeysToStrings(value)
2016-11-06 21:41:46 +00:00
}
return typedDatas
default:
return item
}
}
func ValidateBytes(bytes []byte) (*gojsonschema.Result, error) {
2016-11-06 21:41:46 +00:00
var rawCfg map[string]interface{}
if err := yaml.Unmarshal([]byte(bytes), &rawCfg); err != nil {
return nil, err
}
return ValidateRawCfg(rawCfg)
}
func ValidateRawCfg(rawCfg interface{}) (*gojsonschema.Result, error) {
2016-11-27 07:27:42 +00:00
rawCfg = ConvertKeysToStrings(rawCfg).(map[string]interface{})
2016-11-06 21:41:46 +00:00
loader := gojsonschema.NewGoLoader(rawCfg)
schemaLoader := gojsonschema.NewStringLoader(schema)
return gojsonschema.Validate(schemaLoader, loader)
}