-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathvalidator_test.go
More file actions
64 lines (58 loc) · 1.45 KB
/
Copy pathvalidator_test.go
File metadata and controls
64 lines (58 loc) · 1.45 KB
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
54
55
56
57
58
59
60
61
62
63
64
package jsonlogic_test
import (
"fmt"
"io"
"strings"
"testing"
"github.com/stretchr/testify/assert"
jsonlogic "github.com/diegoholiveira/jsonlogic/v3"
)
func TestJSONLogicValidator(t *testing.T) {
jsonlogic.AddOperator("customOperator", func(values, data any) any {
return values
})
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`),
},
"primitive map is a valid rule": {
IsValid: true,
Rule: strings.NewReader(`{"if": [{">=": [{ "var": "amount" }, 10] }, { "var": "amount" }, { "output": true, "result": "too low" } ]}`),
},
"set must be valid": {
IsValid: true,
Rule: strings.NewReader(`{
"map": [
{"var": "objects"},
{"set": [
{"var": ""},
"age",
{"+": [{"var": ".age"}, 2]}
]},
{"customOperator": [1, 2, 3]}
]
}`),
},
}
for name, scenario := range scenarios {
t.Run(fmt.Sprintf("SCENARIO:%s", name), func(t *testing.T) {
assert.Equal(t, scenario.IsValid, jsonlogic.IsValid(scenario.Rule))
})
}
}