result.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. String() string
  47. }
  48. // ResultErrorFields holds the fields for each ResultError implementation.
  49. // ResultErrorFields implements the ResultError interface, so custom errors
  50. // can be defined by just embedding this type
  51. ResultErrorFields struct {
  52. errorType string // A string with the type of error (i.e. invalid_type)
  53. context *jsonContext // Tree like notation of the part that failed the validation. ex (root).a.b ...
  54. description string // A human readable error message
  55. value interface{} // Value given by the JSON file that is the source of the error
  56. details ErrorDetails
  57. }
  58. Result struct {
  59. errors []ResultError
  60. // Scores how well the validation matched. Useful in generating
  61. // better error messages for anyOf and oneOf.
  62. score int
  63. }
  64. )
  65. // Field outputs the field name without the root context
  66. // i.e. firstName or person.firstName instead of (root).firstName or (root).person.firstName
  67. func (v *ResultErrorFields) Field() string {
  68. if p, ok := v.Details()["property"]; ok {
  69. if str, isString := p.(string); isString {
  70. return str
  71. }
  72. }
  73. return strings.TrimPrefix(v.context.String(), STRING_ROOT_SCHEMA_PROPERTY+".")
  74. }
  75. func (v *ResultErrorFields) SetType(errorType string) {
  76. v.errorType = errorType
  77. }
  78. func (v *ResultErrorFields) Type() string {
  79. return v.errorType
  80. }
  81. func (v *ResultErrorFields) SetContext(context *jsonContext) {
  82. v.context = context
  83. }
  84. func (v *ResultErrorFields) Context() *jsonContext {
  85. return v.context
  86. }
  87. func (v *ResultErrorFields) SetDescription(description string) {
  88. v.description = description
  89. }
  90. func (v *ResultErrorFields) Description() string {
  91. return v.description
  92. }
  93. func (v *ResultErrorFields) SetValue(value interface{}) {
  94. v.value = value
  95. }
  96. func (v *ResultErrorFields) Value() interface{} {
  97. return v.value
  98. }
  99. func (v *ResultErrorFields) SetDetails(details ErrorDetails) {
  100. v.details = details
  101. }
  102. func (v *ResultErrorFields) Details() ErrorDetails {
  103. return v.details
  104. }
  105. func (v ResultErrorFields) String() string {
  106. // as a fallback, the value is displayed go style
  107. valueString := fmt.Sprintf("%v", v.value)
  108. // marshal the go value value to json
  109. if v.value == nil {
  110. valueString = TYPE_NULL
  111. } else {
  112. if vs, err := marshalToJsonString(v.value); err == nil {
  113. if vs == nil {
  114. valueString = TYPE_NULL
  115. } else {
  116. valueString = *vs
  117. }
  118. }
  119. }
  120. return formatErrorDescription(Locale.ErrorFormat(), ErrorDetails{
  121. "context": v.context.String(),
  122. "description": v.description,
  123. "value": valueString,
  124. "field": v.Field(),
  125. })
  126. }
  127. func (v *Result) Valid() bool {
  128. return len(v.errors) == 0
  129. }
  130. func (v *Result) Errors() []ResultError {
  131. return v.errors
  132. }
  133. func (v *Result) addError(err ResultError, context *jsonContext, value interface{}, details ErrorDetails) {
  134. newError(err, context, value, Locale, details)
  135. v.errors = append(v.errors, err)
  136. v.score -= 2 // results in a net -1 when added to the +1 we get at the end of the validation function
  137. }
  138. // Used to copy errors from a sub-schema to the main one
  139. func (v *Result) mergeErrors(otherResult *Result) {
  140. v.errors = append(v.errors, otherResult.Errors()...)
  141. v.score += otherResult.score
  142. }
  143. func (v *Result) incrementScore() {
  144. v.score++
  145. }