keyword.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. var IDENTIFIER = /^[a-z_$][a-z0-9_$\-]*$/i;
  3. var customRuleCode = require('./dotjs/custom');
  4. module.exports = {
  5. add: addKeyword,
  6. get: getKeyword,
  7. remove: removeKeyword
  8. };
  9. /**
  10. * Define custom keyword
  11. * @this Ajv
  12. * @param {String} keyword custom keyword, should be unique (including different from all standard, custom and macro keywords).
  13. * @param {Object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`.
  14. */
  15. function addKeyword(keyword, definition) {
  16. /* jshint validthis: true */
  17. /* eslint no-shadow: 0 */
  18. var RULES = this.RULES;
  19. if (RULES.keywords[keyword])
  20. throw new Error('Keyword ' + keyword + ' is already defined');
  21. if (!IDENTIFIER.test(keyword))
  22. throw new Error('Keyword ' + keyword + ' is not a valid identifier');
  23. if (definition) {
  24. if (definition.macro && definition.valid !== undefined)
  25. throw new Error('"valid" option cannot be used with macro keywords');
  26. var dataType = definition.type;
  27. if (Array.isArray(dataType)) {
  28. var i, len = dataType.length;
  29. for (i=0; i<len; i++) checkDataType(dataType[i]);
  30. for (i=0; i<len; i++) _addRule(keyword, dataType[i], definition);
  31. } else {
  32. if (dataType) checkDataType(dataType);
  33. _addRule(keyword, dataType, definition);
  34. }
  35. var $data = definition.$data === true && this._opts.v5;
  36. if ($data && !definition.validate)
  37. throw new Error('$data support: "validate" function is not defined');
  38. var metaSchema = definition.metaSchema;
  39. if (metaSchema) {
  40. if ($data) {
  41. metaSchema = {
  42. anyOf: [
  43. metaSchema,
  44. { '$ref': 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#/definitions/$data' }
  45. ]
  46. };
  47. }
  48. definition.validateSchema = this.compile(metaSchema, true);
  49. }
  50. }
  51. RULES.keywords[keyword] = RULES.all[keyword] = true;
  52. function _addRule(keyword, dataType, definition) {
  53. var ruleGroup;
  54. for (var i=0; i<RULES.length; i++) {
  55. var rg = RULES[i];
  56. if (rg.type == dataType) {
  57. ruleGroup = rg;
  58. break;
  59. }
  60. }
  61. if (!ruleGroup) {
  62. ruleGroup = { type: dataType, rules: [] };
  63. RULES.push(ruleGroup);
  64. }
  65. var rule = {
  66. keyword: keyword,
  67. definition: definition,
  68. custom: true,
  69. code: customRuleCode
  70. };
  71. ruleGroup.rules.push(rule);
  72. RULES.custom[keyword] = rule;
  73. }
  74. function checkDataType(dataType) {
  75. if (!RULES.types[dataType]) throw new Error('Unknown type ' + dataType);
  76. }
  77. }
  78. /**
  79. * Get keyword
  80. * @this Ajv
  81. * @param {String} keyword pre-defined or custom keyword.
  82. * @return {Object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise.
  83. */
  84. function getKeyword(keyword) {
  85. /* jshint validthis: true */
  86. var rule = this.RULES.custom[keyword];
  87. return rule ? rule.definition : this.RULES.keywords[keyword] || false;
  88. }
  89. /**
  90. * Remove keyword
  91. * @this Ajv
  92. * @param {String} keyword pre-defined or custom keyword.
  93. */
  94. function removeKeyword(keyword) {
  95. /* jshint validthis: true */
  96. var RULES = this.RULES;
  97. delete RULES.keywords[keyword];
  98. delete RULES.all[keyword];
  99. delete RULES.custom[keyword];
  100. for (var i=0; i<RULES.length; i++) {
  101. var rules = RULES[i].rules;
  102. for (var j=0; j<rules.length; j++) {
  103. if (rules[j].keyword == keyword) {
  104. rules.splice(j, 1);
  105. break;
  106. }
  107. }
  108. }
  109. }