ajv.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @fileoverview The instance of Ajv validator.
  3. * @author Evgeny Poberezkin
  4. */
  5. //------------------------------------------------------------------------------
  6. // Requirements
  7. //------------------------------------------------------------------------------
  8. import Ajv from "ajv";
  9. //-----------------------------------------------------------------------------
  10. // Helpers
  11. //-----------------------------------------------------------------------------
  12. /*
  13. * Copied from ajv/lib/refs/json-schema-draft-04.json
  14. * The MIT License (MIT)
  15. * Copyright (c) 2015-2017 Evgeny Poberezkin
  16. */
  17. const metaSchema = {
  18. id: "http://json-schema.org/draft-04/schema#",
  19. $schema: "http://json-schema.org/draft-04/schema#",
  20. description: "Core schema meta-schema",
  21. definitions: {
  22. schemaArray: {
  23. type: "array",
  24. minItems: 1,
  25. items: { $ref: "#" }
  26. },
  27. positiveInteger: {
  28. type: "integer",
  29. minimum: 0
  30. },
  31. positiveIntegerDefault0: {
  32. allOf: [{ $ref: "#/definitions/positiveInteger" }, { default: 0 }]
  33. },
  34. simpleTypes: {
  35. enum: ["array", "boolean", "integer", "null", "number", "object", "string"]
  36. },
  37. stringArray: {
  38. type: "array",
  39. items: { type: "string" },
  40. minItems: 1,
  41. uniqueItems: true
  42. }
  43. },
  44. type: "object",
  45. properties: {
  46. id: {
  47. type: "string"
  48. },
  49. $schema: {
  50. type: "string"
  51. },
  52. title: {
  53. type: "string"
  54. },
  55. description: {
  56. type: "string"
  57. },
  58. default: { },
  59. multipleOf: {
  60. type: "number",
  61. minimum: 0,
  62. exclusiveMinimum: true
  63. },
  64. maximum: {
  65. type: "number"
  66. },
  67. exclusiveMaximum: {
  68. type: "boolean",
  69. default: false
  70. },
  71. minimum: {
  72. type: "number"
  73. },
  74. exclusiveMinimum: {
  75. type: "boolean",
  76. default: false
  77. },
  78. maxLength: { $ref: "#/definitions/positiveInteger" },
  79. minLength: { $ref: "#/definitions/positiveIntegerDefault0" },
  80. pattern: {
  81. type: "string",
  82. format: "regex"
  83. },
  84. additionalItems: {
  85. anyOf: [
  86. { type: "boolean" },
  87. { $ref: "#" }
  88. ],
  89. default: { }
  90. },
  91. items: {
  92. anyOf: [
  93. { $ref: "#" },
  94. { $ref: "#/definitions/schemaArray" }
  95. ],
  96. default: { }
  97. },
  98. maxItems: { $ref: "#/definitions/positiveInteger" },
  99. minItems: { $ref: "#/definitions/positiveIntegerDefault0" },
  100. uniqueItems: {
  101. type: "boolean",
  102. default: false
  103. },
  104. maxProperties: { $ref: "#/definitions/positiveInteger" },
  105. minProperties: { $ref: "#/definitions/positiveIntegerDefault0" },
  106. required: { $ref: "#/definitions/stringArray" },
  107. additionalProperties: {
  108. anyOf: [
  109. { type: "boolean" },
  110. { $ref: "#" }
  111. ],
  112. default: { }
  113. },
  114. definitions: {
  115. type: "object",
  116. additionalProperties: { $ref: "#" },
  117. default: { }
  118. },
  119. properties: {
  120. type: "object",
  121. additionalProperties: { $ref: "#" },
  122. default: { }
  123. },
  124. patternProperties: {
  125. type: "object",
  126. additionalProperties: { $ref: "#" },
  127. default: { }
  128. },
  129. dependencies: {
  130. type: "object",
  131. additionalProperties: {
  132. anyOf: [
  133. { $ref: "#" },
  134. { $ref: "#/definitions/stringArray" }
  135. ]
  136. }
  137. },
  138. enum: {
  139. type: "array",
  140. minItems: 1,
  141. uniqueItems: true
  142. },
  143. type: {
  144. anyOf: [
  145. { $ref: "#/definitions/simpleTypes" },
  146. {
  147. type: "array",
  148. items: { $ref: "#/definitions/simpleTypes" },
  149. minItems: 1,
  150. uniqueItems: true
  151. }
  152. ]
  153. },
  154. format: { type: "string" },
  155. allOf: { $ref: "#/definitions/schemaArray" },
  156. anyOf: { $ref: "#/definitions/schemaArray" },
  157. oneOf: { $ref: "#/definitions/schemaArray" },
  158. not: { $ref: "#" }
  159. },
  160. dependencies: {
  161. exclusiveMaximum: ["maximum"],
  162. exclusiveMinimum: ["minimum"]
  163. },
  164. default: { }
  165. };
  166. //------------------------------------------------------------------------------
  167. // Public Interface
  168. //------------------------------------------------------------------------------
  169. export default (additionalOptions = {}) => {
  170. const ajv = new Ajv({
  171. meta: false,
  172. useDefaults: true,
  173. validateSchema: false,
  174. missingRefs: "ignore",
  175. verbose: true,
  176. schemaId: "auto",
  177. ...additionalOptions
  178. });
  179. ajv.addMetaSchema(metaSchema);
  180. // eslint-disable-next-line no-underscore-dangle
  181. ajv._opts.defaultMeta = metaSchema.id;
  182. return ajv;
  183. };