Skip to content

Commit

Permalink
Generate JSON schema of flow action definition
Browse files Browse the repository at this point in the history
  • Loading branch information
debovema committed Feb 4, 2019
1 parent 95199d4 commit 6b8a4f2
Show file tree
Hide file tree
Showing 6 changed files with 563 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
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/project-flogo/core v0.9.0-alpha.1 h1:c6BB8TPct70wvgyJ/FE1CCgZbn5WrLWZSZWJ6qR+Gq8=
github.com/project-flogo/core v0.9.0-alpha.1/go.mod h1:2ahj+3BgitIAcO0P/3aW2YClPekCuVJzyDT6twLb2xM=
github.com/project-flogo/core v0.9.0-alpha.3 h1:G7ToVpdoRthE9BhefJWVL1yy+FxhLbA78kbP2EbWx2k=
github.com/project-flogo/core v0.9.0-alpha.3/go.mod h1:BHeB55AxPhvlNGd+it50rE977ag6xE3bD2RluSDeKBA=
github.com/sirupsen/logrus v1.1.1 h1:VzGj7lhU7KEB9e9gMpAV/v5XT2NVSvLJhJLCWbnkgXg=
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
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=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
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/project-flogo/flow/definition"
"github.com/square-it/jsonschema"
)

func main() {
reflector := &jsonschema.Reflector{ExpandedStruct: true}
schema := reflector.Reflect(&definition.DefinitionRep{})
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 6b8a4f2

Please sign in to comment.