Skip to content

Commit 9ed1bc6

Browse files
debovemaDEBOVE Mathieu
authored andcommitted
Generate JSON schema of Flogo app descriptor (flogo.json)
1 parent f762374 commit 9ed1bc6

8 files changed

Lines changed: 890 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ _testmain.go
4444
.vscode/
4545
tags
4646
.vscode/symbols.json
47+
.idea
4748
.build-cache
4849
submodules/flogo-cicd/.build-cache
4950
./Dockerfile

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/project-flogo/core
22

33
require (
4+
github.com/square-it/jsonschema v1.9.1 // indirect
45
github.com/stretchr/testify v1.2.2
56
go.uber.org/atomic v1.3.2 // indirect
67
go.uber.org/multierr v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
1010
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1212
github.com/project-flogo/core v0.9.0-alpha.3/go.mod h1:BHeB55AxPhvlNGd+it50rE977ag6xE3bD2RluSDeKBA=
13+
github.com/square-it/jsonschema v1.9.1 h1:0pYdNW+bvukTIBqcfal5XXHikgp/AbADeqcP7I0uV4M=
14+
github.com/square-it/jsonschema v1.9.1/go.mod h1:80WJHSuy3YnokzfFopfx+MAt5lVVnVpS6w2Avv+svHk=
1315
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1416
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
1517
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

internal/schema/assets.go

Lines changed: 235 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// +build ignore
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"os"
10+
11+
"github.com/square-it/jsonschema"
12+
"github.com/project-flogo/core/app"
13+
)
14+
15+
func main() {
16+
reflector := &jsonschema.Reflector{ExpandedStruct: true}
17+
schema := reflector.Reflect(&app.Config{})
18+
schemaJSON, err := json.MarshalIndent(schema, "", " ")
19+
if err != nil {
20+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
21+
os.Exit(1)
22+
}
23+
err = ioutil.WriteFile("schema.json", schemaJSON, 0644)
24+
if err != nil {
25+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
26+
os.Exit(1)
27+
}
28+
}

internal/schema/schema.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//go:generate go run generate/schema_generator.go
2+
//go:generate go-bindata -pkg schema -o assets.go schema.json
3+
4+
package schema
5+
6+
import (
7+
"bytes"
8+
"errors"
9+
"fmt"
10+
11+
"github.com/xeipuuv/gojsonschema"
12+
)
13+
14+
var schema *gojsonschema.Schema
15+
16+
func init() {
17+
jsonSchema, err := Asset("schema.json")
18+
if err != nil {
19+
panic(err)
20+
}
21+
schemaLoader := gojsonschema.NewStringLoader(string(jsonSchema))
22+
schema, err = gojsonschema.NewSchema(schemaLoader)
23+
if err != nil {
24+
panic(err)
25+
}
26+
}
27+
28+
// Validate validates the provided JSON against the v2 JSON schema.
29+
func Validate(JSON []byte) error {
30+
JSONLoader := gojsonschema.NewStringLoader(string(JSON))
31+
result, err := schema.Validate(JSONLoader)
32+
33+
if err != nil {
34+
return err
35+
}
36+
37+
if result.Valid() {
38+
return err
39+
}
40+
var msg bytes.Buffer
41+
42+
msg.WriteString("The JSON is not valid. See errors:\n")
43+
for _, desc := range result.Errors() {
44+
msg.WriteString(fmt.Sprintf("- %s\n", desc))
45+
}
46+
return errors.New(msg.String())
47+
}

0 commit comments

Comments
 (0)