Skip to content

Commit

Permalink
add is-array, is-not-array ops
Browse files Browse the repository at this point in the history
  • Loading branch information
lhitchon committed Aug 25, 2018
1 parent 23b23be commit 8e98146
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions assertion/match.go
Expand Up @@ -112,6 +112,16 @@ func isMatch(data interface{}, expression Expression) (MatchResult, error) {
return matches()
}
return doesNotMatch("%v should not be empty", key)
case "is-array":
if isArray(data) {
return matches()
}
return doesNotMatch("%v should be an array", key)
case "is-not-array":
if !isArray(data) {
return matches()
}
return doesNotMatch("%v should not be an array", key)
case "intersect":
if jsonListsIntersect(searchResult, value) {
return matches()
Expand Down
4 changes: 4 additions & 0 deletions assertion/match_test.go
Expand Up @@ -111,6 +111,10 @@ func TestIsMatch(t *testing.T) {
"startsWithFalse": {"FooBar", "starts-with", "Bar", "", false},
"endsWithTrue": {"FooBar", "ends-with", "Bar", "", true},
"endsWithFalse": {"FooBar", "ends-with", "Foo", "", false},
"isArrayTrue": {sliceOfTags, "is-array", "", "", true},
"isArrayFalse": {"Foo", "is-array", "", "", false},
"isNotArrayTrue": {sliceOfTags, "is-not-array", "", "", false},
"isNotArrayFalse": {"Foo", "is-not-array", "", "", true},
}
for k, tc := range testCases {
var m MatchResult
Expand Down
15 changes: 15 additions & 0 deletions assertion/util.go
Expand Up @@ -45,6 +45,21 @@ func isEmpty(data interface{}) bool {
}
}

func isArray(data interface{}) bool {
switch data.(type) {
case nil:
return false
case string:
return false
case []interface{}:
return true
case []map[string]interface{}:
return true
default:
return false
}
}

func listsIntersect(list1 []string, list2 []string) bool {
for _, a := range list1 {
for _, b := range list2 {
Expand Down
38 changes: 38 additions & 0 deletions docs/operations.md
Expand Up @@ -12,7 +12,9 @@
| [every](#every) | Every |
| [has-properties](#has-properties) | Has Properties |
| [in](#in) | In |
| [is-array](#is-array) | Is Array |
| [is-false](#is-false) | Is False |
| [is-not-array](#is-not-array) | Is Not Array |
| [is-true](#is-true) | Is True |
| [ne](#ne) | Not equal |
| [none](#none) | None |
Expand Down Expand Up @@ -389,3 +391,39 @@ Example:
value: Resource
...
```

## is-array

Check that an attribute is an array

Example:

```
...
- id: TAGS_ARRAY
message: Tags should be an array
severity: FAILURE
resource: some_resource
assertions:
- key: Tags[]
op: is-array
...
```

## is-not-array

Check that an attribute is not an array

Example:

```
...
- id: DESCRIPTION_NOT_AN_ARRAY
message: Description should not be an array
severity: FAILURE
resource: some_resource
assertions:
- key: Name
op: is-not-array
...
```

0 comments on commit 8e98146

Please sign in to comment.