Using strict parsing for yaml configs (#1433)

pull/1430/head
Nathan Zender 6 years ago committed by Cyril Tovena
parent 4f2316ff74
commit 282d77b690
  1. 22
      pkg/cfg/cfg_test.go
  2. 2
      pkg/cfg/files.go

@ -41,3 +41,25 @@ tls:
},
}, data)
}
func TestParseWithInvalidYAML(t *testing.T) {
yamlSource := dYAML([]byte(`
servers:
ports: 2000
timeoutz: 60h
tls:
keey: YAML
`))
fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
flagSource := dFlags(fs, []string{"-verbose", "-server.port=21"})
data := Data{}
err := dParse(&data,
dDefaults(fs),
yamlSource,
flagSource,
)
require.Error(t, err)
require.Equal(t, err.Error(), "yaml: unmarshal errors:\n line 2: field servers not found in type cfg.Data\n line 6: field keey not found in type cfg.TLS")
}

@ -55,7 +55,7 @@ func YAML(f *string) Source {
// dYAML returns a YAML source and allows dependency injection
func dYAML(y []byte) Source {
return func(dst interface{}) error {
return yaml.Unmarshal(y, dst)
return yaml.UnmarshalStrict(y, dst)
}
}

Loading…
Cancel
Save