3 var IDENTIFIER = /^[a-z_$][a-z0-9_$-]*$/i;
4 var customRuleCode = require('./dotjs/custom');
5 var definitionSchema = require('./definition_schema');
10 remove: removeKeyword,
11 validate: validateKeyword
16 * Define custom keyword
18 * @param {String} keyword custom keyword, should be unique (including different from all standard, custom and macro keywords).
19 * @param {Object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`.
20 * @return {Ajv} this for method chaining
22 function addKeyword(keyword, definition) {
23 /* jshint validthis: true */
24 /* eslint no-shadow: 0 */
25 var RULES = this.RULES;
26 if (RULES.keywords[keyword])
27 throw new Error('Keyword ' + keyword + ' is already defined');
29 if (!IDENTIFIER.test(keyword))
30 throw new Error('Keyword ' + keyword + ' is not a valid identifier');
33 this.validateKeyword(definition, true);
35 var dataType = definition.type;
36 if (Array.isArray(dataType)) {
37 for (var i=0; i<dataType.length; i++)
38 _addRule(keyword, dataType[i], definition);
40 _addRule(keyword, dataType, definition);
43 var metaSchema = definition.metaSchema;
45 if (definition.$data && this._opts.$data) {
49 { '$ref': 'https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json#' }
53 definition.validateSchema = this.compile(metaSchema, true);
57 RULES.keywords[keyword] = RULES.all[keyword] = true;
60 function _addRule(keyword, dataType, definition) {
62 for (var i=0; i<RULES.length; i++) {
64 if (rg.type == dataType) {
71 ruleGroup = { type: dataType, rules: [] };
72 RULES.push(ruleGroup);
77 definition: definition,
80 implements: definition.implements
82 ruleGroup.rules.push(rule);
83 RULES.custom[keyword] = rule;
93 * @param {String} keyword pre-defined or custom keyword.
94 * @return {Object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise.
96 function getKeyword(keyword) {
97 /* jshint validthis: true */
98 var rule = this.RULES.custom[keyword];
99 return rule ? rule.definition : this.RULES.keywords[keyword] || false;
106 * @param {String} keyword pre-defined or custom keyword.
107 * @return {Ajv} this for method chaining
109 function removeKeyword(keyword) {
110 /* jshint validthis: true */
111 var RULES = this.RULES;
112 delete RULES.keywords[keyword];
113 delete RULES.all[keyword];
114 delete RULES.custom[keyword];
115 for (var i=0; i<RULES.length; i++) {
116 var rules = RULES[i].rules;
117 for (var j=0; j<rules.length; j++) {
118 if (rules[j].keyword == keyword) {
129 * Validate keyword definition
131 * @param {Object} definition keyword definition object.
132 * @param {Boolean} throwError true to throw exception if definition is invalid
133 * @return {boolean} validation result
135 function validateKeyword(definition, throwError) {
136 validateKeyword.errors = null;
137 var v = this._validateKeyword = this._validateKeyword
138 || this.compile(definitionSchema, true);
140 if (v(definition)) return true;
141 validateKeyword.errors = v.errors;
143 throw new Error('custom keyword definition is invalid: ' + this.errorsText(v.errors));