Skip to content

Commit be93380

Browse files
errors: captures error message fields in struct
closes Issue #44
1 parent 2dc9222 commit be93380

File tree

6 files changed

+500
-75
lines changed

6 files changed

+500
-75
lines changed

errors.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package jsonschema
33
import (
44
"fmt"
55
"strings"
6+
7+
"github.com/santhosh-tekuri/jsonschema/v5/msg"
68
)
79

810
// InvalidJSONTypeError is the error type returned by ValidateInterface.
@@ -69,7 +71,7 @@ type ValidationError struct {
6971
KeywordLocation string // validation path of validating keyword or schema
7072
AbsoluteKeywordLocation string // absolute location of validating keyword or schema
7173
InstanceLocation string // location of the json value within the instance being validated
72-
Message string // describes error
74+
Message fmt.Stringer // captures the message and data used in constructing it
7375
Causes []*ValidationError // nested validation errors
7476
}
7577

@@ -81,10 +83,11 @@ func (ve *ValidationError) add(causes ...error) error {
8183
}
8284

8385
func (ve *ValidationError) causes(err error) error {
84-
if err := err.(*ValidationError); err.Message == "" {
85-
ve.Causes = err.Causes
86+
var e = err.(*ValidationError)
87+
if _, ok := e.Message.(msg.Empty); ok {
88+
ve.Causes = e.Causes
8689
} else {
87-
ve.add(err)
90+
ve.add(e)
8891
}
8992
return ve
9093
}

example_extension_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ func (powerOfCompiler) Compile(ctx jsonschema.CompilerContext, m map[string]inte
3131
return nil, nil
3232
}
3333

34+
type powerOfSchemaMsg struct {
35+
got interface{}
36+
want int64
37+
}
38+
39+
func (m powerOfSchemaMsg) String() string {
40+
return fmt.Sprintf("%v not powerOf %v", m.got, m.want)
41+
}
42+
3443
type powerOfSchema int64
3544

3645
func (s powerOfSchema) Validate(ctx jsonschema.ValidationContext, v interface{}) error {
@@ -42,7 +51,7 @@ func (s powerOfSchema) Validate(ctx jsonschema.ValidationContext, v interface{})
4251
n = n / pow
4352
}
4453
if n != 1 {
45-
return ctx.Error("powerOf", "%v not powerOf %v", v, pow)
54+
return ctx.Error("powerOf", powerOfSchemaMsg{got: v, want: pow})
4655
}
4756
return nil
4857
default:

extension.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package jsonschema
22

3+
import "fmt"
4+
35
// ExtCompiler compiles custom keyword(s) into ExtSchema.
46
type ExtCompiler interface {
57
// Compile compiles the custom keywords in schema m and returns its compiled representation.
@@ -75,7 +77,7 @@ type ValidationContext struct {
7577
result validationResult
7678
validate func(sch *Schema, schPath string, v interface{}, vpath string) error
7779
validateInplace func(sch *Schema, schPath string) error
78-
validationError func(keywordPath string, format string, a ...interface{}) *ValidationError
80+
validationError func(keywordPath string, msg fmt.Stringer) *ValidationError
7981
}
8082

8183
// EvaluatedProp marks given property of object as evaluated.
@@ -104,8 +106,8 @@ func (ctx ValidationContext) Validate(s *Schema, spath string, v interface{}, vp
104106
// Error used to construct validation error by extensions.
105107
//
106108
// keywordPath is relative-json-pointer to keyword.
107-
func (ctx ValidationContext) Error(keywordPath string, format string, a ...interface{}) *ValidationError {
108-
return ctx.validationError(keywordPath, format, a...)
109+
func (ctx ValidationContext) Error(keywordPath string, msg fmt.Stringer) *ValidationError {
110+
return ctx.validationError(keywordPath, msg)
109111
}
110112

111113
// Group is used by extensions to group multiple errors as causes to parent error.

0 commit comments

Comments
 (0)