Skip to content

Commit

Permalink
chore(deps): Update module github.com/santhosh-tekuri/jsonschema/v5 t…
Browse files Browse the repository at this point in the history
…o v6 (#693)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Matt Johnson-Pint <[email protected]>
  • Loading branch information
renovate[bot] and mattjohnsonpint authored Jan 8, 2025
1 parent f9ce52a commit b0a0013
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/manifest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ go 1.23.1
toolchain go1.23.4

require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
github.com/tidwall/gjson v1.18.0
github.com/tidwall/jsonc v0.3.2
)

require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
golang.org/x/text v0.14.0 // indirect
)
8 changes: 6 additions & 2 deletions lib/manifest/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/jsonc v0.3.2 h1:ZTKrmejRlAJYdn0kcaFqRAKlxxFIC21pYq8vLa4p2Wc=
Expand All @@ -9,3 +11,5 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
42 changes: 27 additions & 15 deletions lib/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
package manifest

import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"strings"

"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
)
Expand All @@ -28,6 +30,7 @@ const currentVersion = 3

//go:embed modus_schema.json
var schemaContent string
var schema *jsonschema.Schema

type Manifest struct {
Version int `json:"-"`
Expand All @@ -54,34 +57,43 @@ func (m *Manifest) GetVariables() map[string][]string {
return results
}

func init() {
doc, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaContent))
if err != nil {
panic(fmt.Errorf("failed to parse manifest schema: %w", err))
}

c := jsonschema.NewCompiler()
if err := c.AddResource("modus.json", doc); err != nil {
panic(fmt.Errorf("failed to add manifest schema: %w", err))
}

if sch, err := c.Compile("modus.json"); err != nil {
panic(fmt.Errorf("failed to compile manifest schema: %w", err))
} else {
schema = sch
}
}

func IsCurrentVersion(version int) bool {
return version == currentVersion
}

func ValidateManifest(content []byte) error {

sch, err := jsonschema.CompileString("modus.json", schemaContent)
r := bytes.NewReader(jsonc.ToJSON(content))
doc, err := jsonschema.UnmarshalJSON(r)
if err != nil {
return err
return fmt.Errorf("failed to parse manifest: %w", err)
}

content = jsonc.ToJSONInPlace(content)

var v interface{}
if err := json.Unmarshal(content, &v); err != nil {
return fmt.Errorf("failed to deserialize manifest: %w", err)
}
if err := sch.Validate(v); err != nil {
if err := schema.Validate(doc); err != nil {
return fmt.Errorf("failed to validate manifest: %w", err)
}

return nil
}

func ReadManifest(content []byte) (*Manifest, error) {

content = jsonc.ToJSONInPlace(content)

var manifest Manifest
if err := parseManifestJson(content, &manifest); err != nil {
return nil, fmt.Errorf("failed to parse manifest: %w", err)
Expand All @@ -96,7 +108,7 @@ func parseManifestJson(data []byte, manifest *Manifest) error {
Connections map[string]json.RawMessage `json:"connections"`
Collections map[string]CollectionInfo `json:"collections"`
}
if err := json.Unmarshal(data, &m); err != nil {
if err := json.Unmarshal(jsonc.ToJSON(data), &m); err != nil {
return fmt.Errorf("failed to parse manifest: %w", err)
}

Expand Down

0 comments on commit b0a0013

Please sign in to comment.