v5.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. var META_SCHEMA_ID = 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json';
  3. module.exports = {
  4. enable: enableV5,
  5. META_SCHEMA_ID: META_SCHEMA_ID
  6. };
  7. function enableV5(ajv) {
  8. var inlineFunctions = {
  9. 'switch': require('./dotjs/switch'),
  10. 'constant': require('./dotjs/constant'),
  11. '_formatLimit': require('./dotjs/_formatLimit'),
  12. 'patternRequired': require('./dotjs/patternRequired')
  13. };
  14. if (ajv._opts.meta !== false) {
  15. var metaSchema = require('./refs/json-schema-v5.json');
  16. ajv.addMetaSchema(metaSchema, META_SCHEMA_ID);
  17. }
  18. _addKeyword('constant');
  19. ajv.addKeyword('contains', { type: 'array', macro: containsMacro });
  20. _addKeyword('formatMaximum', 'string', inlineFunctions._formatLimit);
  21. _addKeyword('formatMinimum', 'string', inlineFunctions._formatLimit);
  22. ajv.addKeyword('formatExclusiveMaximum');
  23. ajv.addKeyword('formatExclusiveMinimum');
  24. ajv.addKeyword('patternGroups'); // implemented in properties.jst
  25. _addKeyword('patternRequired', 'object');
  26. _addKeyword('switch');
  27. function _addKeyword(keyword, types, inlineFunc) {
  28. var definition = {
  29. inline: inlineFunc || inlineFunctions[keyword],
  30. statements: true,
  31. errors: 'full'
  32. };
  33. if (types) definition.type = types;
  34. ajv.addKeyword(keyword, definition);
  35. }
  36. }
  37. function containsMacro(schema) {
  38. return {
  39. not: { items: { not: schema } }
  40. };
  41. }