-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
43 lines (37 loc) · 1.01 KB
/
server.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
package openapi
import (
"net/url"
"regexp"
)
// codebeat:disable[TOO_MANY_IVARS]
var tmplVarRegexp = regexp.MustCompile("{[^}]+}")
// Server Object
type Server struct {
URL string
Description string
Variables map[string]*ServerVariable
}
// Validate the values of Server object.
func (server Server) Validate() error {
if err := server.validateRequiredFields(); err != nil {
return err
}
// replace template variable with placeholder to validate the replaced string
// is valid URL or not
serverURL := tmplVarRegexp.ReplaceAllLiteralString(server.URL, "ph")
// use url.Parse because relative URL is allowed
if _, err := url.Parse(serverURL); err != nil {
return ErrFormatInvalid{Target: "server.url", Format: "URL"}
}
validaters := []validater{}
for _, sv := range server.Variables {
validaters = append(validaters, sv)
}
return validateAll(validaters)
}
func (server Server) validateRequiredFields() error {
if server.URL == "" {
return ErrRequired{Target: "server.url"}
}
return nil
}