Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON Schema validation on local fs #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

orderedmap "github.com/wk8/go-ordered-map/v2"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"

"github.com/acronis/go-stacktrace"
Expand Down Expand Up @@ -843,8 +844,9 @@ type JSONShape struct {
noScalarShape
*BaseShape

Schema *JSONSchema
Raw string
Schema *JSONSchema
Validator *gojsonschema.Schema
Raw string
}

func (s *JSONShape) Base() *BaseShape {
Expand All @@ -863,8 +865,21 @@ func (s *JSONShape) clone(base *BaseShape, _ map[int64]*BaseShape) Shape {
return &c
}

func (s *JSONShape) validate(_ interface{}, _ string) error {
// TODO: Implement validation with JSON Schema
func (s *JSONShape) validate(v interface{}, _ string) error {
l := gojsonschema.NewGoLoader(v)

result, err := s.Validator.Validate(l)
if err != nil {
return StacktraceNewWrapped("validate JSON schema", err, s.Location, stacktrace.WithPosition(&s.Position))
}

if !result.Valid() {
st := StacktraceNew("failed to validate against JSON schema", s.Location, stacktrace.WithPosition(&s.Position))
for _, err := range result.Errors() {
st = st.Append(StacktraceNew(err.String(), s.Location, stacktrace.WithPosition(&s.Position)))
}
return st
}
return nil
}

Expand All @@ -884,6 +899,7 @@ func (s *JSONShape) inherit(source Shape) (Shape, error) {
return nil, StacktraceNew("cannot inherit from different JSON schema", s.Location,
stacktrace.WithPosition(&s.Position))
}
s.Validator = ss.Validator
s.Schema = ss.Schema
s.Raw = ss.Raw
return s, nil
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/antlr4-go/antlr/v4 v4.13.0
github.com/stretchr/testify v1.9.0
github.com/wk8/go-ordered-map/v2 v2.1.8
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -16,5 +17,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
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.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
26 changes: 25 additions & 1 deletion shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync/atomic"

orderedmap "github.com/wk8/go-ordered-map/v2"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"

"github.com/acronis/go-stacktrace"
Expand Down Expand Up @@ -500,14 +501,37 @@ func (r *RAML) MakeRecursiveShape(headBase *BaseShape) *BaseShape {
func (r *RAML) MakeJSONShape(base *BaseShape, rawSchema string) (*JSONShape, error) {
base.Type = "json"

// TODO: Probably this can be replaced with gojsonschema but it does not expose internal schema structure.
var schema *JSONSchema
err := json.Unmarshal([]byte(rawSchema), &schema)
if err != nil {
return nil, StacktraceNewWrapped("unmarshal json", err, base.Location,
stacktrace.WithPosition(&base.Position))
}

return &JSONShape{BaseShape: base, Raw: rawSchema, Schema: schema}, nil
// TODO: This will only work with local files, but currently we work only with local files anyway
p := "file://" + filepath.ToSlash(base.Location)
// Load schema using string loader
l := gojsonschema.NewStringLoader(rawSchema)
sl := gojsonschema.NewSchemaLoader()
// Add it to schema loader with URI pointing to RAML file.
// This will cache the schema and resolve all references against this URI.
err = sl.AddSchema(p, l)
if err != nil {
return nil, StacktraceNewWrapped("add schema", err, base.Location,
stacktrace.WithPosition(&base.Position))
}
// Replace StringLoader with ReferenceLoader to support local/remote references resolution.
// Since the reference is cached, gojsonschema will not attempt to load it from the storage.
// TODO: Introduce custom reference loader to have possibility to disallow remote schemas.
l = gojsonschema.NewReferenceLoader(p)
validator, err := sl.Compile(l)
if err != nil {
return nil, StacktraceNewWrapped("new schema", err, base.Location,
stacktrace.WithPosition(&base.Position))
}

return &JSONShape{BaseShape: base, Raw: rawSchema, Schema: schema, Validator: validator}, nil
}

const HookBeforeRAMLMakeConcreteShapeYAML = "before:RAML.makeConcreteShapeYAML"
Expand Down
Loading