Skip to content

Commit

Permalink
Merge pull request #5 from neolit123/fix-unknown-json-fields
Browse files Browse the repository at this point in the history
UnmarshalStrict: use DisallowUnknownFields
  • Loading branch information
k8s-ci-robot committed Nov 2, 2018
2 parents 9fb50c1 + 8f2b31b commit fd68e98
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yaml.go
Expand Up @@ -39,7 +39,7 @@ func Unmarshal(y []byte, o interface{}, opts ...JSONOpt) error {
// UnmarshalStrict strictly converts YAML to JSON then uses JSON to unmarshal
// into an object, optionally configuring the behavior of the JSON unmarshal.
func UnmarshalStrict(y []byte, o interface{}, opts ...JSONOpt) error {
return yamlUnmarshal(y, o, true, opts...)
return yamlUnmarshal(y, o, true, append(opts, DisallowUnknownFields)...)
}

// yamlUnmarshal unmarshals the given YAML byte stream into the given interface,
Expand Down
9 changes: 9 additions & 0 deletions yaml_test.go
Expand Up @@ -211,6 +211,15 @@ a:
`)
s4 := NamedThing{}
unmarshalStrictFail(t, y, &s4)

// Strict unmarshal should fail for unknown fields
y = []byte(`
name: TestB
id: ID-B
unknown: Some-Value
`)
s5 := NamedThing{}
unmarshalStrictFail(t, y, &s5)
}

func unmarshalStrict(t *testing.T, y []byte, s, e interface{}, opts ...JSONOpt) {
Expand Down

0 comments on commit fd68e98

Please sign in to comment.