locales.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 xeipuuv@gmail.com
  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. // False returns a format-string for "false" schema validation errors
  29. False() string
  30. // Required returns a format-string for "required" schema validation errors
  31. Required() string
  32. // InvalidType returns a format-string for "invalid type" schema validation errors
  33. InvalidType() string
  34. // NumberAnyOf returns a format-string for "anyOf" schema validation errors
  35. NumberAnyOf() string
  36. // NumberOneOf returns a format-string for "oneOf" schema validation errors
  37. NumberOneOf() string
  38. // NumberAllOf returns a format-string for "allOf" schema validation errors
  39. NumberAllOf() string
  40. // NumberNot returns a format-string to format a NumberNotError
  41. NumberNot() string
  42. // MissingDependency returns a format-string for "missing dependency" schema validation errors
  43. MissingDependency() string
  44. // Internal returns a format-string for internal errors
  45. Internal() string
  46. // Const returns a format-string to format a ConstError
  47. Const() string
  48. // Enum returns a format-string to format an EnumError
  49. Enum() string
  50. // ArrayNotEnoughItems returns a format-string to format an error for arrays having not enough items to match positional list of schema
  51. ArrayNotEnoughItems() string
  52. // ArrayNoAdditionalItems returns a format-string to format an ArrayNoAdditionalItemsError
  53. ArrayNoAdditionalItems() string
  54. // ArrayMinItems returns a format-string to format an ArrayMinItemsError
  55. ArrayMinItems() string
  56. // ArrayMaxItems returns a format-string to format an ArrayMaxItemsError
  57. ArrayMaxItems() string
  58. // Unique returns a format-string to format an ItemsMustBeUniqueError
  59. Unique() string
  60. // ArrayContains returns a format-string to format an ArrayContainsError
  61. ArrayContains() string
  62. // ArrayMinProperties returns a format-string to format an ArrayMinPropertiesError
  63. ArrayMinProperties() string
  64. // ArrayMaxProperties returns a format-string to format an ArrayMaxPropertiesError
  65. ArrayMaxProperties() string
  66. // AdditionalPropertyNotAllowed returns a format-string to format an AdditionalPropertyNotAllowedError
  67. AdditionalPropertyNotAllowed() string
  68. // InvalidPropertyPattern returns a format-string to format an InvalidPropertyPatternError
  69. InvalidPropertyPattern() string
  70. // InvalidPropertyName returns a format-string to format an InvalidPropertyNameError
  71. InvalidPropertyName() string
  72. // StringGTE returns a format-string to format an StringLengthGTEError
  73. StringGTE() string
  74. // StringLTE returns a format-string to format an StringLengthLTEError
  75. StringLTE() string
  76. // DoesNotMatchPattern returns a format-string to format an DoesNotMatchPatternError
  77. DoesNotMatchPattern() string
  78. // DoesNotMatchFormat returns a format-string to format an DoesNotMatchFormatError
  79. DoesNotMatchFormat() string
  80. // MultipleOf returns a format-string to format an MultipleOfError
  81. MultipleOf() string
  82. // NumberGTE returns a format-string to format an NumberGTEError
  83. NumberGTE() string
  84. // NumberGT returns a format-string to format an NumberGTError
  85. NumberGT() string
  86. // NumberLTE returns a format-string to format an NumberLTEError
  87. NumberLTE() string
  88. // NumberLT returns a format-string to format an NumberLTError
  89. NumberLT() string
  90. // Schema validations
  91. // RegexPattern returns a format-string to format a regex-pattern error
  92. RegexPattern() string
  93. // GreaterThanZero returns a format-string to format an error where a number must be greater than zero
  94. GreaterThanZero() string
  95. // MustBeOfA returns a format-string to format an error where a value is of the wrong type
  96. MustBeOfA() string
  97. // MustBeOfAn returns a format-string to format an error where a value is of the wrong type
  98. MustBeOfAn() string
  99. // CannotBeUsedWithout returns a format-string to format a "cannot be used without" error
  100. CannotBeUsedWithout() string
  101. // CannotBeGT returns a format-string to format an error where a value are greater than allowed
  102. CannotBeGT() string
  103. // MustBeOfType returns a format-string to format an error where a value does not match the required type
  104. MustBeOfType() string
  105. // MustBeValidRegex returns a format-string to format an error where a regex is invalid
  106. MustBeValidRegex() string
  107. // MustBeValidFormat returns a format-string to format an error where a value does not match the expected format
  108. MustBeValidFormat() string
  109. // MustBeGTEZero returns a format-string to format an error where a value must be greater or equal than 0
  110. MustBeGTEZero() string
  111. // KeyCannotBeGreaterThan returns a format-string to format an error where a key is greater than the maximum allowed
  112. KeyCannotBeGreaterThan() string
  113. // KeyItemsMustBeOfType returns a format-string to format an error where a key is of the wrong type
  114. KeyItemsMustBeOfType() string
  115. // KeyItemsMustBeUnique returns a format-string to format an error where keys are not unique
  116. KeyItemsMustBeUnique() string
  117. // ReferenceMustBeCanonical returns a format-string to format a "reference must be canonical" error
  118. ReferenceMustBeCanonical() string
  119. // NotAValidType returns a format-string to format an invalid type error
  120. NotAValidType() string
  121. // Duplicated returns a format-string to format an error where types are duplicated
  122. Duplicated() string
  123. // HttpBadStatus returns a format-string for errors when loading a schema using HTTP
  124. HttpBadStatus() string
  125. // ParseError returns a format-string for JSON parsing errors
  126. ParseError() string
  127. // ConditionThen returns a format-string for ConditionThenError errors
  128. ConditionThen() string
  129. // ConditionElse returns a format-string for ConditionElseError errors
  130. ConditionElse() string
  131. // ErrorFormat returns a format string for errors
  132. ErrorFormat() string
  133. }
  134. // DefaultLocale is the default locale for this package
  135. DefaultLocale struct{}
  136. )
  137. // False returns a format-string for "false" schema validation errors
  138. func (l DefaultLocale) False() string {
  139. return "False always fails validation"
  140. }
  141. // Required returns a format-string for "required" schema validation errors
  142. func (l DefaultLocale) Required() string {
  143. return `{{.property}} is required`
  144. }
  145. // InvalidType returns a format-string for "invalid type" schema validation errors
  146. func (l DefaultLocale) InvalidType() string {
  147. return `Invalid type. Expected: {{.expected}}, given: {{.given}}`
  148. }
  149. // NumberAnyOf returns a format-string for "anyOf" schema validation errors
  150. func (l DefaultLocale) NumberAnyOf() string {
  151. return `Must validate at least one schema (anyOf)`
  152. }
  153. // NumberOneOf returns a format-string for "oneOf" schema validation errors
  154. func (l DefaultLocale) NumberOneOf() string {
  155. return `Must validate one and only one schema (oneOf)`
  156. }
  157. // NumberAllOf returns a format-string for "allOf" schema validation errors
  158. func (l DefaultLocale) NumberAllOf() string {
  159. return `Must validate all the schemas (allOf)`
  160. }
  161. // NumberNot returns a format-string to format a NumberNotError
  162. func (l DefaultLocale) NumberNot() string {
  163. return `Must not validate the schema (not)`
  164. }
  165. // MissingDependency returns a format-string for "missing dependency" schema validation errors
  166. func (l DefaultLocale) MissingDependency() string {
  167. return `Has a dependency on {{.dependency}}`
  168. }
  169. // Internal returns a format-string for internal errors
  170. func (l DefaultLocale) Internal() string {
  171. return `Internal Error {{.error}}`
  172. }
  173. // Const returns a format-string to format a ConstError
  174. func (l DefaultLocale) Const() string {
  175. return `{{.field}} does not match: {{.allowed}}`
  176. }
  177. // Enum returns a format-string to format an EnumError
  178. func (l DefaultLocale) Enum() string {
  179. return `{{.field}} must be one of the following: {{.allowed}}`
  180. }
  181. // ArrayNoAdditionalItems returns a format-string to format an ArrayNoAdditionalItemsError
  182. func (l DefaultLocale) ArrayNoAdditionalItems() string {
  183. return `No additional items allowed on array`
  184. }
  185. // ArrayNotEnoughItems returns a format-string to format an error for arrays having not enough items to match positional list of schema
  186. func (l DefaultLocale) ArrayNotEnoughItems() string {
  187. return `Not enough items on array to match positional list of schema`
  188. }
  189. // ArrayMinItems returns a format-string to format an ArrayMinItemsError
  190. func (l DefaultLocale) ArrayMinItems() string {
  191. return `Array must have at least {{.min}} items`
  192. }
  193. // ArrayMaxItems returns a format-string to format an ArrayMaxItemsError
  194. func (l DefaultLocale) ArrayMaxItems() string {
  195. return `Array must have at most {{.max}} items`
  196. }
  197. // Unique returns a format-string to format an ItemsMustBeUniqueError
  198. func (l DefaultLocale) Unique() string {
  199. return `{{.type}} items[{{.i}},{{.j}}] must be unique`
  200. }
  201. // ArrayContains returns a format-string to format an ArrayContainsError
  202. func (l DefaultLocale) ArrayContains() string {
  203. return `At least one of the items must match`
  204. }
  205. // ArrayMinProperties returns a format-string to format an ArrayMinPropertiesError
  206. func (l DefaultLocale) ArrayMinProperties() string {
  207. return `Must have at least {{.min}} properties`
  208. }
  209. // ArrayMaxProperties returns a format-string to format an ArrayMaxPropertiesError
  210. func (l DefaultLocale) ArrayMaxProperties() string {
  211. return `Must have at most {{.max}} properties`
  212. }
  213. // AdditionalPropertyNotAllowed returns a format-string to format an AdditionalPropertyNotAllowedError
  214. func (l DefaultLocale) AdditionalPropertyNotAllowed() string {
  215. return `Additional property {{.property}} is not allowed`
  216. }
  217. // InvalidPropertyPattern returns a format-string to format an InvalidPropertyPatternError
  218. func (l DefaultLocale) InvalidPropertyPattern() string {
  219. return `Property "{{.property}}" does not match pattern {{.pattern}}`
  220. }
  221. // InvalidPropertyName returns a format-string to format an InvalidPropertyNameError
  222. func (l DefaultLocale) InvalidPropertyName() string {
  223. return `Property name of "{{.property}}" does not match`
  224. }
  225. // StringGTE returns a format-string to format an StringLengthGTEError
  226. func (l DefaultLocale) StringGTE() string {
  227. return `String length must be greater than or equal to {{.min}}`
  228. }
  229. // StringLTE returns a format-string to format an StringLengthLTEError
  230. func (l DefaultLocale) StringLTE() string {
  231. return `String length must be less than or equal to {{.max}}`
  232. }
  233. // DoesNotMatchPattern returns a format-string to format an DoesNotMatchPatternError
  234. func (l DefaultLocale) DoesNotMatchPattern() string {
  235. return `Does not match pattern '{{.pattern}}'`
  236. }
  237. // DoesNotMatchFormat returns a format-string to format an DoesNotMatchFormatError
  238. func (l DefaultLocale) DoesNotMatchFormat() string {
  239. return `Does not match format '{{.format}}'`
  240. }
  241. // MultipleOf returns a format-string to format an MultipleOfError
  242. func (l DefaultLocale) MultipleOf() string {
  243. return `Must be a multiple of {{.multiple}}`
  244. }
  245. // NumberGTE returns the format string to format a NumberGTEError
  246. func (l DefaultLocale) NumberGTE() string {
  247. return `Must be greater than or equal to {{.min}}`
  248. }
  249. // NumberGT returns the format string to format a NumberGTError
  250. func (l DefaultLocale) NumberGT() string {
  251. return `Must be greater than {{.min}}`
  252. }
  253. // NumberLTE returns the format string to format a NumberLTEError
  254. func (l DefaultLocale) NumberLTE() string {
  255. return `Must be less than or equal to {{.max}}`
  256. }
  257. // NumberLT returns the format string to format a NumberLTError
  258. func (l DefaultLocale) NumberLT() string {
  259. return `Must be less than {{.max}}`
  260. }
  261. // Schema validators
  262. // RegexPattern returns a format-string to format a regex-pattern error
  263. func (l DefaultLocale) RegexPattern() string {
  264. return `Invalid regex pattern '{{.pattern}}'`
  265. }
  266. // GreaterThanZero returns a format-string to format an error where a number must be greater than zero
  267. func (l DefaultLocale) GreaterThanZero() string {
  268. return `{{.number}} must be strictly greater than 0`
  269. }
  270. // MustBeOfA returns a format-string to format an error where a value is of the wrong type
  271. func (l DefaultLocale) MustBeOfA() string {
  272. return `{{.x}} must be of a {{.y}}`
  273. }
  274. // MustBeOfAn returns a format-string to format an error where a value is of the wrong type
  275. func (l DefaultLocale) MustBeOfAn() string {
  276. return `{{.x}} must be of an {{.y}}`
  277. }
  278. // CannotBeUsedWithout returns a format-string to format a "cannot be used without" error
  279. func (l DefaultLocale) CannotBeUsedWithout() string {
  280. return `{{.x}} cannot be used without {{.y}}`
  281. }
  282. // CannotBeGT returns a format-string to format an error where a value are greater than allowed
  283. func (l DefaultLocale) CannotBeGT() string {
  284. return `{{.x}} cannot be greater than {{.y}}`
  285. }
  286. // MustBeOfType returns a format-string to format an error where a value does not match the required type
  287. func (l DefaultLocale) MustBeOfType() string {
  288. return `{{.key}} must be of type {{.type}}`
  289. }
  290. // MustBeValidRegex returns a format-string to format an error where a regex is invalid
  291. func (l DefaultLocale) MustBeValidRegex() string {
  292. return `{{.key}} must be a valid regex`
  293. }
  294. // MustBeValidFormat returns a format-string to format an error where a value does not match the expected format
  295. func (l DefaultLocale) MustBeValidFormat() string {
  296. return `{{.key}} must be a valid format {{.given}}`
  297. }
  298. // MustBeGTEZero returns a format-string to format an error where a value must be greater or equal than 0
  299. func (l DefaultLocale) MustBeGTEZero() string {
  300. return `{{.key}} must be greater than or equal to 0`
  301. }
  302. // KeyCannotBeGreaterThan returns a format-string to format an error where a value is greater than the maximum allowed
  303. func (l DefaultLocale) KeyCannotBeGreaterThan() string {
  304. return `{{.key}} cannot be greater than {{.y}}`
  305. }
  306. // KeyItemsMustBeOfType returns a format-string to format an error where a key is of the wrong type
  307. func (l DefaultLocale) KeyItemsMustBeOfType() string {
  308. return `{{.key}} items must be {{.type}}`
  309. }
  310. // KeyItemsMustBeUnique returns a format-string to format an error where keys are not unique
  311. func (l DefaultLocale) KeyItemsMustBeUnique() string {
  312. return `{{.key}} items must be unique`
  313. }
  314. // ReferenceMustBeCanonical returns a format-string to format a "reference must be canonical" error
  315. func (l DefaultLocale) ReferenceMustBeCanonical() string {
  316. return `Reference {{.reference}} must be canonical`
  317. }
  318. // NotAValidType returns a format-string to format an invalid type error
  319. func (l DefaultLocale) NotAValidType() string {
  320. return `has a primitive type that is NOT VALID -- given: {{.given}} Expected valid values are:{{.expected}}`
  321. }
  322. // Duplicated returns a format-string to format an error where types are duplicated
  323. func (l DefaultLocale) Duplicated() string {
  324. return `{{.type}} type is duplicated`
  325. }
  326. // HttpBadStatus returns a format-string for errors when loading a schema using HTTP
  327. func (l DefaultLocale) HttpBadStatus() string {
  328. return `Could not read schema from HTTP, response status is {{.status}}`
  329. }
  330. // ErrorFormat returns a format string for errors
  331. // Replacement options: field, description, context, value
  332. func (l DefaultLocale) ErrorFormat() string {
  333. return `{{.field}}: {{.description}}`
  334. }
  335. // ParseError returns a format-string for JSON parsing errors
  336. func (l DefaultLocale) ParseError() string {
  337. return `Expected: {{.expected}}, given: Invalid JSON`
  338. }
  339. // ConditionThen returns a format-string for ConditionThenError errors
  340. // If/Else
  341. func (l DefaultLocale) ConditionThen() string {
  342. return `Must validate "then" as "if" was valid`
  343. }
  344. // ConditionElse returns a format-string for ConditionElseError errors
  345. func (l DefaultLocale) ConditionElse() string {
  346. return `Must validate "else" as "if" was not valid`
  347. }
  348. // constants
  349. const (
  350. STRING_NUMBER = "number"
  351. STRING_ARRAY_OF_STRINGS = "array of strings"
  352. STRING_ARRAY_OF_SCHEMAS = "array of schemas"
  353. STRING_SCHEMA = "valid schema"
  354. STRING_SCHEMA_OR_ARRAY_OF_STRINGS = "schema or array of strings"
  355. STRING_PROPERTIES = "properties"
  356. STRING_DEPENDENCY = "dependency"
  357. STRING_PROPERTY = "property"
  358. STRING_UNDEFINED = "undefined"
  359. STRING_CONTEXT_ROOT = "(root)"
  360. STRING_ROOT_SCHEMA_PROPERTY = "(root)"
  361. )