-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
513 lines (471 loc) · 13.6 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package jsonschema
import (
"embed"
"encoding/json"
"fmt"
"net/url"
"path"
"strings"
"github.com/gobuffalo/flect"
"github.com/twelvelabs/termite/render"
"gopkg.in/yaml.v3"
)
//go:embed templates/*
var Templates embed.FS
// Schema represents a JSON schema document.
type Schema struct {
AdditionalItems *Schema `json:"additionalItems,omitempty"`
AdditionalProperties any `json:"additionalProperties,omitempty"`
AllOf []*Schema `json:"allOf,omitempty"`
AnyOf []*Schema `json:"anyOf,omitempty"`
Comment string `json:"$comment,omitempty"`
Const Any `json:"const,omitempty"`
Contains *Schema `json:"contains,omitempty"`
ContentEncoding string `json:"contentEncoding,omitempty"`
ContentMediaType string `json:"contentMediaType,omitempty"`
Default Any `json:"default,omitempty"`
Definitions map[string]*Schema `json:"definitions,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Description string `json:"description,omitempty"`
Else *Schema `json:"else,omitempty"`
Enum []Any `json:"enum,omitempty"`
EnumDescriptions []string `json:"enumDescriptions,omitempty"`
Examples []Any `json:"examples,omitempty"`
ExclusiveMaximum float64 `json:"exclusiveMaximum,omitempty"`
ExclusiveMinimum float64 `json:"exclusiveMinimum,omitempty"`
Format string `json:"format,omitempty"`
ID string `json:"$id,omitempty"`
If *Schema `json:"if,omitempty"`
Items *Schema `json:"items,omitempty"`
MarkdownDescription string `json:"markdownDescription,omitempty"`
MaxContains int `json:"maxContains,omitempty"`
Maximum float64 `json:"maximum,omitempty"`
MaxItems int `json:"maxItems,omitempty"`
MaxLength int `json:"maxLength,omitempty"`
MaxProperties int `json:"maxProperties,omitempty"`
MinContains int `json:"minContains,omitempty"`
Minimum float64 `json:"minimum,omitempty"`
MinItems int `json:"minItems,omitempty"`
MinLength int `json:"minLength,omitempty"`
MinProperties int `json:"minProperties,omitempty"`
MultipleOf float64 `json:"multipleOf,omitempty"`
Not []*Schema `json:"not,omitempty"`
OneOf []*Schema `json:"oneOf,omitempty"`
Pattern string `json:"pattern,omitempty"`
PatternProperties map[string]*Schema `json:"patternProperties,omitempty"`
PrefixItems []*Schema `json:"prefixItems,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
PropertyNames *Schema `json:"propertyNames,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Ref string `json:"$ref,omitempty"`
Required []string `json:"required,omitempty"`
Schema string `json:"$schema,omitempty"`
Then *Schema `json:"then,omitempty"`
Title string `json:"title,omitempty"`
Types Types `json:"type,omitempty"`
UnevaluatedProperties bool `json:"unevaluatedProperties,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
WriteOnly bool `json:"writeOnly,omitempty"`
/**
* Internal, non-schema fields.
**/
Context *Context `json:"-"`
Document any `json:"-"`
GenPathTpl render.Template `json:"-"`
Key string `json:"key,omitempty"`
Parent *Schema `json:"-"`
Resolved bool `json:"resolved,omitempty"`
RetrievalURI string `json:"retrievalURI,omitempty"`
}
// BaseURI returns the resolved base URI for the schema.
// The base URI is the schema $id attribute resolved against
// the retrieval URI.
func (s *Schema) BaseURI() *url.URL {
retURI, err := url.Parse(s.Root().RetrievalURI)
if err != nil {
panic(err)
}
if s.ID == "" {
// If the $id attribute is missing, then the base URI is assumed
// to be the same as the retrieval URI.
// See: https://json-schema.org/understanding-json-schema/structuring.html#retrieval-uri
return retURI
}
baseURI, err := url.Parse(s.Root().ID)
if err != nil {
panic(err)
}
// The $id attribute MAY be relative to the retrieval URI.
// See: https://json-schema.org/understanding-json-schema/structuring.html#id
return retURI.ResolveReference(baseURI)
}
// RefURI resolves the given ref against the base URI.
func (s *Schema) RefURI(ref string) *url.URL {
refURI, err := url.Parse(ref)
if err != nil {
panic(err)
}
return s.BaseURI().ResolveReference(refURI)
}
// Clone returns a deep-copied clone of the receiver.
func (s *Schema) Clone() (*Schema, error) {
doc, err := json.Marshal(s)
if err != nil {
return nil, err
}
return s.Context.parse(doc, s.Parent)
}
// DescriptionMarkdown returns the schema description formatted as Markdown,
// Will prioritize the non-standard `markdownDescription` attribute if present,
// otherwise uses `description`.
func (s *Schema) DescriptionMarkdown() string {
if s.MarkdownDescription != "" {
return s.MarkdownDescription
}
return s.Description
}
func (s *Schema) EntityName() string {
if s.Title != "" {
return flect.Pascalize(s.Title)
}
if s.Key != "" {
return flect.Pascalize(s.Key)
}
if s.Ref != "" {
return flect.Pascalize(path.Base(s.Ref))
}
return ""
}
func (s *Schema) EntityLink() string {
anchor := strings.ToLower(s.EntityName())
anchor = strings.ReplaceAll(anchor, " ", "-")
return s.GenPath() + "#" + anchor
}
func (s *Schema) GenPath() string {
path, err := s.Root().GenPathTpl.Render(s)
if err != nil {
panic(err)
}
return path
}
func (s *Schema) EnsureDocument() {
if s.Document == nil {
buf, err := json.Marshal(s)
if err != nil {
panic(err)
}
err = json.Unmarshal(buf, &s.Document)
if err != nil {
panic(err)
}
}
}
// EnumMarkdown returns the enum and enum descriptions
// formatted as a markdown list.
func (s *Schema) EnumMarkdown() string {
items := []string{}
for idx, enum := range s.Enum {
desc := ""
if idx < len(s.EnumDescriptions) {
desc = s.EnumDescriptions[idx]
}
item := "- `" + enum.String() + "`"
if desc != "" {
item += ": " + desc
}
items = append(items, item)
}
if len(items) == 0 {
return ""
}
return strings.Join(items, "\n")
}
// ExamplesMarkdown returns the examples as a markdown list.
func (s *Schema) ExamplesMarkdown() string {
items := []string{}
for _, example := range s.Examples {
items = append(items, fmt.Sprintf(" * `%s`", example.YAMLString()))
}
if len(items) == 0 {
return ""
}
return strings.Join(items, "\n")
}
// Merge merges fields from other into the receiver.
func (s *Schema) Merge(other *Schema) {
s.EnsureDocument()
other.EnsureDocument()
doc, ok := other.Document.(map[string]any)
if !ok {
// Empty or invalid doc, nothing to do.
return
}
for key := range doc {
switch key {
case "additionalItems":
s.AdditionalItems = other.AdditionalItems
case "additionalProperties":
s.AdditionalProperties = other.AdditionalProperties
case "allOf":
s.AllOf = other.AllOf
case "anyOf":
s.AnyOf = other.AnyOf
case "$comment":
s.Comment = other.Comment
case "const":
s.Const = other.Const
case "contains":
s.Contains = other.Contains
case "contentEncoding":
s.ContentEncoding = other.ContentEncoding
case "contentMediaType":
s.ContentMediaType = other.ContentMediaType
case "default":
s.Default = other.Default
case "definitions":
s.Definitions = other.Definitions
case "deprecated":
s.Deprecated = other.Deprecated
case "description":
s.Description = other.Description
case "else":
s.Else = other.Else
case "enum":
s.Enum = other.Enum
case "enumDescriptions":
s.EnumDescriptions = other.EnumDescriptions
case "examples":
s.Examples = other.Examples
case "exclusiveMaximum":
s.ExclusiveMaximum = other.ExclusiveMaximum
case "exclusiveMinimum":
s.ExclusiveMinimum = other.ExclusiveMinimum
case "format":
s.Format = other.Format
case "$id":
s.ID = other.ID
case "if":
s.If = other.If
case "items":
s.Items = other.Items
case "markdownDescription":
s.MarkdownDescription = other.MarkdownDescription
case "maxContains":
s.MaxContains = other.MaxContains
case "maximum":
s.Maximum = other.Maximum
case "maxItems":
s.MaxItems = other.MaxItems
case "maxLength":
s.MaxLength = other.MaxLength
case "maxProperties":
s.MaxProperties = other.MaxProperties
case "minContains":
s.MinContains = other.MinContains
case "minimum":
s.Minimum = other.Minimum
case "minItems":
s.MinItems = other.MinItems
case "minLength":
s.MinLength = other.MinLength
case "minProperties":
s.MinProperties = other.MinProperties
case "multipleOf":
s.MultipleOf = other.MultipleOf
case "not":
s.Not = other.Not
case "oneOf":
s.OneOf = other.OneOf
case "pattern":
s.Pattern = other.Pattern
case "patternProperties":
s.PatternProperties = other.PatternProperties
case "prefixItems":
s.PrefixItems = other.PrefixItems
case "properties":
s.Properties = other.Properties
case "propertyNames":
s.PropertyNames = other.PropertyNames
case "readOnly":
s.ReadOnly = other.ReadOnly
case "$ref":
s.Ref = other.Ref
case "required":
s.Required = other.Required
case "$schema":
s.Schema = other.Schema
case "then":
s.Then = other.Then
case "title":
s.Title = other.Title
case "type":
s.Types = other.Types
case "unevaluatedProperties":
s.UnevaluatedProperties = other.UnevaluatedProperties
case "uniqueItems":
s.UniqueItems = other.UniqueItems
case "writeOnly":
s.WriteOnly = other.WriteOnly
default:
}
}
}
// RequiredKey returns true when key is a required property.
func (s *Schema) RequiredKey(key string) bool {
for _, k := range s.Required {
if k == key {
return true
}
}
return false
}
// Root returns the root schema.
func (s *Schema) Root() *Schema {
if s.Parent != nil {
return s.Parent.Root()
}
return s
}
func (s *Schema) TypeInfo() []TypeInfo {
result := []TypeInfo{}
for _, t := range s.Types {
switch t {
case TypeArray:
result = append(result, TypeInfo{
Type: t,
Schema: s.Items,
})
case TypeObject:
result = append(result, TypeInfo{
Type: t,
Schema: s,
})
default:
result = append(result, TypeInfo{
Type: t,
Schema: nil,
})
}
}
return result
}
func (s *Schema) TypeInfoMarkdown() string {
segments := []string{}
for _, ti := range s.TypeInfo() {
segments = append(segments, ti.Markdown())
}
return strings.Join(segments, " | ")
}
type Type string
const (
TypeArray Type = "array"
TypeBoolean Type = "boolean"
TypeInteger Type = "integer"
TypeNull Type = "null"
TypeNumber Type = "number"
TypeObject Type = "object"
TypeString Type = "string"
)
type Types []Type
// Contains returns true if the receiver contains val.
func (ts *Types) Contains(val Type) bool {
for _, t := range *ts {
if t == val {
return true
}
}
return false
}
func (ts *Types) UnmarshalJSON(data []byte) error {
var value any
if err := json.Unmarshal(data, &value); err != nil {
return err
}
// JSON Schema supports `type` being either a string or an array
// of strings, so we need to normalize into a slice.
switch val := value.(type) {
case string:
*ts = []Type{Type(val)}
return nil
case []any:
var types []Type
for _, t := range val {
s, ok := t.(string)
if !ok {
return fmt.Errorf("unsupported type: %T for %v", t, t)
}
types = append(types, Type(s))
}
*ts = types
default:
return fmt.Errorf("unsupported type: %T for %v", val, val)
}
return nil
}
type TypeInfo struct {
Type Type
Schema *Schema
}
func (ti TypeInfo) Markdown() string {
if ti.Schema == nil {
return string(ti.Type)
}
switch ti.Type {
case TypeArray:
switch len(ti.Schema.Types) {
case 0:
// array
return string(ti.Type)
case 1:
// string[]
// Widget[]
// etc...
return fmt.Sprintf("%s[]", ti.Schema.TypeInfoMarkdown())
default:
// (string | number | Widget)[]
return fmt.Sprintf("(%s)[]", ti.Schema.TypeInfoMarkdown())
}
case TypeObject:
if ti.Schema.EntityName() == "" {
return string(ti.Type)
}
return fmt.Sprintf("[%s](%s)", ti.Schema.EntityName(), ti.Schema.EntityLink())
default:
return string(ti.Type)
}
}
type Any struct {
value any
}
func (a Any) MarshalJSON() ([]byte, error) {
return json.Marshal(a.value)
}
func (a *Any) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &a.value)
}
func (a Any) String() string {
if a.value == nil {
return ""
}
return fmt.Sprintf("%v", a.value)
}
func (a Any) JSONString() string {
if a.value == nil {
return ""
}
serialized, err := json.Marshal(a.value)
if err != nil {
return err.Error()
}
return strings.TrimSpace(string(serialized))
}
func (a Any) YAMLString() string {
if a.value == nil {
return ""
}
serialized, err := yaml.Marshal(a.value)
if err != nil {
return err.Error()
}
return strings.TrimSpace(string(serialized))
}