2017-04-17 18:08:24 -04:00
|
|
|
package gojsonschema
|
|
|
|
|
|
|
|
import (
|
2019-04-12 20:56:09 -04:00
|
|
|
"bytes"
|
|
|
|
"sync"
|
|
|
|
"text/template"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
var errorTemplates errorTemplate = errorTemplate{template.New("errors-new"), sync.RWMutex{}}
|
|
|
|
|
|
|
|
// template.Template is not thread-safe for writing, so some locking is done
|
|
|
|
// sync.RWMutex is used for efficiently locking when new templates are created
|
|
|
|
type errorTemplate struct {
|
|
|
|
*template.Template
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
type (
|
|
|
|
// RequiredError. ErrorDetails: property string
|
|
|
|
RequiredError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidTypeError. ErrorDetails: expected, given
|
|
|
|
InvalidTypeError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberAnyOfError. ErrorDetails: -
|
|
|
|
NumberAnyOfError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOneOfError. ErrorDetails: -
|
|
|
|
NumberOneOfError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberAllOfError. ErrorDetails: -
|
|
|
|
NumberAllOfError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberNotError. ErrorDetails: -
|
|
|
|
NumberNotError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// MissingDependencyError. ErrorDetails: dependency
|
|
|
|
MissingDependencyError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// InternalError. ErrorDetails: error
|
|
|
|
InternalError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// ConstError. ErrorDetails: allowed
|
|
|
|
ConstError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// EnumError. ErrorDetails: allowed
|
|
|
|
EnumError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayNoAdditionalItemsError. ErrorDetails: -
|
|
|
|
ArrayNoAdditionalItemsError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayMinItemsError. ErrorDetails: min
|
|
|
|
ArrayMinItemsError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayMaxItemsError. ErrorDetails: max
|
|
|
|
ArrayMaxItemsError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// ItemsMustBeUniqueError. ErrorDetails: type, i, j
|
2017-04-17 18:08:24 -04:00
|
|
|
ItemsMustBeUniqueError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// ArrayContainsError. ErrorDetails:
|
|
|
|
ArrayContainsError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// ArrayMinPropertiesError. ErrorDetails: min
|
|
|
|
ArrayMinPropertiesError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayMaxPropertiesError. ErrorDetails: max
|
|
|
|
ArrayMaxPropertiesError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// AdditionalPropertyNotAllowedError. ErrorDetails: property
|
|
|
|
AdditionalPropertyNotAllowedError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidPropertyPatternError. ErrorDetails: property, pattern
|
|
|
|
InvalidPropertyPatternError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// InvalidPopertyNameError. ErrorDetails: property
|
|
|
|
InvalidPropertyNameError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// StringLengthGTEError. ErrorDetails: min
|
|
|
|
StringLengthGTEError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// StringLengthLTEError. ErrorDetails: max
|
|
|
|
StringLengthLTEError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoesNotMatchPatternError. ErrorDetails: pattern
|
|
|
|
DoesNotMatchPatternError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoesNotMatchFormatError. ErrorDetails: format
|
|
|
|
DoesNotMatchFormatError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultipleOfError. ErrorDetails: multiple
|
|
|
|
MultipleOfError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberGTEError. ErrorDetails: min
|
|
|
|
NumberGTEError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberGTError. ErrorDetails: min
|
|
|
|
NumberGTError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberLTEError. ErrorDetails: max
|
|
|
|
NumberLTEError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumberLTError. ErrorDetails: max
|
|
|
|
NumberLTError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
2019-04-12 20:56:09 -04:00
|
|
|
|
|
|
|
// ConditionThenError. ErrorDetails: -
|
|
|
|
ConditionThenError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConditionElseError. ErrorDetails: -
|
|
|
|
ConditionElseError struct {
|
|
|
|
ResultErrorFields
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// newError takes a ResultError type and sets the type, context, description, details, value, and field
|
2019-04-12 20:56:09 -04:00
|
|
|
func newError(err ResultError, context *JsonContext, value interface{}, locale locale, details ErrorDetails) {
|
2017-04-17 18:08:24 -04:00
|
|
|
var t string
|
|
|
|
var d string
|
|
|
|
switch err.(type) {
|
|
|
|
case *RequiredError:
|
|
|
|
t = "required"
|
|
|
|
d = locale.Required()
|
|
|
|
case *InvalidTypeError:
|
|
|
|
t = "invalid_type"
|
|
|
|
d = locale.InvalidType()
|
|
|
|
case *NumberAnyOfError:
|
|
|
|
t = "number_any_of"
|
|
|
|
d = locale.NumberAnyOf()
|
|
|
|
case *NumberOneOfError:
|
|
|
|
t = "number_one_of"
|
|
|
|
d = locale.NumberOneOf()
|
|
|
|
case *NumberAllOfError:
|
|
|
|
t = "number_all_of"
|
|
|
|
d = locale.NumberAllOf()
|
|
|
|
case *NumberNotError:
|
|
|
|
t = "number_not"
|
|
|
|
d = locale.NumberNot()
|
|
|
|
case *MissingDependencyError:
|
|
|
|
t = "missing_dependency"
|
|
|
|
d = locale.MissingDependency()
|
|
|
|
case *InternalError:
|
|
|
|
t = "internal"
|
|
|
|
d = locale.Internal()
|
2019-04-12 20:56:09 -04:00
|
|
|
case *ConstError:
|
|
|
|
t = "const"
|
|
|
|
d = locale.Const()
|
2017-04-17 18:08:24 -04:00
|
|
|
case *EnumError:
|
|
|
|
t = "enum"
|
|
|
|
d = locale.Enum()
|
|
|
|
case *ArrayNoAdditionalItemsError:
|
|
|
|
t = "array_no_additional_items"
|
|
|
|
d = locale.ArrayNoAdditionalItems()
|
|
|
|
case *ArrayMinItemsError:
|
|
|
|
t = "array_min_items"
|
|
|
|
d = locale.ArrayMinItems()
|
|
|
|
case *ArrayMaxItemsError:
|
|
|
|
t = "array_max_items"
|
|
|
|
d = locale.ArrayMaxItems()
|
|
|
|
case *ItemsMustBeUniqueError:
|
|
|
|
t = "unique"
|
|
|
|
d = locale.Unique()
|
2019-04-12 20:56:09 -04:00
|
|
|
case *ArrayContainsError:
|
|
|
|
t = "contains"
|
|
|
|
d = locale.ArrayContains()
|
2017-04-17 18:08:24 -04:00
|
|
|
case *ArrayMinPropertiesError:
|
|
|
|
t = "array_min_properties"
|
|
|
|
d = locale.ArrayMinProperties()
|
|
|
|
case *ArrayMaxPropertiesError:
|
|
|
|
t = "array_max_properties"
|
|
|
|
d = locale.ArrayMaxProperties()
|
|
|
|
case *AdditionalPropertyNotAllowedError:
|
|
|
|
t = "additional_property_not_allowed"
|
|
|
|
d = locale.AdditionalPropertyNotAllowed()
|
|
|
|
case *InvalidPropertyPatternError:
|
|
|
|
t = "invalid_property_pattern"
|
|
|
|
d = locale.InvalidPropertyPattern()
|
2019-04-12 20:56:09 -04:00
|
|
|
case *InvalidPropertyNameError:
|
|
|
|
t = "invalid_property_name"
|
|
|
|
d = locale.InvalidPropertyName()
|
2017-04-17 18:08:24 -04:00
|
|
|
case *StringLengthGTEError:
|
|
|
|
t = "string_gte"
|
|
|
|
d = locale.StringGTE()
|
|
|
|
case *StringLengthLTEError:
|
|
|
|
t = "string_lte"
|
|
|
|
d = locale.StringLTE()
|
|
|
|
case *DoesNotMatchPatternError:
|
|
|
|
t = "pattern"
|
|
|
|
d = locale.DoesNotMatchPattern()
|
|
|
|
case *DoesNotMatchFormatError:
|
|
|
|
t = "format"
|
|
|
|
d = locale.DoesNotMatchFormat()
|
|
|
|
case *MultipleOfError:
|
|
|
|
t = "multiple_of"
|
|
|
|
d = locale.MultipleOf()
|
|
|
|
case *NumberGTEError:
|
|
|
|
t = "number_gte"
|
|
|
|
d = locale.NumberGTE()
|
|
|
|
case *NumberGTError:
|
|
|
|
t = "number_gt"
|
|
|
|
d = locale.NumberGT()
|
|
|
|
case *NumberLTEError:
|
|
|
|
t = "number_lte"
|
|
|
|
d = locale.NumberLTE()
|
|
|
|
case *NumberLTError:
|
|
|
|
t = "number_lt"
|
|
|
|
d = locale.NumberLT()
|
2019-04-12 20:56:09 -04:00
|
|
|
case *ConditionThenError:
|
|
|
|
t = "condition_then"
|
|
|
|
d = locale.ConditionThen()
|
|
|
|
case *ConditionElseError:
|
|
|
|
t = "condition_else"
|
|
|
|
d = locale.ConditionElse()
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err.SetType(t)
|
|
|
|
err.SetContext(context)
|
|
|
|
err.SetValue(value)
|
|
|
|
err.SetDetails(details)
|
2019-04-12 20:56:09 -04:00
|
|
|
err.SetDescriptionFormat(d)
|
2017-04-17 18:08:24 -04:00
|
|
|
details["field"] = err.Field()
|
2019-04-12 20:56:09 -04:00
|
|
|
|
|
|
|
if _, exists := details["context"]; !exists && context != nil {
|
|
|
|
details["context"] = context.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
err.SetDescription(formatErrorDescription(err.DescriptionFormat(), details))
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// formatErrorDescription takes a string in the default text/template
|
|
|
|
// format and converts it to a string with replacements. The fields come
|
|
|
|
// from the ErrorDetails struct and vary for each type of error.
|
2017-04-17 18:08:24 -04:00
|
|
|
func formatErrorDescription(s string, details ErrorDetails) string {
|
2019-04-12 20:56:09 -04:00
|
|
|
|
|
|
|
var tpl *template.Template
|
|
|
|
var descrAsBuffer bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
|
|
|
errorTemplates.RLock()
|
|
|
|
tpl = errorTemplates.Lookup(s)
|
|
|
|
errorTemplates.RUnlock()
|
|
|
|
|
|
|
|
if tpl == nil {
|
|
|
|
errorTemplates.Lock()
|
|
|
|
tpl = errorTemplates.New(s)
|
|
|
|
|
|
|
|
if ErrorTemplateFuncs != nil {
|
|
|
|
tpl.Funcs(ErrorTemplateFuncs)
|
|
|
|
}
|
|
|
|
|
|
|
|
tpl, err = tpl.Parse(s)
|
|
|
|
errorTemplates.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tpl.Execute(&descrAsBuffer, details)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
return descrAsBuffer.String()
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|