diff options
author | Kat Marchán <kzm@zkat.tech> | 2018-08-29 12:03:09 -0700 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-09-02 12:51:59 +0200 |
commit | 78f5685acc8fa574c32ed60d07de6273c60639b9 (patch) | |
tree | c3381e90e1c734763d98f079aad5d5f45438ca6d /deps/npm/node_modules/ajv/lib/keyword.js | |
parent | 1287e524eeba4632decce231da161426efb8fc34 (diff) | |
download | android-node-v8-78f5685acc8fa574c32ed60d07de6273c60639b9.tar.gz android-node-v8-78f5685acc8fa574c32ed60d07de6273c60639b9.tar.bz2 android-node-v8-78f5685acc8fa574c32ed60d07de6273c60639b9.zip |
deps: upgrade npm to 6.4.1
PR-URL: https://github.com/nodejs/node/pull/22591
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'deps/npm/node_modules/ajv/lib/keyword.js')
-rw-r--r-- | deps/npm/node_modules/ajv/lib/keyword.js | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/deps/npm/node_modules/ajv/lib/keyword.js b/deps/npm/node_modules/ajv/lib/keyword.js new file mode 100644 index 0000000000..5fcfb75fca --- /dev/null +++ b/deps/npm/node_modules/ajv/lib/keyword.js @@ -0,0 +1,135 @@ +'use strict'; + +var IDENTIFIER = /^[a-z_$][a-z0-9_$-]*$/i; +var customRuleCode = require('./dotjs/custom'); + +module.exports = { + add: addKeyword, + get: getKeyword, + remove: removeKeyword +}; + +/** + * Define custom keyword + * @this Ajv + * @param {String} keyword custom keyword, should be unique (including different from all standard, custom and macro keywords). + * @param {Object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`. + * @return {Ajv} this for method chaining + */ +function addKeyword(keyword, definition) { + /* jshint validthis: true */ + /* eslint no-shadow: 0 */ + var RULES = this.RULES; + + if (RULES.keywords[keyword]) + throw new Error('Keyword ' + keyword + ' is already defined'); + + if (!IDENTIFIER.test(keyword)) + throw new Error('Keyword ' + keyword + ' is not a valid identifier'); + + if (definition) { + if (definition.macro && definition.valid !== undefined) + throw new Error('"valid" option cannot be used with macro keywords'); + + var dataType = definition.type; + if (Array.isArray(dataType)) { + var i, len = dataType.length; + for (i=0; i<len; i++) checkDataType(dataType[i]); + for (i=0; i<len; i++) _addRule(keyword, dataType[i], definition); + } else { + if (dataType) checkDataType(dataType); + _addRule(keyword, dataType, definition); + } + + var $data = definition.$data === true && this._opts.$data; + if ($data && !definition.validate) + throw new Error('$data support: "validate" function is not defined'); + + var metaSchema = definition.metaSchema; + if (metaSchema) { + if ($data) { + metaSchema = { + anyOf: [ + metaSchema, + { '$ref': 'https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/$data.json#' } + ] + }; + } + definition.validateSchema = this.compile(metaSchema, true); + } + } + + RULES.keywords[keyword] = RULES.all[keyword] = true; + + + function _addRule(keyword, dataType, definition) { + var ruleGroup; + for (var i=0; i<RULES.length; i++) { + var rg = RULES[i]; + if (rg.type == dataType) { + ruleGroup = rg; + break; + } + } + + if (!ruleGroup) { + ruleGroup = { type: dataType, rules: [] }; + RULES.push(ruleGroup); + } + + var rule = { + keyword: keyword, + definition: definition, + custom: true, + code: customRuleCode, + implements: definition.implements + }; + ruleGroup.rules.push(rule); + RULES.custom[keyword] = rule; + } + + + function checkDataType(dataType) { + if (!RULES.types[dataType]) throw new Error('Unknown type ' + dataType); + } + + return this; +} + + +/** + * Get keyword + * @this Ajv + * @param {String} keyword pre-defined or custom keyword. + * @return {Object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise. + */ +function getKeyword(keyword) { + /* jshint validthis: true */ + var rule = this.RULES.custom[keyword]; + return rule ? rule.definition : this.RULES.keywords[keyword] || false; +} + + +/** + * Remove keyword + * @this Ajv + * @param {String} keyword pre-defined or custom keyword. + * @return {Ajv} this for method chaining + */ +function removeKeyword(keyword) { + /* jshint validthis: true */ + var RULES = this.RULES; + delete RULES.keywords[keyword]; + delete RULES.all[keyword]; + delete RULES.custom[keyword]; + for (var i=0; i<RULES.length; i++) { + var rules = RULES[i].rules; + for (var j=0; j<rules.length; j++) { + if (rules[j].keyword == keyword) { + rules.splice(j, 1); + break; + } + } + } + return this; +} |