-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.go
41 lines (33 loc) · 1.12 KB
/
validate.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
package ambient
import (
"fmt"
"regexp"
"golang.org/x/mod/semver"
)
var (
// DisallowedPluginNames is a list of disallowed plugin names.
disallowedPluginNames = map[string]bool{
"amb": false,
"ambient": false,
"plugin": false,
}
// Only allow starting with a lowercase letter and then containing all
// lowercase letters and numbers.
rePluginName = regexp.MustCompile("^[a-z][a-z0-9]*$")
)
// Validate returns an error if the plugin name or version is not valid.
func Validate(p PluginCore) error {
// Don't allow certain plugin names.
if allowed, ok := disallowedPluginNames[p.PluginName()]; ok && !allowed {
return fmt.Errorf("plugin name not allowed: %v", p.PluginName())
}
// Don't allow certain plugin name characters.
if ok := rePluginName.Match([]byte(p.PluginName())); !ok {
return fmt.Errorf("plugin name format not allowed: '%v'", p.PluginName())
}
// Ensure version meets https://semver.org/ requirements.
if ok := semver.IsValid(fmt.Sprintf("v%v", p.PluginVersion())); !ok {
return fmt.Errorf("plugin (%v) version not in semver format: %v", p.PluginName(), p.PluginVersion())
}
return nil
}