-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonschema.go
131 lines (117 loc) · 2.58 KB
/
jsonschema.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
package action
import (
"bytes"
"encoding/json"
"github.com/santhosh-tekuri/jsonschema"
"projectforge.dev/projectforge/app/util"
"projectforge.dev/projectforge/assets"
)
var schemata = map[string]*jsonschema.Schema{}
func schemaCheck(pm *PrjAndMods) ([]string, error) {
if len(schemata) == 0 {
err := loadSchemata()
if err != nil {
return nil, err
}
}
ret := &util.StringSlice{}
prj, err := schemaProject(schemata["project"], pm.File)
if err != nil {
return nil, err
}
ret.Push(prj...)
if pm.EArgs != nil {
args, err := schemaConfig(schemata["config"], pm.EArgs.ConfigFile)
if err != nil {
return nil, err
}
ret.Push(args...)
for k, v := range pm.EArgs.EnumFiles {
enum, err := schemaEnum(schemata["enum"], k, v)
if err != nil {
return nil, err
}
ret.Push(enum...)
}
for k, v := range pm.EArgs.ModelFiles {
model, err := schemaModel(schemata["model"], k, v)
if err != nil {
return nil, err
}
ret.Push(model...)
}
}
return ret.Slice, nil
}
func schemaProject(sch *jsonschema.Schema, f json.RawMessage) ([]string, error) {
ret := &util.StringSlice{}
err := sch.Validate(bytes.NewReader(f))
if err != nil {
ret.Pushf("[project.json]: %v", err)
}
return ret.Slice, nil
}
func schemaConfig(sch *jsonschema.Schema, f json.RawMessage) ([]string, error) {
if len(f) == 0 {
return nil, nil
}
ret := &util.StringSlice{}
err := sch.Validate(bytes.NewReader(f))
if err != nil {
ret.Pushf("[config.json]: %v", err)
}
return ret.Slice, nil
}
func schemaEnum(sch *jsonschema.Schema, k string, f json.RawMessage) ([]string, error) {
ret := &util.StringSlice{}
err := sch.Validate(bytes.NewReader(f))
if err != nil {
ret.Pushf("[enum/%s.json]: %v", k, err)
}
return ret.Slice, nil
}
func schemaModel(sch *jsonschema.Schema, k string, f json.RawMessage) ([]string, error) {
ret := &util.StringSlice{}
err := sch.Validate(bytes.NewReader(f))
if err != nil {
ret.Pushf("[model/%s.json]: %v", k, err)
}
return ret.Slice, nil
}
func loadSchemata() error {
c := jsonschema.NewCompiler()
c.Draft = jsonschema.Draft7
x := func(k string) error {
e, err := assets.Embed("schema/" + k + ".schema.json")
if err != nil {
return err
}
err = c.AddResource(k, bytes.NewReader(e.Bytes))
if err != nil {
return err
}
sch, err := c.Compile(k)
if err != nil {
return err
}
schemata[k] = sch
return nil
}
err := x("project")
if err != nil {
return err
}
err = x("config")
if err != nil {
return err
}
err = x("enum")
if err != nil {
return err
}
err = x("model")
if err != nil {
return err
}
return nil
}