-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_schema.go
More file actions
131 lines (118 loc) · 3.29 KB
/
json_schema.go
File metadata and controls
131 lines (118 loc) · 3.29 KB
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
package main
import (
"encoding/json"
"github.com/xeipuuv/gojsonpointer"
"reflect"
"strings"
)
type JsonSchema struct {
Schema string `json:"$schema"`
Type JsonSchemaType `json:"type"`
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Default interface{} `json:"default"`
Enum []interface{} `json:"enum"`
Definitions map[string]JsonSchema `json:"definitions"`
Ref string `json:"$ref"`
// string
MinLength json.Number `json:"minLength"`
MaxLength json.Number `json:"maxLength"`
Pattern string `json:"pattern"`
Format string `json:"format"`
// numeric
MultipleOf json.Number `json:"multipleOf"`
Minimum json.Number `json:"minimum"`
Maximum json.Number `json:"maximum"`
ExclusiveMaximum bool `json:"exclusiveMaximum"`
// object
Properties map[string]JsonSchema `json:"properties"`
AdditionalProperties interface{} `json:"additionalProperties"`
Required []string `json:"required"`
MinProperties json.Number `json:"minProperties"`
MaxProperties json.Number `json:"maxProperties"`
Dependencies map[string]JsonSchema `json:"dependencies"`
// array
Items interface{} `json:"items"`
AdditionalItems bool `json:"additionalItems"`
MinItems json.Number `json:"minItems"`
MaxItems json.Number `json:"maxItems"`
UniqueItems bool `json:"uniqueItems"`
// combining
AllOf []JsonSchema `json:"allOf"`
AnyOf []JsonSchema `json:"anyOf"`
OneOf []JsonSchema `json:"oneOf"`
Not []JsonSchema `json:"not"`
}
func (schema JsonSchema) GetItemList() []JsonSchema {
if schema.Items == nil {
return nil
}
// schema.items defined type of Object or array
j, _ := json.Marshal(schema.Items)
var res []JsonSchema
switch reflect.ValueOf(schema.Items).Kind() {
case reflect.Array, reflect.Slice:
json.Unmarshal(j, &res)
case reflect.Map:
var s JsonSchema
json.Unmarshal(j, &s)
res = []JsonSchema{s}
}
return res
}
func (schema JsonSchema) IsRequired(key string) bool {
if schema.Required == nil {
return false
}
for _, r := range schema.Required {
if key == r {
return true
}
}
return false
}
func (schema JsonSchema) GetRefSchema(pointer *gojsonpointer.JsonPointer) JsonSchema {
path := strings.Split(pointer.String(), "/")
s := schema
i := 0
for i < len(path) {
p := path[i]
if p == "definitions" {
i++
s = s.Definitions[path[i]]
}
i++
}
return s
}
func (schema JsonSchema) GetRefList() []string {
res := []string{}
switch schema.Type {
case JsonSchemaTypeObject:
for _, s := range schema.Properties {
res = append(res, s.GetRefList()...)
}
case JsonSchemaTypeArray:
for _, s := range schema.GetItemList() {
res = append(res, s.GetRefList()...)
}
default:
if schema.Ref != "" {
res = append(res, schema.Ref)
}
}
return res
}
func (schema JsonSchema) HasReference() bool {
return schema.Ref != ""
}
func (schema JsonSchema) HasStructure() bool {
switch schema.Type {
case JsonSchemaTypeObject:
return true
case JsonSchemaTypeArray:
return schema.GetItemList()[0].HasStructure()
}
return false
}