-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.go
261 lines (233 loc) · 6.43 KB
/
compiler.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package jsonschema
import (
"math/big"
"regexp"
"github.com/go-faster/errors"
)
// compiler parses JSON schemas.
type compiler struct {
doc *document
remote RemoteResolver
remotes map[string]*document
refcache map[string]*Schema
}
// newCompiler creates new compiler.
func newCompiler(root *document) *compiler {
var loc string
if root.id != nil {
r := stripFragment(root.id)
loc = r.String()
}
return &compiler{
doc: root,
remote: Remote{},
remotes: map[string]*document{
"": root,
loc: root,
},
refcache: map[string]*Schema{},
}
}
// Compile compiles given RawSchema and returns compiled Schema.
//
// Do not modify RawSchema fields, Schema will reference them.
func (p *compiler) Compile(schema RawSchema) (*Schema, error) {
return p.compile(schema, newResolveCtx(p.doc.id))
}
func (p *compiler) compile(schema RawSchema, ctx *resolveCtx) (_ *Schema, err error) {
return p.compile1(schema, ctx, func(s *Schema) {})
}
func (p *compiler) compile1(schema RawSchema, ctx *resolveCtx, save func(s *Schema)) (_ *Schema, err error) {
if ref := schema.Ref; ref != "" {
s, err := p.resolve(ref, ctx)
if err != nil {
return nil, errors.Wrapf(err, "resolve %q", ref)
}
return s, nil
}
if id := schema.ID; id != "" {
idURL, err := ctx.parseURL(id)
if err != nil {
return nil, errors.Wrap(err, "parse $id")
}
ctx = ctx.child(idURL)
}
if f := schema.Format; f != "" {
// TODO: support format validation
schema.Format = ""
}
s := &Schema{
types: typeSet(0).set(schema.Type),
format: schema.Format,
enum: schema.Enum,
enumMap: make(map[string]struct{}, len(schema.Enum)),
allOf: nil,
anyOf: nil,
oneOf: nil,
not: nil,
minProperties: parseMinMax(schema.MinProperties),
maxProperties: parseMinMax(schema.MaxProperties),
required: map[string]struct{}{},
properties: map[string]*Schema{},
patternProperties: nil,
additionalProperties: additionalProperties{},
dependentRequired: nil,
dependentSchemas: nil,
minItems: parseMinMax(schema.MinItems),
maxItems: parseMinMax(schema.MaxItems),
uniqueItems: schema.UniqueItems,
items: items{},
additionalItems: additionalItems{},
minimum: nil,
exclusiveMinimum: schema.ExclusiveMinimum,
maximum: nil,
exclusiveMaximum: schema.ExclusiveMaximum,
multipleOf: nil,
minLength: parseMinMax(schema.MinLength),
maxLength: parseMinMax(schema.MaxLength),
pattern: nil,
}
save(s)
for _, value := range schema.Enum {
s.enumMap[string(value)] = struct{}{}
}
for _, field := range schema.Required {
// See https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.4.3.
//
// Elements of this array MUST be strings, and MUST be unique.
if _, ok := s.required[field]; ok {
return nil, errors.Errorf(`"required" list must be unique, duplicate %q`, field)
}
s.required[field] = struct{}{}
}
for _, field := range schema.Properties {
s.properties[field.Name], err = p.compile(field.Schema, ctx)
if err != nil {
return nil, errors.Wrapf(err, "property %q", field.Name)
}
}
for _, field := range schema.PatternProperties {
if err := func() error {
pattern, err := regexp.Compile(field.Pattern)
if err != nil {
return err
}
item, err := p.compile(field.Schema, ctx)
if err != nil {
return err
}
s.patternProperties = append(s.patternProperties, patternProperty{
Regexp: pattern,
Schema: item,
})
return nil
}(); err != nil {
return nil, errors.Wrapf(err, "patternProperty %q", field.Pattern)
}
}
if it := schema.Items; it != nil {
s.items.Set = true
if it.Array {
s.items.Array, err = p.compileMany(it.Schemas, ctx)
} else {
s.items.Object, err = p.compile(it.Schema, ctx)
}
if err != nil {
return nil, errors.Wrap(err, "items")
}
}
if ap := schema.AdditionalProperties; ap != nil {
s.additionalProperties.Set = true
if val := ap.Bool; val != nil {
s.additionalProperties.Bool = *val
} else {
s.additionalProperties.Schema, err = p.compile(ap.Schema, ctx)
if err != nil {
return nil, errors.Wrap(err, "additionalProperties")
}
}
}
{
dep := schema.Dependencies
if len(dep.Schemas) > 0 {
s.dependentSchemas = make(map[string]*Schema, len(dep.Schemas))
for field, schema := range dep.Schemas {
s.dependentSchemas[field], err = p.compile(schema, ctx)
if err != nil {
return nil, errors.Wrapf(err, "dependent schema %q", field)
}
}
}
s.dependentRequired = dep.Required
}
if ai := schema.AdditionalItems; ai != nil {
s.additionalItems.Set = true
if val := ai.Bool; val != nil {
s.additionalItems.Bool = *val
} else {
s.additionalItems.Schema, err = p.compile(ai.Schema, ctx)
if err != nil {
return nil, errors.Wrap(err, "additionalItems")
}
}
}
if pattern := schema.Pattern; len(pattern) > 0 {
s.pattern, err = regexp.Compile(pattern)
if err != nil {
return nil, errors.Wrap(err, "pattern")
}
}
// TODO: how does it affect performance?
for _, many := range []struct {
name string
to *[]*Schema
schemas []RawSchema
}{
{"allOf", &s.allOf, schema.AllOf},
{"anyOf", &s.anyOf, schema.AnyOf},
{"oneOf", &s.oneOf, schema.OneOf},
} {
*many.to, err = p.compileMany(many.schemas, ctx)
if err != nil {
return nil, errors.Wrap(err, many.name)
}
}
if sch := schema.Not; sch != nil {
s.not, err = p.compile(*sch, ctx)
if err != nil {
return nil, errors.Wrap(err, "not")
}
}
for _, v := range []struct {
name string
to **big.Rat
num Num
}{
{"minimum", &s.minimum, schema.Minimum},
{"maximum", &s.maximum, schema.Maximum},
{"multipleOf", &s.multipleOf, schema.MultipleOf},
} {
if len(v.num) == 0 {
// Value is not set.
continue
}
val := new(big.Rat)
// TODO: more efficient way?
if err := val.UnmarshalText(v.num); err != nil {
return nil, errors.Wrap(err, v.name)
}
*v.to = val
}
return s, nil
}
func (p *compiler) compileMany(schemas []RawSchema, ctx *resolveCtx) ([]*Schema, error) {
result := make([]*Schema, 0, len(schemas))
for i, schema := range schemas {
s, err := p.compile(schema, ctx)
if err != nil {
return nil, errors.Wrapf(err, "[%d]", i)
}
result = append(result, s)
}
return result, nil
}