-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
109 lines (90 loc) · 2.63 KB
/
validator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package schema
import (
"errors"
"fmt"
"slices"
"github.com/ubavic/mint/parser"
)
var ErrCommandNotAllowed = errors.New("command not allowed")
var ErrCommandNotFound = errors.New("command not found")
var ErrCommandInvalidArguments = errors.New("command has invalid arguments")
var ErrGroupNotFound = errors.New("group not found")
var ErrTargetNotFound = errors.New("target not found")
func (s Schema) Validate(document parser.Element) error {
return s.validate(document, nil)
}
func (s Schema) validate(document parser.Element, parent *string) error {
var parentAllowedCommands []string
var err error
if parent == nil {
if s.Source.AllowedRootCommands != "" {
parentAllowedCommands, err = s.GetGroupCommands(s.Source.AllowedRootCommands)
if err != nil {
return fmt.Errorf("%w: parent allowed commands", err)
}
}
} else {
command, err := s.GetCommand(*parent)
if err != nil {
return fmt.Errorf("%w: command %s", err, command.Command)
}
parentAllowedCommands, err = s.GetGroupCommands(command.Command)
if err != nil {
return fmt.Errorf("%w: command %s", err, command.Command)
}
}
if parentAllowedCommands == nil {
return nil
}
for _, el := range document.Content() {
if command, ok := el.(*parser.Command); ok {
if !slices.Contains(parentAllowedCommands, command.Name) {
return fmt.Errorf("%w: command %s is not in list %v", ErrCommandNotFound, command.Name, parentAllowedCommands)
}
}
}
return nil
}
func (s Schema) ValidateSingleCommand(name string, args int) error {
for _, command := range s.Source.Commands {
if command.Command == name {
if command.Arguments == args {
return nil
} else {
return fmt.Errorf("%w: command %s requires %d arguments, but %d is given", ErrCommandInvalidArguments, name, command.Arguments, args)
}
}
}
return fmt.Errorf("%w: command %s is not found in the schema", ErrCommandNotFound, name)
}
func (s *Schema) GetCommand(commandName string) (*Command, error) {
for _, command := range s.Source.Commands {
if command.Command == commandName {
return &command, nil
}
}
return nil, ErrCommandNotFound
}
func (s *Schema) GetGroupCommands(groupName string) ([]string, error) {
for _, group := range s.Source.Groups {
if group.Name == groupName {
return group.Commands, nil
}
}
return nil, ErrGroupNotFound
}
func (s *Schema) GetTarget(targetName string) (*Target, error) {
if targetName == "" {
if len(s.Targets) == 0 {
return nil, ErrTargetNotFound
} else {
return &s.Targets[0], nil
}
}
for _, target := range s.Targets {
if target.Name == targetName {
return &target, nil
}
}
return nil, ErrTargetNotFound
}