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