Skip to content

Commit

Permalink
fix(bundle/definition): add custom validator for contentEncoding
Browse files Browse the repository at this point in the history
  • Loading branch information
vdice committed Sep 4, 2019
1 parent bfc3988 commit 1a7a762
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
3 changes: 1 addition & 2 deletions bundle/definition/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/qri-io/jsonschema"
)

type Definitions map[string]*Schema
Expand Down Expand Up @@ -96,7 +95,7 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
// Before we unmarshal into the cnab-go bundle/definition/Schema type, unmarshal into
// the library struct so we can handle any validation errors in the schema. If there
// are any errors, return those.
js := new(jsonschema.RootSchema)
js := NewRootSchema()
if err := js.UnmarshalJSON(data); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions bundle/definition/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"

"github.com/pkg/errors"
"github.com/qri-io/jsonschema"
)

// ValidationError error represents a validation error
Expand All @@ -24,7 +23,7 @@ func (s *Schema) Validate(data interface{}) ([]ValidationError, error) {
if err != nil {
return nil, errors.Wrap(err, "unable to load schema")
}
def := new(jsonschema.RootSchema)
def := NewRootSchema()
err = json.Unmarshal([]byte(b), def)
if err != nil {
return nil, errors.Wrap(err, "unable to build schema")
Expand Down
33 changes: 33 additions & 0 deletions bundle/definition/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ func TestObjectValidationValid(t *testing.T) {
assert.NoError(t, err)
}

func TestObjectValidationValid_CustomValidator(t *testing.T) {
s := `{
"type": "object",
"properties" : {
"file" : {
"type": "string",
"contentEncoding": "base64"
}
},
"required" : ["file"]
}`
definition := new(Schema)
err := json.Unmarshal([]byte(s), definition)
require.NoError(t, err, "should have been able to marshal definition")
assert.Equal(t, "object", definition.Type, "type should have been an object")
props := definition.Properties
assert.NotNil(t, props, "should have found properties")
assert.Equal(t, 1, len(props), "should have had a single property")
propSchema, ok := props["file"]
assert.True(t, ok, "should have found file property")
assert.Equal(t, "string", propSchema.Type, "file type should have been a string")
assert.Equal(t, "base64", propSchema.ContentEncoding, "file contentEncoding should have been base64")

val := struct {
File string `json:"file"`
}{
File: "SGVsbG8gV29ybGQhCg==",
}
valErrors, err := definition.Validate(val)
assert.Len(t, valErrors, 0, "expected no validation errors")
assert.NoError(t, err)
}

func TestObjectValidationInValidMinimum(t *testing.T) {
s := `{
"type": "object",
Expand Down
29 changes: 29 additions & 0 deletions bundle/definition/validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package definition

import (
"github.com/qri-io/jsonschema"
)

// ContentEncoding represents a "custom" Schema property
type ContentEncoding string

// NewContentEncoding allocates a new ContentEncoding validator
func NewContentEncoding() jsonschema.Validator {
return new(ContentEncoding)
}

// Validate implements the Validator interface for ContentEncoding
// Currently, this is a no-op and is only used to register with the jsonschema library
// that 'contentEncoding' is a valid property (as of writing, it isn't one of the defaults)
func (c ContentEncoding) Validate(propPath string, data interface{}, errs *[]jsonschema.ValError) {}

// NewRootSchema returns a jsonschema.RootSchema with any needed custom
// jsonschema.Validators pre-registered
func NewRootSchema() *jsonschema.RootSchema {
// Register custom validators here
// Note: as of writing, jsonschema doesn't have a stock validator for intances of type `contentEncoding`
// There may be others missing in the library that exist in http://json-schema.org/draft-07/schema#
// and thus, we'd need to create/register them here (if not included upstream)
jsonschema.RegisterValidator("contentEncoding", NewContentEncoding)
return new(jsonschema.RootSchema)
}

0 comments on commit 1a7a762

Please sign in to comment.