Skip to content

Commit

Permalink
changes from comments middleware validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelarkn committed Oct 2, 2017
1 parent d4e73e2 commit 8d75b0d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 88 deletions.
15 changes: 3 additions & 12 deletions backend/repository/endpoint_update.go
Expand Up @@ -25,7 +25,6 @@ import (
"path/filepath"
"strings"

"encoding/json"
"github.com/pkg/errors"
"github.com/uber/zanzibar/codegen"
)
Expand Down Expand Up @@ -87,17 +86,9 @@ func (r *Repository) validateEndpointCfg(req *EndpointConfig) error {
for _, mid := range req.Middlewares {
for _, midCfg := range r.gatewayConfig.Middlewares {
if mid.Name == midCfg.Name {
result, err := r.validator.ValidateGo(midCfg.SchemaFile, mid.Options)
if err != nil && result != nil {
resultErrors, _ := json.Marshal(result.Errors())
return errors.Wrap(err, "schema validation error\nErrors:\n"+string(resultErrors))
}
if err != nil {
return errors.Wrap(err, "schema validation error unknown")
}
if !result.Valid() {
resultErrors, _ := json.Marshal(result.Errors())
return errors.New("schema validation error :\n" + string(resultErrors))
err := codegen.SchemaValidateGo(midCfg.SchemaFile, mid.Options)
if err != nil {
return err
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions backend/repository/repository.go
Expand Up @@ -25,7 +25,6 @@ import (
"time"

"github.com/pkg/errors"
"github.com/uber/zanzibar/codegen"
)

// Repository operates one local repository:
Expand All @@ -45,7 +44,6 @@ type Repository struct {
// Cached error when getting gateway config.
gatewayConfigError error
sync.RWMutex
validator *codegen.SchemaValidator
}

// Fetcher fetches remote repository to local repository.
Expand Down Expand Up @@ -80,7 +78,6 @@ func NewRepository(localRoot, remote string, fetcher Fetcher,
fetcher: fetcher,
lastUpdateTime: time.Now(),
refreshInterval: refreshInterval,
validator: &codegen.SchemaValidator{},
}, nil
}

Expand Down
77 changes: 29 additions & 48 deletions codegen/schema_validator.go
Expand Up @@ -20,59 +20,40 @@

package codegen

import "github.com/xeipuuv/gojsonschema"

// ResultError abstracts a subset of interface in gojsonschema
type ResultError interface {
Field() string
Type() string
Description() string
Value() interface{}
String() string
}

// Result interface type subset of gojsonschema
type Result interface {
Valid() bool
Errors() []ResultError
}

type resultStruct struct {
valid bool
errors []ResultError
}

func (r *resultStruct) Valid() bool {
return r.valid
}

func (r *resultStruct) Errors() []ResultError {
return r.errors
}

// SchemaValidator abstracts undlerlying schema validation library
type SchemaValidator struct{}

// ValidateGo performs JSON schema validation from schema file on to arbitrary Go object
func (s *SchemaValidator) ValidateGo(schemaFile string, arbitraryObj map[string]interface{}) (Result, error) {
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)

// SchemaValidateGo performs JSON schema validation from schema file on to arbitrary Go object
func SchemaValidateGo(schemaFile string, arbitraryObj map[string]interface{}) error {
// gojsonschmea caches schema validators
schemaLoader := gojsonschema.NewReferenceLoader(schemaFile)
jsonLoader := gojsonschema.NewGoLoader(arbitraryObj)
res, err := gojsonschema.Validate(schemaLoader, jsonLoader)
result, err := gojsonschema.Validate(schemaLoader, jsonLoader)

if res == nil {
return nil, err
if err != nil && result != nil {
resultErrors, _ := json.Marshal(result.Errors())
return errors.Wrap(err, "schema validation error\nErrors:\n"+string(resultErrors))
}

// convert gojsonschema to our result type
resultErrors := res.Errors()
myResult := &resultStruct{
res.Valid(),
make([]ResultError, len(res.Errors())),
if err != nil {
return errors.Wrap(err, "schema validation error unknown")
}
for i, resultError := range resultErrors {
myResult.errors[i] = resultError.(ResultError)
if !result.Valid() {
msg := "schema validation error :"
for _, resErr := range result.Errors() {

details, _ := json.Marshal(resErr.Details())
msg += fmt.Sprintf("\nType: %s\nField: %s\nDescription: %s\nDetails: %s\n",
resErr.Type(),
resErr.Field(),
resErr.Description(),
string(details),
)
}
return errors.New(msg)
}

return myResult, err
return nil
}
25 changes: 4 additions & 21 deletions codegen/schema_validator_test.go
Expand Up @@ -28,9 +28,6 @@ import (
)

func TestSchemaValidatorSuccess(t *testing.T) {

validator := &codegen.SchemaValidator{}

var obj map[string]interface{}
obj = make(map[string]interface{})
obj["foo"] = true
Expand All @@ -39,18 +36,11 @@ func TestSchemaValidatorSuccess(t *testing.T) {
assert.NoError(t, err)
canonFileRef := "file://" + absPath

res, err := validator.ValidateGo(canonFileRef, obj)
assert.NoError(t, err)
if res != nil {
assert.True(t, res.Valid())
} else {
assert.Fail(t, "res was nil")
}

vErr := codegen.SchemaValidateGo(canonFileRef, obj)
assert.NoError(t, vErr)
}

func TestSchemaValidatorFail(t *testing.T) {
validator := &codegen.SchemaValidator{}

var obj map[string]interface{}
obj = make(map[string]interface{})
Expand All @@ -60,13 +50,6 @@ func TestSchemaValidatorFail(t *testing.T) {
assert.NoError(t, err)
canonFileRef := "file://" + absPath

res, err := validator.ValidateGo(canonFileRef, obj)
assert.NoError(t, err)
if res != nil {
assert.False(t, res.Valid())
assert.Equal(t, res.Errors()[0].Description(), "Invalid type. Expected: boolean, given: integer")
} else {
assert.Fail(t, "res was nil")
}

vErr := codegen.SchemaValidateGo(canonFileRef, obj)
assert.Error(t, vErr)
}
8 changes: 4 additions & 4 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Expand Up @@ -47,3 +47,4 @@ import:
- package: github.com/stretchr/testify
version: f6abca593680b2315d2075e0f5e2a9751e3f431a
- package: github.com/xeipuuv/gojsonschema
version: 3f523f4c14b6e925da10475eb0447c2f28614aac

0 comments on commit 8d75b0d

Please sign in to comment.