errors.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package gojsonschema
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type (
  7. // RequiredError. ErrorDetails: property string
  8. RequiredError struct {
  9. ResultErrorFields
  10. }
  11. // InvalidTypeError. ErrorDetails: expected, given
  12. InvalidTypeError struct {
  13. ResultErrorFields
  14. }
  15. // NumberAnyOfError. ErrorDetails: -
  16. NumberAnyOfError struct {
  17. ResultErrorFields
  18. }
  19. // NumberOneOfError. ErrorDetails: -
  20. NumberOneOfError struct {
  21. ResultErrorFields
  22. }
  23. // NumberAllOfError. ErrorDetails: -
  24. NumberAllOfError struct {
  25. ResultErrorFields
  26. }
  27. // NumberNotError. ErrorDetails: -
  28. NumberNotError struct {
  29. ResultErrorFields
  30. }
  31. // MissingDependencyError. ErrorDetails: dependency
  32. MissingDependencyError struct {
  33. ResultErrorFields
  34. }
  35. // InternalError. ErrorDetails: error
  36. InternalError struct {
  37. ResultErrorFields
  38. }
  39. // EnumError. ErrorDetails: allowed
  40. EnumError struct {
  41. ResultErrorFields
  42. }
  43. // ArrayNoAdditionalItemsError. ErrorDetails: -
  44. ArrayNoAdditionalItemsError struct {
  45. ResultErrorFields
  46. }
  47. // ArrayMinItemsError. ErrorDetails: min
  48. ArrayMinItemsError struct {
  49. ResultErrorFields
  50. }
  51. // ArrayMaxItemsError. ErrorDetails: max
  52. ArrayMaxItemsError struct {
  53. ResultErrorFields
  54. }
  55. // ItemsMustBeUniqueError. ErrorDetails: type
  56. ItemsMustBeUniqueError struct {
  57. ResultErrorFields
  58. }
  59. // ArrayMinPropertiesError. ErrorDetails: min
  60. ArrayMinPropertiesError struct {
  61. ResultErrorFields
  62. }
  63. // ArrayMaxPropertiesError. ErrorDetails: max
  64. ArrayMaxPropertiesError struct {
  65. ResultErrorFields
  66. }
  67. // AdditionalPropertyNotAllowedError. ErrorDetails: property
  68. AdditionalPropertyNotAllowedError struct {
  69. ResultErrorFields
  70. }
  71. // InvalidPropertyPatternError. ErrorDetails: property, pattern
  72. InvalidPropertyPatternError struct {
  73. ResultErrorFields
  74. }
  75. // StringLengthGTEError. ErrorDetails: min
  76. StringLengthGTEError struct {
  77. ResultErrorFields
  78. }
  79. // StringLengthLTEError. ErrorDetails: max
  80. StringLengthLTEError struct {
  81. ResultErrorFields
  82. }
  83. // DoesNotMatchPatternError. ErrorDetails: pattern
  84. DoesNotMatchPatternError struct {
  85. ResultErrorFields
  86. }
  87. // DoesNotMatchFormatError. ErrorDetails: format
  88. DoesNotMatchFormatError struct {
  89. ResultErrorFields
  90. }
  91. // MultipleOfError. ErrorDetails: multiple
  92. MultipleOfError struct {
  93. ResultErrorFields
  94. }
  95. // NumberGTEError. ErrorDetails: min
  96. NumberGTEError struct {
  97. ResultErrorFields
  98. }
  99. // NumberGTError. ErrorDetails: min
  100. NumberGTError struct {
  101. ResultErrorFields
  102. }
  103. // NumberLTEError. ErrorDetails: max
  104. NumberLTEError struct {
  105. ResultErrorFields
  106. }
  107. // NumberLTError. ErrorDetails: max
  108. NumberLTError struct {
  109. ResultErrorFields
  110. }
  111. )
  112. // newError takes a ResultError type and sets the type, context, description, details, value, and field
  113. func newError(err ResultError, context *jsonContext, value interface{}, locale locale, details ErrorDetails) {
  114. var t string
  115. var d string
  116. switch err.(type) {
  117. case *RequiredError:
  118. t = "required"
  119. d = locale.Required()
  120. case *InvalidTypeError:
  121. t = "invalid_type"
  122. d = locale.InvalidType()
  123. case *NumberAnyOfError:
  124. t = "number_any_of"
  125. d = locale.NumberAnyOf()
  126. case *NumberOneOfError:
  127. t = "number_one_of"
  128. d = locale.NumberOneOf()
  129. case *NumberAllOfError:
  130. t = "number_all_of"
  131. d = locale.NumberAllOf()
  132. case *NumberNotError:
  133. t = "number_not"
  134. d = locale.NumberNot()
  135. case *MissingDependencyError:
  136. t = "missing_dependency"
  137. d = locale.MissingDependency()
  138. case *InternalError:
  139. t = "internal"
  140. d = locale.Internal()
  141. case *EnumError:
  142. t = "enum"
  143. d = locale.Enum()
  144. case *ArrayNoAdditionalItemsError:
  145. t = "array_no_additional_items"
  146. d = locale.ArrayNoAdditionalItems()
  147. case *ArrayMinItemsError:
  148. t = "array_min_items"
  149. d = locale.ArrayMinItems()
  150. case *ArrayMaxItemsError:
  151. t = "array_max_items"
  152. d = locale.ArrayMaxItems()
  153. case *ItemsMustBeUniqueError:
  154. t = "unique"
  155. d = locale.Unique()
  156. case *ArrayMinPropertiesError:
  157. t = "array_min_properties"
  158. d = locale.ArrayMinProperties()
  159. case *ArrayMaxPropertiesError:
  160. t = "array_max_properties"
  161. d = locale.ArrayMaxProperties()
  162. case *AdditionalPropertyNotAllowedError:
  163. t = "additional_property_not_allowed"
  164. d = locale.AdditionalPropertyNotAllowed()
  165. case *InvalidPropertyPatternError:
  166. t = "invalid_property_pattern"
  167. d = locale.InvalidPropertyPattern()
  168. case *StringLengthGTEError:
  169. t = "string_gte"
  170. d = locale.StringGTE()
  171. case *StringLengthLTEError:
  172. t = "string_lte"
  173. d = locale.StringLTE()
  174. case *DoesNotMatchPatternError:
  175. t = "pattern"
  176. d = locale.DoesNotMatchPattern()
  177. case *DoesNotMatchFormatError:
  178. t = "format"
  179. d = locale.DoesNotMatchFormat()
  180. case *MultipleOfError:
  181. t = "multiple_of"
  182. d = locale.MultipleOf()
  183. case *NumberGTEError:
  184. t = "number_gte"
  185. d = locale.NumberGTE()
  186. case *NumberGTError:
  187. t = "number_gt"
  188. d = locale.NumberGT()
  189. case *NumberLTEError:
  190. t = "number_lte"
  191. d = locale.NumberLTE()
  192. case *NumberLTError:
  193. t = "number_lt"
  194. d = locale.NumberLT()
  195. }
  196. err.SetType(t)
  197. err.SetContext(context)
  198. err.SetValue(value)
  199. err.SetDetails(details)
  200. details["field"] = err.Field()
  201. err.SetDescription(formatErrorDescription(d, details))
  202. }
  203. // formatErrorDescription takes a string in this format: %field% is required
  204. // and converts it to a string with replacements. The fields come from
  205. // the ErrorDetails struct and vary for each type of error.
  206. func formatErrorDescription(s string, details ErrorDetails) string {
  207. for name, val := range details {
  208. s = strings.Replace(s, "%"+strings.ToLower(name)+"%", fmt.Sprintf("%v", val), -1)
  209. }
  210. return s
  211. }