-
Notifications
You must be signed in to change notification settings - Fork 54
/
util.go
49 lines (44 loc) · 1.2 KB
/
util.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
package jsonschema
import (
"fmt"
"net/url"
"os"
"strings"
)
var showDebug = os.Getenv("JSON_SCHEMA_DEBUG") == "1"
// schemaDebug provides a logging interface
// which is off by defauly but can be activated
// for debuging purposes
func schemaDebug(message string, args ...interface{}) {
if showDebug {
if message[len(message)-1] != '\n' {
message += "\n"
}
fmt.Printf(message, args...)
}
}
// SafeResolveURL resolves a string url against the current context url
func SafeResolveURL(ctxURL, resURL string) (string, error) {
cu, err := url.Parse(ctxURL)
if err != nil {
return "", err
}
u, err := url.Parse(resURL)
if err != nil {
return "", err
}
resolvedURL := cu.ResolveReference(u)
if resolvedURL.Scheme == "file" && cu.Scheme != "file" {
return "", fmt.Errorf("cannot access file resources from network context")
}
resolvedURLString := resolvedURL.String()
return resolvedURLString, nil
}
// IsLocalSchemaID validates if a given id is a local id
func IsLocalSchemaID(id string) bool {
splitID := strings.Split(id, "#")
if len(splitID) > 1 && len(splitID[0]) > 0 && splitID[0][0] != '#' {
return false
}
return id != "#" && !strings.HasPrefix(id, "#/") && strings.Contains(id, "#")
}