/
check_rule.go
82 lines (66 loc) · 1.82 KB
/
check_rule.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package algoliasearch
func checkRule(rule Rule) error {
return checkRules([]Rule{rule})
}
func checkRules(rules []Rule) error {
for _, rule := range rules {
consequenceParams := rule.Consequence.Params
if rule.ObjectID == "" {
return emptyField("Rule.ObjectID")
}
// The elements of the RuleConsequence's Params map are first checked
// as query parameters (as any regular query parameters is considered
// valid) at the exception of the "query" field, if any, which is
// ignored. This special case is mandatory as `query` is expected to be
// a string in the context of a regular search query but it may also be
// a `QueryIncrementalEdit` in the context of a RuleConsequence Params.
if err := checkQuery(consequenceParams, "query"); err != nil {
return err
}
// Now that that RuleConsequence's Params map elements have been
// type-checked as regular search parameters, they are now checked as
// RuleConsequence parameters.
for k, v := range consequenceParams {
switch k {
case "query":
switch v.(type) {
case string, QueryIncrementalEdit:
// OK
default:
return invalidType(k, "string or QueryIncrementalEdit")
}
case "automaticFacetFilters",
"automaticOptionalFacetFilters":
if _, ok := v.([]string); !ok {
return invalidType(k, "[]string")
}
default:
// OK
}
}
}
return nil
}
func checkSearchRulesParams(params Map) error {
for k, v := range params {
switch k {
case "query",
"context":
if _, ok := v.(string); !ok {
return invalidType(k, "string")
}
case "anchoring":
if _, ok := v.(RulePatternAnchoring); !ok {
return invalidType(k, "RulePatternAnchoring")
}
case "page",
"hitsPerPage":
if _, ok := v.(int); !ok {
return invalidType(k, "int")
}
default:
// OK
}
}
return nil
}