Skip to content

Commit

Permalink
Return to the user the reason of the invalid schema (#2232)
Browse files Browse the repository at this point in the history
* Return to the user the reason of the invalid schema

Currently an error in the node/edge schema gives no clue to the user to
where is the error.
Append to that message the list of errors returned by the validator.
  • Loading branch information
adrianlzt committed Jun 29, 2020
1 parent 0088d2f commit 5283259
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions graffiti/schema/validator.go
Expand Up @@ -18,13 +18,24 @@
package schema

import (
"errors"
"fmt"
"strings"

"github.com/xeipuuv/gojsonschema"
)

// ErrInvalidSchema is return when a JSON schema is invalid
var ErrInvalidSchema = errors.New("Invalid schema")
type ErrInvalidSchema struct {
Errors []gojsonschema.ResultError
}

func (e ErrInvalidSchema) Error() string {
msg := make([]string, len(e.Errors))
for i, err := range e.Errors {
msg[len(e.Errors)-i-1] = err.String()
}
return fmt.Sprintf("invalid schema: %v", strings.Join(msg, "\n"))
}

// Validator is the interface to implement to validate REST resources
type Validator interface {
Expand All @@ -48,7 +59,7 @@ func (v *JSONValidator) Validate(kind string, obj interface{}) error {
if err != nil {
return err
} else if !result.Valid() {
return ErrInvalidSchema
return &ErrInvalidSchema{Errors: result.Errors()}
}
return nil
}
Expand Down

0 comments on commit 5283259

Please sign in to comment.