forked from swaggest/jsonschema-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
324 lines (256 loc) · 7.1 KB
/
helper.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package jsonschema
import (
"encoding/json"
"fmt"
"reflect"
)
const (
// XEnumNames is the name of JSON property to store names of enumerated values.
XEnumNames = "x-enum-names"
)
// NamedEnum returns the enumerated acceptable values with according string names.
type NamedEnum interface {
NamedEnum() ([]interface{}, []string)
}
// Enum returns the enumerated acceptable values.
type Enum interface {
Enum() []interface{}
}
// Preparer alters reflected JSON Schema.
type Preparer interface {
PrepareJSONSchema(schema *Schema) error
}
// Exposer exposes JSON Schema.
type Exposer interface {
JSONSchema() (Schema, error)
}
// RawExposer exposes JSON Schema as JSON bytes.
type RawExposer interface {
JSONSchemaBytes() ([]byte, error)
}
// OneOfExposer exposes "oneOf" items as list of samples.
type OneOfExposer interface {
JSONSchemaOneOf() []interface{}
}
// AnyOfExposer exposes "anyOf" items as list of samples.
type AnyOfExposer interface {
JSONSchemaAnyOf() []interface{}
}
// AllOfExposer exposes "allOf" items as list of samples.
type AllOfExposer interface {
JSONSchemaAllOf() []interface{}
}
// NotExposer exposes "not" schema as a sample.
type NotExposer interface {
JSONSchemaNot() interface{}
}
// IfExposer exposes "if" schema as a sample.
type IfExposer interface {
JSONSchemaIf() interface{}
}
// ThenExposer exposes "then" schema as a sample.
type ThenExposer interface {
JSONSchemaThen() interface{}
}
// ElseExposer exposes "else" schema as a sample.
type ElseExposer interface {
JSONSchemaElse() interface{}
}
// JSONSchema implements Exposer.
func (s Schema) JSONSchema() (Schema, error) {
// Making a deep copy of Schema with JSON round trip to avoid unintentional sharing of pointer data.
j, err := json.Marshal(s)
if err != nil {
return Schema{}, fmt.Errorf("deepcopy marshal: %w", err)
}
var c Schema
if err := json.Unmarshal(j, &c); err != nil {
return Schema{}, fmt.Errorf("deepcopy unmarshal: %w", err)
}
return c, nil
}
// ToSchemaOrBool creates SchemaOrBool instance from Schema.
func (s *Schema) ToSchemaOrBool() SchemaOrBool {
return SchemaOrBool{
TypeObject: s,
}
}
// Type references simple type.
func (i SimpleType) Type() Type {
return Type{SimpleTypes: &i}
}
// ToSchemaOrBool creates SchemaOrBool instance from SimpleType.
func (i SimpleType) ToSchemaOrBool() SchemaOrBool {
return SchemaOrBool{
TypeObject: (&Schema{}).WithType(i.Type()),
}
}
// AddType adds simple type to Schema.
//
// If type is already there it is ignored.
func (s *Schema) AddType(t SimpleType) {
if s.Type == nil {
s.WithType(t.Type())
return
}
if s.Type.SimpleTypes != nil {
if *s.Type.SimpleTypes == t {
return
}
s.Type.SliceOfSimpleTypeValues = []SimpleType{*s.Type.SimpleTypes, t}
s.Type.SimpleTypes = nil
return
}
if len(s.Type.SliceOfSimpleTypeValues) > 0 {
for _, st := range s.Type.SliceOfSimpleTypeValues {
if st == t {
return
}
}
s.Type.SliceOfSimpleTypeValues = append(s.Type.SliceOfSimpleTypeValues, t)
}
}
// IsTrivial is true if schema does not contain validation constraints other than type.
func (s SchemaOrBool) IsTrivial(refResolvers ...func(string) (SchemaOrBool, bool)) bool {
if s.TypeBoolean != nil && !*s.TypeBoolean {
return false
}
if s.TypeObject != nil {
return s.TypeObject.IsTrivial(refResolvers...)
}
return true
}
// IsTrivial is true if schema does not contain validation constraints other than type.
//
// Trivial schema can define trivial items or properties.
// This flag can be used to skip validation of structures that check types during decoding.
func (s Schema) IsTrivial(refResolvers ...func(string) (SchemaOrBool, bool)) bool {
if len(s.AllOf) > 0 || len(s.AnyOf) > 0 || len(s.OneOf) > 0 || s.Not != nil ||
s.If != nil || s.Then != nil || s.Else != nil {
return false
}
if s.Minimum != nil {
if *s.Minimum != 0 || s.ReflectType == nil {
return false
}
//nolint:exhaustive // Allow trivial schema non-negative integers backed by uint*.
switch s.ReflectType.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
break
default:
return false
}
}
if s.MultipleOf != nil || s.Maximum != nil ||
s.ExclusiveMinimum != nil || s.ExclusiveMaximum != nil {
return false
}
if s.MinLength != 0 || s.MaxLength != nil || s.Pattern != nil || s.Format != nil {
return false
}
if s.MinItems != 0 || s.MaxItems != nil || s.UniqueItems != nil || s.Contains != nil {
return false
}
if s.MinProperties != 0 || s.MaxProperties != nil || len(s.Required) > 0 || len(s.PatternProperties) > 0 {
return false
}
if len(s.Dependencies) > 0 || s.PropertyNames != nil || s.Const != nil || len(s.Enum) > 0 {
return false
}
if s.Type != nil && len(s.Type.SliceOfSimpleTypeValues) > 1 && !s.HasType(Null) {
return false
}
if s.Items != nil && (len(s.Items.SchemaArray) > 0 || !s.Items.SchemaOrBool.IsTrivial(refResolvers...)) {
return false
}
if s.AdditionalItems != nil && !s.AdditionalItems.IsTrivial(refResolvers...) {
return false
}
if s.AdditionalProperties != nil && !s.AdditionalProperties.IsTrivial(refResolvers...) {
return false
}
if len(s.Properties) > 0 {
for _, ps := range s.Properties {
if !ps.IsTrivial(refResolvers...) {
return false
}
}
}
if s.Ref == nil {
return true
}
resolved := false
// If same ref is met, it is returned as trivial schema to avoid duplicate recursion.
skipRef := func(ref string) (SchemaOrBool, bool) {
if ref == *s.Ref {
return SchemaOrBool{}, true
}
return SchemaOrBool{}, false
}
rr := append([]func(ref string) (SchemaOrBool, bool){skipRef}, refResolvers...)
for _, resolve := range refResolvers {
if rs, found := resolve(*s.Ref); found {
resolved = true
if !rs.IsTrivial(rr...) {
return false
}
break
}
}
return resolved
}
// HasType checks if Schema has a simple type.
func (s *Schema) HasType(t SimpleType) bool {
if s.Type == nil {
return false
}
if s.Type.SimpleTypes != nil {
return *s.Type.SimpleTypes == t
}
if len(s.Type.SliceOfSimpleTypeValues) > 0 {
for _, st := range s.Type.SliceOfSimpleTypeValues {
if st == t {
return true
}
}
}
return false
}
// JSONSchemaBytes exposes JSON Schema as raw JSON bytes.
func (s SchemaOrBool) JSONSchemaBytes() ([]byte, error) {
return json.Marshal(s)
}
// JSONSchemaBytes exposes JSON Schema as raw JSON bytes.
func (s Schema) JSONSchemaBytes() ([]byte, error) {
return json.Marshal(s)
}
// ToSimpleMap encodes JSON Schema as a map.
func (s SchemaOrBool) ToSimpleMap() (map[string]interface{}, error) {
var m map[string]interface{}
if s.TypeBoolean != nil {
if *s.TypeBoolean {
return map[string]interface{}{}, nil
}
return map[string]interface{}{
"not": map[string]interface{}{},
}, nil
}
b, err := json.Marshal(s.TypeObject)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &m)
if err != nil {
return nil, err
}
return m, nil
}
// FromSimpleMap decodes JSON Schema from a map.
func (s *SchemaOrBool) FromSimpleMap(m map[string]interface{}) error {
j, err := json.Marshal(m)
if err != nil {
return err
}
s.TypeBoolean = nil
return json.Unmarshal(j, s.TypeObjectEns())
}