Skip to content

Commit

Permalink
condition: throw an error if an unknown condition is used in json (#68)
Browse files Browse the repository at this point in the history
Closes #45
  • Loading branch information
arekkas authored Jun 6, 2017
1 parent 208aa53 commit c8b0a33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ func (cs Conditions) UnmarshalJSON(data []byte) error {
}

for k, jc := range jcs {
var found bool
for name, c := range ConditionFactories {
if name == jc.Type {
found = true
dc = c()

if len(jc.Options) == 0 {
Expand All @@ -72,6 +74,10 @@ func (cs Conditions) UnmarshalJSON(data []byte) error {
break
}
}

if !found {
return errors.Errorf("Could not find condition type %s", jc.Type)
}
}

return nil
Expand Down
9 changes: 9 additions & 0 deletions condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ func TestMarshalUnmarshal(t *testing.T) {
assert.IsType(t, &EqualsSubjectCondition{}, cs["owner"])
assert.IsType(t, &CIDRCondition{}, cs["clientIP"])
}

func TestUnmarshalFails(t *testing.T) {
cs := Conditions{}
require.NotNil(t, json.Unmarshal([]byte(`{
"somefield": {
"type": "DoesntExist"
}
}`), &cs))
}

0 comments on commit c8b0a33

Please sign in to comment.