locales.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Contains const string and messages.
  22. //
  23. // created 01-01-2015
  24. package gojsonschema
  25. type (
  26. // locale is an interface for defining custom error strings
  27. locale interface {
  28. Required() string
  29. InvalidType() string
  30. NumberAnyOf() string
  31. NumberOneOf() string
  32. NumberAllOf() string
  33. NumberNot() string
  34. MissingDependency() string
  35. Internal() string
  36. Enum() string
  37. ArrayNotEnoughItems() string
  38. ArrayNoAdditionalItems() string
  39. ArrayMinItems() string
  40. ArrayMaxItems() string
  41. Unique() string
  42. ArrayMinProperties() string
  43. ArrayMaxProperties() string
  44. AdditionalPropertyNotAllowed() string
  45. InvalidPropertyPattern() string
  46. StringGTE() string
  47. StringLTE() string
  48. DoesNotMatchPattern() string
  49. DoesNotMatchFormat() string
  50. MultipleOf() string
  51. NumberGTE() string
  52. NumberGT() string
  53. NumberLTE() string
  54. NumberLT() string
  55. // Schema validations
  56. RegexPattern() string
  57. GreaterThanZero() string
  58. MustBeOfA() string
  59. MustBeOfAn() string
  60. CannotBeUsedWithout() string
  61. CannotBeGT() string
  62. MustBeOfType() string
  63. MustBeValidRegex() string
  64. MustBeValidFormat() string
  65. MustBeGTEZero() string
  66. KeyCannotBeGreaterThan() string
  67. KeyItemsMustBeOfType() string
  68. KeyItemsMustBeUnique() string
  69. ReferenceMustBeCanonical() string
  70. NotAValidType() string
  71. Duplicated() string
  72. HttpBadStatus() string
  73. ParseError() string
  74. // ErrorFormat
  75. ErrorFormat() string
  76. }
  77. // DefaultLocale is the default locale for this package
  78. DefaultLocale struct{}
  79. )
  80. func (l DefaultLocale) Required() string {
  81. return `{{.property}} is required`
  82. }
  83. func (l DefaultLocale) InvalidType() string {
  84. return `Invalid type. Expected: {{.expected}}, given: {{.given}}`
  85. }
  86. func (l DefaultLocale) NumberAnyOf() string {
  87. return `Must validate at least one schema (anyOf)`
  88. }
  89. func (l DefaultLocale) NumberOneOf() string {
  90. return `Must validate one and only one schema (oneOf)`
  91. }
  92. func (l DefaultLocale) NumberAllOf() string {
  93. return `Must validate all the schemas (allOf)`
  94. }
  95. func (l DefaultLocale) NumberNot() string {
  96. return `Must not validate the schema (not)`
  97. }
  98. func (l DefaultLocale) MissingDependency() string {
  99. return `Has a dependency on {{.dependency}}`
  100. }
  101. func (l DefaultLocale) Internal() string {
  102. return `Internal Error {{.error}}`
  103. }
  104. func (l DefaultLocale) Enum() string {
  105. return `{{.field}} must be one of the following: {{.allowed}}`
  106. }
  107. func (l DefaultLocale) ArrayNoAdditionalItems() string {
  108. return `No additional items allowed on array`
  109. }
  110. func (l DefaultLocale) ArrayNotEnoughItems() string {
  111. return `Not enough items on array to match positional list of schema`
  112. }
  113. func (l DefaultLocale) ArrayMinItems() string {
  114. return `Array must have at least {{.min}} items`
  115. }
  116. func (l DefaultLocale) ArrayMaxItems() string {
  117. return `Array must have at most {{.max}} items`
  118. }
  119. func (l DefaultLocale) Unique() string {
  120. return `{{.type}} items must be unique`
  121. }
  122. func (l DefaultLocale) ArrayMinProperties() string {
  123. return `Must have at least {{.min}} properties`
  124. }
  125. func (l DefaultLocale) ArrayMaxProperties() string {
  126. return `Must have at most {{.max}} properties`
  127. }
  128. func (l DefaultLocale) AdditionalPropertyNotAllowed() string {
  129. return `Additional property {{.property}} is not allowed`
  130. }
  131. func (l DefaultLocale) InvalidPropertyPattern() string {
  132. return `Property "{{.property}}" does not match pattern {{.pattern}}`
  133. }
  134. func (l DefaultLocale) StringGTE() string {
  135. return `String length must be greater than or equal to {{.min}}`
  136. }
  137. func (l DefaultLocale) StringLTE() string {
  138. return `String length must be less than or equal to {{.max}}`
  139. }
  140. func (l DefaultLocale) DoesNotMatchPattern() string {
  141. return `Does not match pattern '{{.pattern}}'`
  142. }
  143. func (l DefaultLocale) DoesNotMatchFormat() string {
  144. return `Does not match format '{{.format}}'`
  145. }
  146. func (l DefaultLocale) MultipleOf() string {
  147. return `Must be a multiple of {{.multiple}}`
  148. }
  149. func (l DefaultLocale) NumberGTE() string {
  150. return `Must be greater than or equal to {{.min}}`
  151. }
  152. func (l DefaultLocale) NumberGT() string {
  153. return `Must be greater than {{.min}}`
  154. }
  155. func (l DefaultLocale) NumberLTE() string {
  156. return `Must be less than or equal to {{.max}}`
  157. }
  158. func (l DefaultLocale) NumberLT() string {
  159. return `Must be less than {{.max}}`
  160. }
  161. // Schema validators
  162. func (l DefaultLocale) RegexPattern() string {
  163. return `Invalid regex pattern '{{.pattern}}'`
  164. }
  165. func (l DefaultLocale) GreaterThanZero() string {
  166. return `{{.number}} must be strictly greater than 0`
  167. }
  168. func (l DefaultLocale) MustBeOfA() string {
  169. return `{{.x}} must be of a {{.y}}`
  170. }
  171. func (l DefaultLocale) MustBeOfAn() string {
  172. return `{{.x}} must be of an {{.y}}`
  173. }
  174. func (l DefaultLocale) CannotBeUsedWithout() string {
  175. return `{{.x}} cannot be used without {{.y}}`
  176. }
  177. func (l DefaultLocale) CannotBeGT() string {
  178. return `{{.x}} cannot be greater than {{.y}}`
  179. }
  180. func (l DefaultLocale) MustBeOfType() string {
  181. return `{{.key}} must be of type {{.type}}`
  182. }
  183. func (l DefaultLocale) MustBeValidRegex() string {
  184. return `{{.key}} must be a valid regex`
  185. }
  186. func (l DefaultLocale) MustBeValidFormat() string {
  187. return `{{.key}} must be a valid format {{.given}}`
  188. }
  189. func (l DefaultLocale) MustBeGTEZero() string {
  190. return `{{.key}} must be greater than or equal to 0`
  191. }
  192. func (l DefaultLocale) KeyCannotBeGreaterThan() string {
  193. return `{{.key}} cannot be greater than {{.y}}`
  194. }
  195. func (l DefaultLocale) KeyItemsMustBeOfType() string {
  196. return `{{.key}} items must be {{.type}}`
  197. }
  198. func (l DefaultLocale) KeyItemsMustBeUnique() string {
  199. return `{{.key}} items must be unique`
  200. }
  201. func (l DefaultLocale) ReferenceMustBeCanonical() string {
  202. return `Reference {{.reference}} must be canonical`
  203. }
  204. func (l DefaultLocale) NotAValidType() string {
  205. return `has a primitive type that is NOT VALID -- given: {{.given}} Expected valid values are:{{.expected}}`
  206. }
  207. func (l DefaultLocale) Duplicated() string {
  208. return `{{.type}} type is duplicated`
  209. }
  210. func (l DefaultLocale) HttpBadStatus() string {
  211. return `Could not read schema from HTTP, response status is {{.status}}`
  212. }
  213. // Replacement options: field, description, context, value
  214. func (l DefaultLocale) ErrorFormat() string {
  215. return `{{.field}}: {{.description}}`
  216. }
  217. //Parse error
  218. func (l DefaultLocale) ParseError() string {
  219. return `Expected: %expected%, given: Invalid JSON`
  220. }
  221. const (
  222. STRING_NUMBER = "number"
  223. STRING_ARRAY_OF_STRINGS = "array of strings"
  224. STRING_ARRAY_OF_SCHEMAS = "array of schemas"
  225. STRING_SCHEMA = "schema"
  226. STRING_SCHEMA_OR_ARRAY_OF_STRINGS = "schema or array of strings"
  227. STRING_PROPERTIES = "properties"
  228. STRING_DEPENDENCY = "dependency"
  229. STRING_PROPERTY = "property"
  230. STRING_UNDEFINED = "undefined"
  231. STRING_CONTEXT_ROOT = "(root)"
  232. STRING_ROOT_SCHEMA_PROPERTY = "(root)"
  233. )