Security update to Drupal 8.4.6
[yaffs-website] / node_modules / ajv / lib / keyword.js
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  */
18 function addKeyword(keyword, definition) {
19   /* jshint validthis: true */
20   /* eslint no-shadow: 0 */
21   var RULES = this.RULES;
22
23   if (RULES.keywords[keyword])
24     throw new Error('Keyword ' + keyword + ' is already defined');
25
26   if (!IDENTIFIER.test(keyword))
27     throw new Error('Keyword ' + keyword + ' is not a valid identifier');
28
29   if (definition) {
30     if (definition.macro && definition.valid !== undefined)
31       throw new Error('"valid" option cannot be used with macro keywords');
32
33     var dataType = definition.type;
34     if (Array.isArray(dataType)) {
35       var i, len = dataType.length;
36       for (i=0; i<len; i++) checkDataType(dataType[i]);
37       for (i=0; i<len; i++) _addRule(keyword, dataType[i], definition);
38     } else {
39       if (dataType) checkDataType(dataType);
40       _addRule(keyword, dataType, definition);
41     }
42
43     var $data = definition.$data === true && this._opts.v5;
44     if ($data && !definition.validate)
45       throw new Error('$data support: "validate" function is not defined');
46
47     var metaSchema = definition.metaSchema;
48     if (metaSchema) {
49       if ($data) {
50         metaSchema = {
51           anyOf: [
52             metaSchema,
53             { '$ref': 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#/definitions/$data' }
54           ]
55         };
56       }
57       definition.validateSchema = this.compile(metaSchema, true);
58     }
59   }
60
61   RULES.keywords[keyword] = RULES.all[keyword] = true;
62
63
64   function _addRule(keyword, dataType, definition) {
65     var ruleGroup;
66     for (var i=0; i<RULES.length; i++) {
67       var rg = RULES[i];
68       if (rg.type == dataType) {
69         ruleGroup = rg;
70         break;
71       }
72     }
73
74     if (!ruleGroup) {
75       ruleGroup = { type: dataType, rules: [] };
76       RULES.push(ruleGroup);
77     }
78
79     var rule = {
80       keyword: keyword,
81       definition: definition,
82       custom: true,
83       code: customRuleCode
84     };
85     ruleGroup.rules.push(rule);
86     RULES.custom[keyword] = rule;
87   }
88
89
90   function checkDataType(dataType) {
91     if (!RULES.types[dataType]) throw new Error('Unknown type ' + dataType);
92   }
93 }
94
95
96 /**
97  * Get keyword
98  * @this  Ajv
99  * @param {String} keyword pre-defined or custom keyword.
100  * @return {Object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise.
101  */
102 function getKeyword(keyword) {
103   /* jshint validthis: true */
104   var rule = this.RULES.custom[keyword];
105   return rule ? rule.definition : this.RULES.keywords[keyword] || false;
106 }
107
108
109 /**
110  * Remove keyword
111  * @this  Ajv
112  * @param {String} keyword pre-defined or custom keyword.
113  */
114 function removeKeyword(keyword) {
115   /* jshint validthis: true */
116   var RULES = this.RULES;
117   delete RULES.keywords[keyword];
118   delete RULES.all[keyword];
119   delete RULES.custom[keyword];
120   for (var i=0; i<RULES.length; i++) {
121     var rules = RULES[i].rules;
122     for (var j=0; j<rules.length; j++) {
123       if (rules[j].keyword == keyword) {
124         rules.splice(j, 1);
125         break;
126       }
127     }
128   }
129 }