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

Update json schema lib #251

Merged
merged 3 commits into from
Nov 10, 2022
Merged
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
2 changes: 1 addition & 1 deletion credential/manifest/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestIsValidCredentialApplicationForManifest(t *testing.T) {
assert.NoError(tt, err)

unfulfilledIDs, err = IsValidCredentialApplicationForManifest(cm, request)
assert.Contains(t, err.Error(), "format must be one of the following:")
assert.Contains(t, err.Error(), "value must be one of")
assert.Empty(tt, unfulfilledIDs)

// reset
Expand Down
2 changes: 1 addition & 1 deletion credential/verification/verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestVerifier(t *testing.T) {
knownSchema := getVCJSONSchema()
err = verifier.VerifyCredential(sampleCredential, WithSchema(knownSchema))
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "emailAddress is required")
assert.Contains(tt, err.Error(), "missing properties: 'emailAddress'")

// verify cred with schema, schema passed in, cred with good data
sampleCredential.CredentialSubject = map[string]interface{}{
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ require (
github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852
github.com/piprate/json-gold v0.4.2
github.com/pkg/errors v0.9.1
github.com/santhosh-tekuri/jsonschema/v5 v5.0.2
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
gopkg.in/h2non/gock.v1 v1.1.2
)
Expand All @@ -41,8 +41,6 @@ require (
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20221006211917-84dc82d7e875 // indirect
golang.org/x/term v0.0.0-20220919170432-7a66f970e087 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prY
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.2 h1:zOYFITq/5SO7YOv39/Taw8s1skb0Py39K5V2XvCEP48=
github.com/santhosh-tekuri/jsonschema/v5 v5.0.2/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 2 additions & 2 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func IOS(pkgs ...string) error {
for _, pkg := range pkgs {
fmt.Printf("Building [%s] package...\n", pkg)
if err := bindIOS(pkg); err != nil {
logrus.WithError(err).Fatal("Error building iOS pkg: %s", pkg)
logrus.WithError(err).Fatalf("Error building iOS pkg: %s", pkg)
return err
}
}
Expand All @@ -275,7 +275,7 @@ func Android(pkgs ...string) error {
for _, pkg := range pkgs {
fmt.Printf("Building [%s] package...\n", pkg)
if err := bindAndroid(pkg); err != nil {
logrus.WithError(err).Fatal("Error building iOS pkg: %s", pkg)
logrus.WithError(err).Fatalf("Error building iOS pkg: %s", pkg)
return err
}
}
Expand Down
61 changes: 43 additions & 18 deletions schema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,40 @@ package schema
import (
"embed"

"github.com/TBD54566975/ssi-sdk/util"
"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/santhosh-tekuri/jsonschema/v5/httploader"
)

"github.com/TBD54566975/ssi-sdk/util"
const (
defaultSchemaURL = "schema.json"
)

var (
//go:embed known_schemas
knownSchemas embed.FS
)

func init() {
jsonschema.Loaders["http"] = httploader.Load
jsonschema.Loaders["https"] = httploader.Load
}

// IsValidJSONSchema returns an error if the schema is not a valid JSON Schema, nil otherwise
func IsValidJSONSchema(maybeSchema string) error {
if !IsValidJSON(maybeSchema) {
return errors.New("input is not valid json")
}
stringLoader := gojsonschema.NewStringLoader(maybeSchema)
schemaLoader := gojsonschema.NewSchemaLoader()
schemaLoader.Validate = true
schemaLoader.Draft = gojsonschema.Draft7
_, err := schemaLoader.Compile(stringLoader)
return err
schema, err := jsonschema.CompileString(defaultSchemaURL, maybeSchema)
if err != nil {
return err
}
if schema == nil {
return errors.New("schema could not be parsed")
}
return nil
}

// IsValidJSON checks if a string is valid json https://stackoverflow.com/a/36922225
Expand All @@ -42,20 +53,34 @@ func IsJSONValidAgainstSchema(json, schema string) error {
if !IsValidJSON(schema) {
return errors.New("schema input is not valid json")
}
jsonLoader := gojsonschema.NewStringLoader(json)
schemaLoader := gojsonschema.NewStringLoader(schema)
result, err := gojsonschema.Validate(schemaLoader, jsonLoader)
if err := IsValidJSONSchema(schema); err != nil {
return errors.Wrap(err, "schema is not valid")
}
jsonSchema, err := jsonschema.CompileString(defaultSchemaURL, schema)
if err != nil {
return err
}
ae := util.NewAppendError()
if !result.Valid() {
for _, e := range result.Errors() {
ae.AppendString(e.String())
}
err = ae.Error()
jsonInterface, err := util.ToJSONInterface(json)
if err != nil {
return errors.Wrap(err, "could not convert json to interface")
}
return jsonSchema.Validate(jsonInterface)
}

// IsJSONValidAgainstSchemaGeneric validates a piece of JSON as an interface{} against a schema,
// returning an error if it is not valid
func IsJSONValidAgainstSchemaGeneric(json interface{}, schema string) error {
if !IsValidJSON(schema) {
return errors.New("schema input is not valid json")
}
if err := IsValidJSONSchema(schema); err != nil {
return errors.Wrap(err, "schema is not valid")
}
jsonSchema, err := jsonschema.CompileString(defaultSchemaURL, schema)
if err != nil {
return err
}
return err
return jsonSchema.Validate(json)
}

func GetKnownSchema(fileName string) (string, error) {
Expand Down
6 changes: 3 additions & 3 deletions schema/jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestJSONSchemaValidation(t *testing.T) {

err = IsJSONValidAgainstSchema(addressDataJSON, addressJSONSchema)
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "postal-code is required")
assert.Contains(tt, err.Error(), "missing properties: 'postal-code'")
})

t.Run("Test Valid Person JSON Schema", func(tt *testing.T) {
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestJSONSchemaValidation(t *testing.T) {
"firstName": "Satoshi",
"middleName": "Coin",
"lastName": "Nakamoto",
"age": "42",
"age": 42,
}

personDataBytes, err := json.Marshal(personData)
Expand All @@ -183,7 +183,7 @@ func TestJSONSchemaValidation(t *testing.T) {

err = IsJSONValidAgainstSchema(personDataJSON, personJSONSchema)
assert.Error(tt, err)
assert.Contains(tt, err.Error(), "Additional property middleName is not allowed")
assert.Contains(tt, err.Error(), "additionalProperties 'middleName' not allowed")
})
}

Expand Down