Skip to content

Commit

Permalink
Generate JSON schema of Flogo app descriptor (flogo.json)
Browse files Browse the repository at this point in the history
  • Loading branch information
debovema committed Jan 18, 2019
1 parent 7245be4 commit c2ec644
Show file tree
Hide file tree
Showing 6 changed files with 607 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _testmain.go

tags
.vscode/symbols.json
.idea
.build-cache
submodules/flogo-cicd/.build-cache
./Dockerfile
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/square-it/jsonschema v1.9.1 h1:0pYdNW+bvukTIBqcfal5XXHikgp/AbADeqcP7I0uV4M=
github.com/square-it/jsonschema v1.9.1/go.mod h1:80WJHSuy3YnokzfFopfx+MAt5lVVnVpS6w2Avv+svHk=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.1.0 h1:ngVtJC9TY/lg0AA/1k48FYhBrhRoFlEmWzsehpNAaZg=
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
Expand Down
235 changes: 235 additions & 0 deletions internal/schema/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions internal/schema/generate/schema_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build ignore

package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/square-it/jsonschema"
"github.com/project-flogo/core/app"
)

func main() {
reflector := &jsonschema.Reflector{ExpandedStruct: true}
schema := reflector.Reflect(&app.Config{})
schemaJSON, err := json.MarshalIndent(schema, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
err = ioutil.WriteFile("schema.json", schemaJSON, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
47 changes: 47 additions & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:generate go run generate/schema_generator.go
//go:generate go-bindata -pkg schema -o assets.go schema.json

package schema

import (
"bytes"
"errors"
"fmt"

"github.com/xeipuuv/gojsonschema"
)

var schema *gojsonschema.Schema

func init() {
jsonSchema, err := Asset("schema.json")
if err != nil {
panic(err)
}
schemaLoader := gojsonschema.NewStringLoader(string(jsonSchema))
schema, err = gojsonschema.NewSchema(schemaLoader)
if err != nil {
panic(err)
}
}

// Validate validates the provided JSON against the v2 JSON schema.
func Validate(JSON []byte) error {
JSONLoader := gojsonschema.NewStringLoader(string(JSON))
result, err := schema.Validate(JSONLoader)

if err != nil {
return err
}

if result.Valid() {
return err
}
var msg bytes.Buffer

msg.WriteString("The JSON is not valid. See errors:\n")
for _, desc := range result.Errors() {
msg.WriteString(fmt.Sprintf("- %s\n", desc))
}
return errors.New(msg.String())
}
Loading

0 comments on commit c2ec644

Please sign in to comment.