-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema.go
195 lines (151 loc) · 3.17 KB
/
schema.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/xeipuuv/gojsonschema"
)
type Schema struct {
Name string
Scope string
Field string
Pattern string
File string
loader gojsonschema.JSONLoader
}
func (s *Schema) Load() error {
p, err := filepath.Abs(s.File)
if err != nil {
return err
}
if _, err := os.Stat(p); err != nil {
return err
}
p = fmt.Sprintf("file://%s", p)
s.loader = gojsonschema.NewReferenceLoader(p)
return nil
}
func (s *Schema) Validate(v map[string]interface{}) (*gojsonschema.Result, error) {
dl := gojsonschema.NewGoLoader(v)
return gojsonschema.Validate(s.loader, dl)
}
type ResultErrors map[string][]gojsonschema.ResultError
func (r ResultErrors) Error() string {
var b bytes.Buffer
for k, errs := range r {
fmt.Fprintln(&b, k)
for _, err := range errs {
fmt.Fprintf(&b, "- %s\n", err)
}
}
return b.String()
}
func (r ResultErrors) MarshalJSON() ([]byte, error) {
if len(r) == 0 {
return nil, nil
}
var m []map[string]interface{}
for k, errs := range r {
var v []map[string]interface{}
for _, err := range errs {
v = append(v, map[string]interface{}{
"type": err.Type(),
"description": err.Description(),
"details": err.Details(),
})
}
m = append(m, map[string]interface{}{
"schema": k,
"errors": v,
})
}
return json.Marshal(m)
}
type Result map[string]*gojsonschema.Result
func (r Result) Matches() []string {
var m []string
for k := range r {
m = append(m, k)
}
sort.Strings(m)
return m
}
func (r Result) Errors() ResultErrors {
m := make(ResultErrors)
for k, v := range r {
errs := v.Errors()
if len(errs) > 0 {
m[k] = errs
}
}
return m
}
func (r Result) Valid() bool {
for _, res := range r {
if !res.Valid() {
return false
}
}
return true
}
// Validate validates and object against a set of schemas.
func Validate(k string, m map[string]interface{}, schemas ...*Schema) (Result, error) {
result := make(Result)
for _, s := range schemas {
switch s.Scope {
// Empty scope matches all documents.
case "":
case "object":
ok, err := compileAndTest(s.Pattern, k)
if err != nil {
return nil, err
}
if !ok {
continue
}
case "value":
v, ok := m[s.Field]
if !ok {
continue
}
// If the pattern is empty, then the presense of the field
// is all that is required.
if s.Pattern != "" {
// Coerce to string for comparison.
// There are certainly edge cases with this approach, such as nil
// but the fields being matched on are expected to be simple.
x := fmt.Sprint(v)
ok, err := compileAndTest(s.Pattern, x)
if err != nil {
return nil, err
}
if !ok {
continue
}
}
// Invalid scope.
default:
return nil, fmt.Errorf("invalid schema scope: %s", s.Scope)
}
r, err := s.Validate(m)
if err != nil {
return nil, err
}
result[s.Name] = r
}
return result, nil
}
func compileAndTest(expr, str string) (bool, error) {
if !strings.HasPrefix(expr, "^") {
expr = "^" + expr
}
if !strings.HasSuffix(expr, "$") {
expr = expr + "$"
}
return regexp.MatchString(expr, str)
}