Skip to content

Commit

Permalink
feat(validators.go): add base64 validation to the ContentEncoding val…
Browse files Browse the repository at this point in the history
…idator
  • Loading branch information
vdice committed Sep 19, 2019
1 parent f51437c commit b962d3d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
37 changes: 36 additions & 1 deletion bundle/definition/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestObjectValidationValid(t *testing.T) {
assert.NoError(t, err)
}

func TestObjectValidationValid_CustomValidator(t *testing.T) {
func TestObjectValidationValid_CustomValidator_ContentEncoding_base64(t *testing.T) {
s := `{
"type": "object",
"properties" : {
Expand Down Expand Up @@ -71,8 +71,43 @@ func TestObjectValidationValid_CustomValidator(t *testing.T) {
File: "SGVsbG8gV29ybGQhCg==",
}
valErrors, err := definition.Validate(val)
assert.NoError(t, err)
assert.Len(t, valErrors, 0, "expected no validation errors")

invalidVal := struct {
File string `json:"file"`
}{
File: "SGVsbG8gV29ybGQhCg===",
}
valErrors, err = definition.Validate(invalidVal)
assert.NoError(t, err)
assert.Len(t, valErrors, 1, "expected 1 validation error")
assert.Equal(t, "invalid base64 value: SGVsbG8gV29ybGQhCg===", valErrors[0].Error)
}

func TestObjectValidationValid_CustomValidator_ContentEncoding_InvalidEncoding(t *testing.T) {
s := `{
"type": "object",
"properties" : {
"file" : {
"type": "string",
"contentEncoding": "base65"
}
},
"required" : ["file"]
}`
definition := new(Schema)
err := json.Unmarshal([]byte(s), definition)

val := struct {
File string `json:"file"`
}{
File: "SGVsbG8gV29ybGQhCg==",
}
valErrors, err := definition.Validate(val)
assert.NoError(t, err)
assert.Len(t, valErrors, 1, "expected 1 validation error")
assert.Equal(t, "unsupported or invalid contentEncoding type of base65", valErrors[0].Error)
}

func TestObjectValidationInValidMinimum(t *testing.T) {
Expand Down
24 changes: 20 additions & 4 deletions bundle/definition/validators.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package definition

import (
"encoding/base64"
"fmt"

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

Expand All @@ -13,15 +16,28 @@ func NewContentEncoding() jsonschema.Validator {
}

// 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) {}
// which, as of writing, isn't included by default in the jsonschema library we consume
func (c ContentEncoding) Validate(propPath string, data interface{}, errs *[]jsonschema.ValError) {
if obj, ok := data.(string); ok {
switch c {
case "base64":
_, err := base64.StdEncoding.DecodeString(obj)
if err != nil {
jsonschema.AddError(errs, propPath, data, fmt.Sprintf("invalid %s value: %s", c, obj))
}
// Add validation support for other encodings as needed
// See https://json-schema.org/latest/json-schema-validation.html#rfc.section.8.3
default:
jsonschema.AddError(errs, propPath, data, fmt.Sprintf("unsupported or invalid contentEncoding type of %s", c))
}
}
}

// 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`
// Note: as of writing, jsonschema doesn't have a stock validator for instances 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)
Expand Down

0 comments on commit b962d3d

Please sign in to comment.