From 52832599f28feb38c1a0e82dd30479aef62cad5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20L=C3=B3pez?= Date: Mon, 29 Jun 2020 18:18:42 +0200 Subject: [PATCH] Return to the user the reason of the invalid schema (#2232) * 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. --- graffiti/schema/validator.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/graffiti/schema/validator.go b/graffiti/schema/validator.go index 7107051162..707a3beaa1 100644 --- a/graffiti/schema/validator.go +++ b/graffiti/schema/validator.go @@ -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 { @@ -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 }