make jsonContext public + improve result.AddError
diff --git a/README.md b/README.md
index fe43659..51a5111 100644
--- a/README.md
+++ b/README.md
@@ -190,7 +190,7 @@
**err.Value()**: *interface{}* Returns the value given
-**err.Context()**: *gojsonschema.jsonContext* Returns the context. This has a String() method that will print something like this: (root).firstName
+**err.Context()**: *gojsonschema.JsonContext* Returns the context. This has a String() method that will print something like this: (root).firstName
**err.Field()**: *string* Returns the fieldname in the format firstName, or for embedded properties, person.firstName. This returns the same as the String() method on *err.Context()* but removes the (root). prefix.
diff --git a/errors.go b/errors.go
index 072ce25..0b17027 100644
--- a/errors.go
+++ b/errors.go
@@ -158,7 +158,7 @@
)
// newError takes a ResultError type and sets the type, context, description, details, value, and field
-func newError(err ResultError, context *jsonContext, value interface{}, locale locale, details ErrorDetails) {
+func newError(err ResultError, context *JsonContext, value interface{}, locale locale, details ErrorDetails) {
var t string
var d string
switch err.(type) {
diff --git a/jsonContext.go b/jsonContext.go
index 1c58656..f40668a 100644
--- a/jsonContext.go
+++ b/jsonContext.go
@@ -26,20 +26,20 @@
import "bytes"
-// jsonContext implements a persistent linked-list of strings
-type jsonContext struct {
+// JsonContext implements a persistent linked-list of strings
+type JsonContext struct {
head string
- tail *jsonContext
+ tail *JsonContext
}
-func NewJsonContext(head string, tail *jsonContext) *jsonContext {
- return &jsonContext{head, tail}
+func NewJsonContext(head string, tail *JsonContext) *JsonContext {
+ return &JsonContext{head, tail}
}
// String displays the context in reverse.
// This plays well with the data structure's persistent nature with
// Cons and a json document's tree structure.
-func (c *jsonContext) String(del ...string) string {
+func (c *JsonContext) String(del ...string) string {
byteArr := make([]byte, 0, c.stringLen())
buf := bytes.NewBuffer(byteArr)
c.writeStringToBuffer(buf, del)
@@ -47,7 +47,7 @@
return buf.String()
}
-func (c *jsonContext) stringLen() int {
+func (c *JsonContext) stringLen() int {
length := 0
if c.tail != nil {
length = c.tail.stringLen() + 1 // add 1 for "."
@@ -57,7 +57,7 @@
return length
}
-func (c *jsonContext) writeStringToBuffer(buf *bytes.Buffer, del []string) {
+func (c *JsonContext) writeStringToBuffer(buf *bytes.Buffer, del []string) {
if c.tail != nil {
c.tail.writeStringToBuffer(buf, del)
diff --git a/result.go b/result.go
index e2e96fb..b4d3229 100644
--- a/result.go
+++ b/result.go
@@ -40,8 +40,8 @@
Field() string
SetType(string)
Type() string
- SetContext(*jsonContext)
- Context() *jsonContext
+ SetContext(*JsonContext)
+ Context() *JsonContext
SetDescription(string)
Description() string
SetDescriptionFormat(string)
@@ -58,7 +58,7 @@
// can be defined by just embedding this type
ResultErrorFields struct {
errorType string // A string with the type of error (i.e. invalid_type)
- context *jsonContext // Tree like notation of the part that failed the validation. ex (root).a.b ...
+ context *JsonContext // Tree like notation of the part that failed the validation. ex (root).a.b ...
description string // A human readable error message
descriptionFormat string // A format for human readable error message
value interface{} // Value given by the JSON file that is the source of the error
@@ -93,11 +93,11 @@
return v.errorType
}
-func (v *ResultErrorFields) SetContext(context *jsonContext) {
+func (v *ResultErrorFields) SetContext(context *JsonContext) {
v.context = context
}
-func (v *ResultErrorFields) Context() *jsonContext {
+func (v *ResultErrorFields) Context() *JsonContext {
return v.context
}
@@ -166,9 +166,10 @@
return v.errors
}
// Add a fully filled error to the error set
-func (v *Result) AddError(err ResultError, context *jsonContext, value interface{}, details ErrorDetails) {
- if _, exists := details["context"]; !exists && context != nil {
- details["context"] = context.String()
+// SetDescription() will be called with the result of the parsed err.DescriptionFormat()
+func (v *Result) AddError(err ResultError, details ErrorDetails) {
+ if _, exists := details["context"]; !exists && err.Context() != nil {
+ details["context"] = err.Context().String()
}
err.SetDescription(formatErrorDescription(err.DescriptionFormat(), details))
@@ -177,7 +178,7 @@
v.score--
}
-func (v *Result) addInternalError(err ResultError, context *jsonContext, value interface{}, details ErrorDetails) {
+func (v *Result) addInternalError(err ResultError, context *JsonContext, value interface{}, details ErrorDetails) {
newError(err, context, value, Locale, details)
v.errors = append(v.errors, err)
v.score -= 2 // results in a net -1 when added to the +1 we get at the end of the validation function
diff --git a/validation.go b/validation.go
index 3a1ae26..54fc7a7 100644
--- a/validation.go
+++ b/validation.go
@@ -71,14 +71,14 @@
}
-func (v *subSchema) subValidateWithContext(document interface{}, context *jsonContext) *Result {
+func (v *subSchema) subValidateWithContext(document interface{}, context *JsonContext) *Result {
result := &Result{}
v.validateRecursive(v, document, result, context)
return result
}
// Walker function to validate the json recursively against the subSchema
-func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *JsonContext) {
if internalLogEnabled {
internalLog("validateRecursive %s", context.String())
@@ -264,7 +264,7 @@
}
// Different kinds of validation there, subSchema / common / array / object / string...
-func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *JsonContext) {
if internalLogEnabled {
internalLog("validateSchema %s", context.String())
@@ -396,7 +396,7 @@
result.incrementScore()
}
-func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) {
if internalLogEnabled {
internalLog("validateCommon %s", context.String())
@@ -424,7 +424,7 @@
result.incrementScore()
}
-func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface{}, result *Result, context *JsonContext) {
if internalLogEnabled {
internalLog("validateArray %s", context.String())
@@ -518,7 +518,7 @@
result.incrementScore()
}
-func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *JsonContext) {
if internalLogEnabled {
internalLog("validateObject %s", context.String())
@@ -664,7 +664,7 @@
result.incrementScore()
}
-func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *jsonContext) (has bool, matched bool) {
+func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *JsonContext) (has bool, matched bool) {
if internalLogEnabled {
internalLog("validatePatternProperty %s", context.String())
@@ -696,7 +696,7 @@
return has, true
}
-func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) {
// Ignore JSON numbers
if isJsonNumber(value) {
@@ -765,7 +765,7 @@
result.incrementScore()
}
-func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
+func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) {
// Ignore non numbers
if !isJsonNumber(value) {