MCPcopy Create free account
hub / github.com/daodst/chat / ValidateRule

Function ValidateRule

internal/pushrules/validate.go:10–49  ·  view source on GitHub ↗

ValidateRule checks the rule for errors. These follow from Sytests and the specification.

(kind Kind, rule *Rule)

Source from the content-addressed store, hash-verified

8// ValidateRule checks the rule for errors. These follow from Sytests
9// and the specification.
10func ValidateRule(kind Kind, rule *Rule) []error {
11 var errs []error
12
13 if !validRuleIDRE.MatchString(rule.RuleID) {
14 errs = append(errs, fmt.Errorf("invalid rule ID: %s", rule.RuleID))
15 }
16
17 if len(rule.Actions) == 0 {
18 errs = append(errs, fmt.Errorf("missing actions"))
19 }
20 for _, action := range rule.Actions {
21 errs = append(errs, validateAction(action)...)
22 }
23
24 for _, cond := range rule.Conditions {
25 errs = append(errs, validateCondition(cond)...)
26 }
27
28 switch kind {
29 case OverrideKind, UnderrideKind:
30 // The empty list is allowed, but for JSON-encoding reasons,
31 // it must not be nil.
32 if rule.Conditions == nil {
33 errs = append(errs, fmt.Errorf("missing rule conditions"))
34 }
35
36 case ContentKind:
37 if rule.Pattern == "" {
38 errs = append(errs, fmt.Errorf("missing content rule pattern"))
39 }
40
41 case RoomKind, SenderKind:
42 // Do nothing.
43
44 default:
45 errs = append(errs, fmt.Errorf("invalid rule kind: %s", kind))
46 }
47
48 return errs
49}
50
51// validRuleIDRE is a regexp for valid IDs.
52

Callers 2

Calls 2

validateActionFunction · 0.85
validateConditionFunction · 0.85

Tested by 2