-
Notifications
You must be signed in to change notification settings - Fork 52
/
validator_test.go
53 lines (49 loc) · 1.06 KB
/
validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package jsonlogic
import (
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJSONLogicValidator(t *testing.T) {
scenarios := map[string]struct {
IsValid bool
Rule io.Reader
}{
"invalid rule": {
IsValid: false,
Rule: strings.NewReader(`{"a", "b"}`),
},
"invalid operator": {
IsValid: false,
Rule: strings.NewReader(`{"filt":[[10, 1, 100], {">=":[{"var":""},2]}]}`),
},
"invalid condition inside a filter": {
IsValid: false,
Rule: strings.NewReader(`{"filter":[{"var":"integers"}, {"=": [{"var":""}, [10]]}]}`),
},
"primitive is a valid rule": {
IsValid: true,
Rule: strings.NewReader(`10`),
},
"set must be valid": {
IsValid: true,
Rule: strings.NewReader(`{
"map": [
{"var": "objects"},
{"set": [
{"var": ""},
"age",
{"+": [{"var": ".age"}, 2]}
]}
]
}`),
},
}
for name, scenario := range scenarios {
t.Run(fmt.Sprintf("SCENARIO:%s", name), func(t *testing.T) {
assert.Equal(t, scenario.IsValid, IsValid(scenario.Rule))
})
}
}