mirror of
https://github.com/rancher/os.git
synced 2025-09-13 05:33:34 +00:00
Merge pull request #1425 from joshwget/validation-fixes
Validation fixes
This commit is contained in:
@@ -63,6 +63,7 @@ var schema = `{
|
|||||||
|
|
||||||
"properties": {
|
"properties": {
|
||||||
"encoding": {"type": "string"},
|
"encoding": {"type": "string"},
|
||||||
|
"container": {"type": "string"},
|
||||||
"content": {"type": "string"},
|
"content": {"type": "string"},
|
||||||
"owner": {"type": "string"},
|
"owner": {"type": "string"},
|
||||||
"path": {"type": "string"},
|
"path": {"type": "string"},
|
||||||
|
@@ -6,23 +6,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// TODO: use this function from libcompose
|
// TODO: use this function from libcompose
|
||||||
func convertKeysToStrings(item interface{}) interface{} {
|
func ConvertKeysToStrings(item interface{}) interface{} {
|
||||||
switch typedDatas := item.(type) {
|
switch typedDatas := item.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
for key, value := range typedDatas {
|
for key, value := range typedDatas {
|
||||||
typedDatas[key] = convertKeysToStrings(value)
|
typedDatas[key] = ConvertKeysToStrings(value)
|
||||||
}
|
}
|
||||||
return typedDatas
|
return typedDatas
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
newMap := make(map[string]interface{})
|
newMap := make(map[string]interface{})
|
||||||
for key, value := range typedDatas {
|
for key, value := range typedDatas {
|
||||||
stringKey := key.(string)
|
stringKey := key.(string)
|
||||||
newMap[stringKey] = convertKeysToStrings(value)
|
newMap[stringKey] = ConvertKeysToStrings(value)
|
||||||
}
|
}
|
||||||
return newMap
|
return newMap
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
for i, value := range typedDatas {
|
for i, value := range typedDatas {
|
||||||
typedDatas[i] = append(typedDatas, convertKeysToStrings(value))
|
typedDatas[i] = ConvertKeysToStrings(value)
|
||||||
}
|
}
|
||||||
return typedDatas
|
return typedDatas
|
||||||
default:
|
default:
|
||||||
@@ -35,7 +35,7 @@ func Validate(bytes []byte) (*gojsonschema.Result, error) {
|
|||||||
if err := yaml.Unmarshal([]byte(bytes), &rawCfg); err != nil {
|
if err := yaml.Unmarshal([]byte(bytes), &rawCfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rawCfg = convertKeysToStrings(rawCfg).(map[string]interface{})
|
rawCfg = ConvertKeysToStrings(rawCfg).(map[string]interface{})
|
||||||
loader := gojsonschema.NewGoLoader(rawCfg)
|
loader := gojsonschema.NewGoLoader(rawCfg)
|
||||||
schemaLoader := gojsonschema.NewStringLoader(schema)
|
schemaLoader := gojsonschema.NewStringLoader(schema)
|
||||||
return gojsonschema.Validate(schemaLoader, loader)
|
return gojsonschema.Validate(schemaLoader, loader)
|
||||||
|
@@ -25,8 +25,19 @@ func testValidate(t *testing.T, cfg []byte, contains string) {
|
|||||||
func TestValidate(t *testing.T) {
|
func TestValidate(t *testing.T) {
|
||||||
testValidate(t, []byte("{}"), "")
|
testValidate(t, []byte("{}"), "")
|
||||||
testValidate(t, []byte(`rancher:
|
testValidate(t, []byte(`rancher:
|
||||||
log: true
|
log: true`), "")
|
||||||
`), "")
|
testValidate(t, []byte(`write_files:
|
||||||
|
- container: console
|
||||||
|
path: /etc/rc.local
|
||||||
|
permissions: "0755"
|
||||||
|
owner: root
|
||||||
|
content: |
|
||||||
|
#!/bin/bash
|
||||||
|
wait-for-docker`), "")
|
||||||
|
testValidate(t, []byte(`rancher:
|
||||||
|
docker:
|
||||||
|
extra_args: ['--insecure-registry', 'my.registry.com']`), "")
|
||||||
|
|
||||||
testValidate(t, []byte("bad_key: {}"), "Additional property bad_key is not allowed")
|
testValidate(t, []byte("bad_key: {}"), "Additional property bad_key is not allowed")
|
||||||
testValidate(t, []byte("rancher: []"), "rancher: Invalid type. Expected: object, given: array")
|
testValidate(t, []byte("rancher: []"), "rancher: Invalid type. Expected: object, given: array")
|
||||||
|
|
||||||
|
@@ -61,6 +61,7 @@
|
|||||||
|
|
||||||
"properties": {
|
"properties": {
|
||||||
"encoding": {"type": "string"},
|
"encoding": {"type": "string"},
|
||||||
|
"container": {"type": "string"},
|
||||||
"content": {"type": "string"},
|
"content": {"type": "string"},
|
||||||
"owner": {"type": "string"},
|
"owner": {"type": "string"},
|
||||||
"path": {"type": "string"},
|
"path": {"type": "string"},
|
||||||
|
Reference in New Issue
Block a user