Skip to content

Commit 5c7d528

Browse files
authored
add support for validator (#70)
* add support for validator 2020-12 * add JsonSchema2023 * timeout for tests increased
1 parent b106f94 commit 5c7d528

File tree

7 files changed

+334
-24
lines changed

7 files changed

+334
-24
lines changed

.github/workflows/ci-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ jobs:
3131
- name: Unit Tests
3232
env:
3333
IPFS_URL: ${{secrets.IPFS_URL }}
34-
run: go test -race -timeout=60s ./...
34+
run: go test -race -timeout=180s ./...

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
// https://github.com/piprate/json-gold/commit/36fcca9d7e487684a764e552e7d837a14546a157
1414
github.com/piprate/json-gold v0.5.1-0.20230111113000-6ddbe6e6f19f
1515
github.com/pkg/errors v0.9.1
16-
github.com/qri-io/jsonschema v0.2.1
16+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0
1717
github.com/stretchr/testify v1.8.2
1818
golang.org/x/crypto v0.7.0
1919
)
@@ -43,7 +43,6 @@ require (
4343
github.com/multiformats/go-varint v0.0.7 // indirect
4444
github.com/pmezard/go-difflib v1.0.0 // indirect
4545
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
46-
github.com/qri-io/jsonpointer v0.1.1 // indirect
4746
github.com/spaolacci/murmur3 v1.1.0 // indirect
4847
github.com/whyrusleeping/tar-utils v0.0.0-20201201191210-20a61371de5b // indirect
4948
golang.org/x/sys v0.6.0 // indirect

go.sum

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,13 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
6767
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6868
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 h1:J9b7z+QKAmPf4YLrFg6oQUotqHQeUNWwkvo7jZp1GLU=
6969
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
70-
github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA=
71-
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
72-
github.com/qri-io/jsonschema v0.2.1 h1:NNFoKms+kut6ABPf6xiKNM5214jzxAhDBrPHCJ97Wg0=
73-
github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI=
74-
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
75-
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
70+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0 h1:uIkTLo0AGRc8l7h5l9r+GcYi9qfVPt6lD4/bhmzfiKo=
71+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
7672
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
7773
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
7874
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7975
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8076
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
81-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
8277
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8378
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
8479
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=

json/validator.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package json
22

33
import (
4-
"context"
4+
"bytes"
55
"encoding/json"
66

7-
"github.com/pkg/errors"
8-
"github.com/qri-io/jsonschema"
7+
jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
98
)
109

1110
// Validator validate json data
@@ -14,16 +13,23 @@ type Validator struct {
1413

1514
// ValidateData validate JSON data by JSON Schema
1615
func (v Validator) ValidateData(data, schema []byte) error {
17-
rs := &jsonschema.Schema{}
18-
if err := json.Unmarshal(schema, rs); err != nil {
19-
return errors.Wrap(err, "unmarshal schema")
16+
17+
compiler := jsonschema.NewCompiler()
18+
19+
err := compiler.AddResource("temp.json", bytes.NewReader(schema))
20+
if err != nil {
21+
return err
2022
}
21-
errs, err := rs.ValidateBytes(context.Background(), data)
23+
24+
var c map[string]interface{}
25+
err = json.Unmarshal(data, &c)
2226
if err != nil {
23-
return errors.Wrap(err, "err during schema validation")
27+
return err
2428
}
25-
if len(errs) > 0 {
26-
return errs[0] // return only first error
29+
30+
sh, err := compiler.Compile("temp.json")
31+
if err != nil {
32+
return err
2733
}
28-
return nil
34+
return sh.Validate(c)
2935
}

json/validator_test.go

Lines changed: 308 additions & 2 deletions
Large diffs are not rendered by default.

processor/json/processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ func TestValidatorWithInvalidField(t *testing.T) {
8686
err = jsonProcessor.ValidateData(dataBytes, schema)
8787

8888
assert.NotNil(t, err)
89-
assert.Containsf(t, err.Error(), "\"birthday\" value is required", "expected error containing %q, got %s", "\"birthdayYear\" value is required", err)
89+
assert.Containsf(t, err.Error(), "missing properties: 'birthday'", "expected error containing %q, got %s", "missing properties: 'birthDayYear'", err)
9090

9191
}

verifiable/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ const (
1919
Iden3ReverseSparseMerkleTreeProof CredentialStatusType = "Iden3ReverseSparseMerkleTreeProof"
2020

2121
// JSONSchemaValidator2018 JSON schema for verification of W3CCredential
22+
// Deprecated: https://www.w3.org/2018/credentials/#JsonSchemaValidator2018
2223
JSONSchemaValidator2018 = "JsonSchemaValidator2018"
2324

25+
// JSONSchema2023 JSON schema for verification of W3CCredential (https://www.w3.org/TR/vc-json-schema/#jsonschema2023)
26+
JSONSchema2023 = "JsonSchema2023"
27+
2428
// BJJSignatureProofType is a proof type for BJJ signature proofs
2529
BJJSignatureProofType ProofType = "BJJSignature2021"
2630

0 commit comments

Comments
 (0)