Skip to content

Commit

Permalink
Using strict parsing for yaml configs (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
zendern authored and cyriltovena committed Jan 6, 2020
1 parent 4f2316f commit 282d77b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions pkg/cfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion pkg/cfg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 282d77b

Please sign in to comment.