result.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2015 xeipuuv ( https://github.com/xeipuuv )
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // author xeipuuv
  15. // author-github https://github.com/xeipuuv
  16. // author-mail [email protected]
  17. //
  18. // repository-name gojsonschema
  19. // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
  20. //
  21. // description Result and ResultError implementations.
  22. //
  23. // created 01-01-2015
  24. package gojsonschema
  25. import (
  26. "fmt"
  27. "strings"
  28. )
  29. type (
  30. // ErrorDetails is a map of details specific to each error.
  31. // While the values will vary, every error will contain a "field" value
  32. ErrorDetails map[string]interface{}
  33. // ResultError is the interface that library errors must implement
  34. ResultError interface {
  35. Field() string
  36. SetType(string)
  37. Type() string
  38. SetContext(*jsonContext)
  39. Context() *jsonContext
  40. SetDescription(string)
  41. Description() string
  42. SetValue(interface{})
  43. Value() interface{}
  44. SetDetails(ErrorDetails)
  45. Details() ErrorDetails
  46. }
  47. // ResultErrorFields holds the fields for each ResultError implementation.
  48. // ResultErrorFields implements the ResultError interface, so custom errors
  49. // can be defined by just embedding this type
  50. ResultErrorFields struct {
  51. errorType string // A string with the type of error (i.e. invalid_type)
  52. context *jsonContext // Tree like notation of the part that failed the validation. ex (root).a.b ...
  53. description string // A human readable error message
  54. value interface{} // Value given by the JSON file that is the source of the error
  55. details ErrorDetails
  56. }
  57. Result struct {
  58. errors []ResultError
  59. // Scores how well the validation matched. Useful in generating
  60. // better error messages for anyOf and oneOf.
  61. score int
  62. }
  63. )
  64. // Field outputs the field name without the root context
  65. // i.e. firstName or person.firstName instead of (root).firstName or (root).person.firstName
  66. func (v *ResultErrorFields) Field() string {
  67. if p, ok := v.Details()["property"]; ok {
  68. if str, isString := p.(string); isString {
  69. return str
  70. }
  71. }
  72. return strings.TrimPrefix(v.context.String(), STRING_ROOT_SCHEMA_PROPERTY+".")
  73. }
  74. func (v *ResultErrorFields) SetType(errorType string) {
  75. v.errorType = errorType
  76. }
  77. func (v *ResultErrorFields) Type() string {
  78. return v.errorType
  79. }
  80. func (v *ResultErrorFields) SetContext(context *jsonContext) {
  81. v.context = context
  82. }
  83. func (v *ResultErrorFields) Context() *jsonContext {
  84. return v.context
  85. }
  86. func (v *ResultErrorFields) SetDescription(description string) {
  87. v.description = description
  88. }
  89. func (v *ResultErrorFields) Description() string {
  90. return v.description
  91. }
  92. func (v *ResultErrorFields) SetValue(value interface{}) {
  93. v.value = value
  94. }
  95. func (v *ResultErrorFields) Value() interface{} {
  96. return v.value
  97. }
  98. func (v *ResultErrorFields) SetDetails(details ErrorDetails) {
  99. v.details = details
  100. }
  101. func (v *ResultErrorFields) Details() ErrorDetails {
  102. return v.details
  103. }
  104. func (v ResultErrorFields) String() string {
  105. // as a fallback, the value is displayed go style
  106. valueString := fmt.Sprintf("%v", v.value)
  107. // marshal the go value value to json
  108. if v.Value == nil {
  109. valueString = TYPE_NULL
  110. } else {
  111. if vs, err := marshalToJsonString(v.value); err == nil {
  112. if vs == nil {
  113. valueString = TYPE_NULL
  114. } else {
  115. valueString = *vs
  116. }
  117. }
  118. }
  119. return formatErrorDescription(Locale.ErrorFormat(), ErrorDetails{
  120. "context": v.context.String(),
  121. "description": v.description,
  122. "value": valueString,
  123. "field": v.Field(),
  124. })
  125. }
  126. func (v *Result) Valid() bool {
  127. return len(v.errors) == 0
  128. }
  129. func (v *Result) Errors() []ResultError {
  130. return v.errors
  131. }
  132. func (v *Result) addError(err ResultError, context *jsonContext, value interface{}, details ErrorDetails) {
  133. newError(err, context, value, Locale, details)
  134. v.errors = append(v.errors, err)
  135. v.score -= 2 // results in a net -1 when added to the +1 we get at the end of the validation function
  136. }
  137. // Used to copy errors from a sub-schema to the main one
  138. func (v *Result) mergeErrors(otherResult *Result) {
  139. v.errors = append(v.errors, otherResult.Errors()...)
  140. v.score += otherResult.score
  141. }
  142. func (v *Result) incrementScore() {
  143. v.score++
  144. }