-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsourcedescription.go
106 lines (89 loc) · 3.5 KB
/
sourcedescription.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
package arazzo
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/speakeasy-api/openapi/arazzo/core"
"github.com/speakeasy-api/openapi/extensions"
"github.com/speakeasy-api/openapi/validation"
)
// SourceDescriptions represents a list of SourceDescription objects that describe the source of the data that the workflow is orchestrating.
type SourceDescriptions []*SourceDescription
// Find will return the first SourceDescription object with the provided name.
func (s SourceDescriptions) Find(name string) *SourceDescription {
for _, sourceDescription := range s {
if sourceDescription.Name == name {
return sourceDescription
}
}
return nil
}
// SourceDescriptionType represents the type of the SourceDescription object.
type SourceDescriptionType string
const (
// SourceDescriptionTypeOpenAPI represents a SourceDescription that describes an OpenAPI document.
SourceDescriptionTypeOpenAPI = "openapi"
// SourceDescriptionTypeArazzo represents a SourceDescription that describes an Arazzo document.
SourceDescriptionTypeArazzo = "arazzo"
)
// SourceDescription represents an Arazzo or OpenAPI document that is referenced by this Arazzo document.
type SourceDescription struct {
// Name is the case-sensitive name of the SourceDescription object used to reference it.
Name string
// URL is a URL or relative URI to the location of the referenced document.
URL string
// Type is the type of the referenced document.
Type SourceDescriptionType
// Extensions provides a list of extensions to the SourceDescription object.
Extensions *extensions.Extensions
// Valid indicates whether this model passed validation.
Valid bool
core core.SourceDescription
}
var _ model[core.SourceDescription] = (*SourceDescription)(nil)
// GetCore will return the low level representation of the source description object.
// Useful for accessing line and column numbers for various nodes in the backing yaml/json document.
func (s *SourceDescription) GetCore() *core.SourceDescription {
return &s.core
}
// Validate will validate the source description object against the Arazzo specification.
func (s *SourceDescription) Validate(ctx context.Context, opts ...validation.Option) []error {
errs := []error{}
if s.core.Name.Present && s.Name == "" {
errs = append(errs, &validation.Error{
Message: "name is required",
Line: s.core.Name.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.Name.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if s.core.URL.Present && s.URL == "" {
errs = append(errs, &validation.Error{
Message: "url is required",
Line: s.core.URL.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.URL.GetValueNodeOrRoot(s.core.RootNode).Column,
})
} else if s.core.URL.Present {
if _, err := url.Parse(s.URL); err != nil {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("url is not a valid url/uri according to RFC 3986: %s", err),
Line: s.core.URL.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.URL.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
}
switch s.Type {
case SourceDescriptionTypeOpenAPI:
case SourceDescriptionTypeArazzo:
default:
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("type must be one of [%s]", strings.Join([]string{SourceDescriptionTypeOpenAPI, SourceDescriptionTypeArazzo}, ", ")),
Line: s.core.Type.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.Type.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if len(errs) == 0 {
s.Valid = true
}
return errs
}