Skip to content

Commit 1a7a762

Browse files
committed
fix(bundle/definition): add custom validator for contentEncoding
1 parent bfc3988 commit 1a7a762

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

bundle/definition/schema.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/pkg/errors"
9-
"github.com/qri-io/jsonschema"
109
)
1110

1211
type Definitions map[string]*Schema
@@ -96,7 +95,7 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
9695
// Before we unmarshal into the cnab-go bundle/definition/Schema type, unmarshal into
9796
// the library struct so we can handle any validation errors in the schema. If there
9897
// are any errors, return those.
99-
js := new(jsonschema.RootSchema)
98+
js := NewRootSchema()
10099
if err := js.UnmarshalJSON(data); err != nil {
101100
return err
102101
}

bundle/definition/validation.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55

66
"github.com/pkg/errors"
7-
"github.com/qri-io/jsonschema"
87
)
98

109
// ValidationError error represents a validation error
@@ -24,7 +23,7 @@ func (s *Schema) Validate(data interface{}) ([]ValidationError, error) {
2423
if err != nil {
2524
return nil, errors.Wrap(err, "unable to load schema")
2625
}
27-
def := new(jsonschema.RootSchema)
26+
def := NewRootSchema()
2827
err = json.Unmarshal([]byte(b), def)
2928
if err != nil {
3029
return nil, errors.Wrap(err, "unable to build schema")

bundle/definition/validation_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,39 @@ func TestObjectValidationValid(t *testing.T) {
4242
assert.NoError(t, err)
4343
}
4444

45+
func TestObjectValidationValid_CustomValidator(t *testing.T) {
46+
s := `{
47+
"type": "object",
48+
"properties" : {
49+
"file" : {
50+
"type": "string",
51+
"contentEncoding": "base64"
52+
}
53+
},
54+
"required" : ["file"]
55+
}`
56+
definition := new(Schema)
57+
err := json.Unmarshal([]byte(s), definition)
58+
require.NoError(t, err, "should have been able to marshal definition")
59+
assert.Equal(t, "object", definition.Type, "type should have been an object")
60+
props := definition.Properties
61+
assert.NotNil(t, props, "should have found properties")
62+
assert.Equal(t, 1, len(props), "should have had a single property")
63+
propSchema, ok := props["file"]
64+
assert.True(t, ok, "should have found file property")
65+
assert.Equal(t, "string", propSchema.Type, "file type should have been a string")
66+
assert.Equal(t, "base64", propSchema.ContentEncoding, "file contentEncoding should have been base64")
67+
68+
val := struct {
69+
File string `json:"file"`
70+
}{
71+
File: "SGVsbG8gV29ybGQhCg==",
72+
}
73+
valErrors, err := definition.Validate(val)
74+
assert.Len(t, valErrors, 0, "expected no validation errors")
75+
assert.NoError(t, err)
76+
}
77+
4578
func TestObjectValidationInValidMinimum(t *testing.T) {
4679
s := `{
4780
"type": "object",

bundle/definition/validators.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package definition
2+
3+
import (
4+
"github.com/qri-io/jsonschema"
5+
)
6+
7+
// ContentEncoding represents a "custom" Schema property
8+
type ContentEncoding string
9+
10+
// NewContentEncoding allocates a new ContentEncoding validator
11+
func NewContentEncoding() jsonschema.Validator {
12+
return new(ContentEncoding)
13+
}
14+
15+
// Validate implements the Validator interface for ContentEncoding
16+
// Currently, this is a no-op and is only used to register with the jsonschema library
17+
// that 'contentEncoding' is a valid property (as of writing, it isn't one of the defaults)
18+
func (c ContentEncoding) Validate(propPath string, data interface{}, errs *[]jsonschema.ValError) {}
19+
20+
// NewRootSchema returns a jsonschema.RootSchema with any needed custom
21+
// jsonschema.Validators pre-registered
22+
func NewRootSchema() *jsonschema.RootSchema {
23+
// Register custom validators here
24+
// Note: as of writing, jsonschema doesn't have a stock validator for intances of type `contentEncoding`
25+
// There may be others missing in the library that exist in http://json-schema.org/draft-07/schema#
26+
// and thus, we'd need to create/register them here (if not included upstream)
27+
jsonschema.RegisterValidator("contentEncoding", NewContentEncoding)
28+
return new(jsonschema.RootSchema)
29+
}

0 commit comments

Comments
 (0)