-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvalidate.go
More file actions
95 lines (78 loc) · 2.8 KB
/
Copy pathvalidate.go
File metadata and controls
95 lines (78 loc) · 2.8 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
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
package overlay
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/speakeasy-api/openapi/internal/sliceutil"
"github.com/speakeasy-api/openapi/internal/version"
)
var (
SupportedVersions = []*version.Version{version.MustParse("1.0.0"), version.MustParse("1.1.0")}
)
// Errors
var (
ErrOverlayVersionInvalid = errors.New("overlay version is invalid")
ErrOverlayVersionNotSupported = fmt.Errorf("overlay version must be one of: `%s`", strings.Join(sliceutil.Map(SupportedVersions, func(v *version.Version) string { return v.String() }), ", "))
ErrOverlayVersionMustBeDefined = errors.New("overlay version must be defined")
ErrOverlayInfoTitleMustBeDefined = errors.New("overlay info title must be defined")
ErrOverlayInfoVersionMustBeDefined = errors.New("overlay info version must be defined")
ErrOverlayExtendsMustBeAValidURL = errors.New("overlay extends must be a valid URL")
ErrOverlayMustDefineAtLeastOneAction = errors.New("overlay must define at least one action")
ErrOverlayActionTargetMustBeDefined = errors.New("overlay action target must be defined")
ErrOverlayActionRemoveAndUpdateCannotBeSet = errors.New("overlay action remove and update cannot be set")
)
type ValidationErrors []error
func (v ValidationErrors) Error() string {
msgs := make([]string, len(v))
for i, err := range v {
msgs[i] = err.Error()
}
return strings.Join(msgs, "\n")
}
func (v ValidationErrors) Return() error {
if len(v) > 0 {
return v
}
return nil
}
func (o *Overlay) ValidateVersion() []error {
errs := make(ValidationErrors, 0)
overlayVersion, err := version.Parse(o.Version)
switch {
case err != nil || overlayVersion == nil:
errs = append(errs, ErrOverlayVersionInvalid)
case !overlayVersion.IsOneOf(SupportedVersions):
errs = append(errs, ErrOverlayVersionNotSupported)
}
return errs
}
func (o *Overlay) Validate() error {
errs := make(ValidationErrors, 0)
errs = append(errs, o.ValidateVersion()...)
if o.Info.Version == "" {
errs = append(errs, errors.New("overlay info version must be defined"))
}
if o.Info.Title == "" {
errs = append(errs, errors.New("overlay info title must be defined"))
}
if o.Extends != "" {
_, err := url.Parse(o.Extends)
if err != nil {
errs = append(errs, errors.New("overlay extends must be a valid URL"))
}
}
if len(o.Actions) == 0 {
errs = append(errs, errors.New("overlay must define at least one action"))
} else {
for i, action := range o.Actions {
if action.Target == "" {
errs = append(errs, fmt.Errorf("overlay action at index %d target must be defined", i))
}
if action.Remove && !action.Update.IsZero() {
errs = append(errs, fmt.Errorf("overlay action at index %d should not both set remove and define update", i))
}
}
}
return errs.Return()
}