diff options
author | Rebecca Turner <me@re-becca.org> | 2017-04-12 21:47:49 -0700 |
---|---|---|
committer | Jeremiah Senkpiel <fishrock123@rocketmail.com> | 2017-04-25 10:52:01 -0400 |
commit | 00842604483e4c2e622dfdb3a97440e07646158f (patch) | |
tree | f3346902636a44b6037652523767636bf7e4f2c9 /deps/npm/node_modules/request/node_modules/har-validator/node_modules/ajv/lib/compile/resolve.js | |
parent | 061c5da010e0d249379618382a499840d38247b8 (diff) | |
download | android-node-v8-00842604483e4c2e622dfdb3a97440e07646158f.tar.gz android-node-v8-00842604483e4c2e622dfdb3a97440e07646158f.tar.bz2 android-node-v8-00842604483e4c2e622dfdb3a97440e07646158f.zip |
deps: upgrade npm to 4.5.0
PR-URL: https://github.com/nodejs/node/pull/12480
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'deps/npm/node_modules/request/node_modules/har-validator/node_modules/ajv/lib/compile/resolve.js')
-rw-r--r-- | deps/npm/node_modules/request/node_modules/har-validator/node_modules/ajv/lib/compile/resolve.js | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/deps/npm/node_modules/request/node_modules/har-validator/node_modules/ajv/lib/compile/resolve.js b/deps/npm/node_modules/request/node_modules/har-validator/node_modules/ajv/lib/compile/resolve.js new file mode 100644 index 0000000000..db2b91fbf1 --- /dev/null +++ b/deps/npm/node_modules/request/node_modules/har-validator/node_modules/ajv/lib/compile/resolve.js @@ -0,0 +1,267 @@ +'use strict'; + +var url = require('url') + , equal = require('./equal') + , util = require('./util') + , SchemaObject = require('./schema_obj'); + +module.exports = resolve; + +resolve.normalizeId = normalizeId; +resolve.fullPath = getFullPath; +resolve.url = resolveUrl; +resolve.ids = resolveIds; +resolve.inlineRef = inlineRef; +resolve.schema = resolveSchema; + +/** + * [resolve and compile the references ($ref)] + * @this Ajv + * @param {Function} compile reference to schema compilation funciton (localCompile) + * @param {Object} root object with information about the root schema for the current schema + * @param {String} ref reference to resolve + * @return {Object|Function} schema object (if the schema can be inlined) or validation function + */ +function resolve(compile, root, ref) { + /* jshint validthis: true */ + var refVal = this._refs[ref]; + if (typeof refVal == 'string') { + if (this._refs[refVal]) refVal = this._refs[refVal]; + else return resolve.call(this, compile, root, refVal); + } + + refVal = refVal || this._schemas[ref]; + if (refVal instanceof SchemaObject) { + return inlineRef(refVal.schema, this._opts.inlineRefs) + ? refVal.schema + : refVal.validate || this._compile(refVal); + } + + var res = resolveSchema.call(this, root, ref); + var schema, v, baseId; + if (res) { + schema = res.schema; + root = res.root; + baseId = res.baseId; + } + + if (schema instanceof SchemaObject) { + v = schema.validate || compile.call(this, schema.schema, root, undefined, baseId); + } else if (schema) { + v = inlineRef(schema, this._opts.inlineRefs) + ? schema + : compile.call(this, schema, root, undefined, baseId); + } + + return v; +} + + +/** + * Resolve schema, its root and baseId + * @this Ajv + * @param {Object} root root object with properties schema, refVal, refs + * @param {String} ref reference to resolve + * @return {Object} object with properties schema, root, baseId + */ +function resolveSchema(root, ref) { + /* jshint validthis: true */ + var p = url.parse(ref, false, true) + , refPath = _getFullPath(p) + , baseId = getFullPath(root.schema.id); + if (refPath !== baseId) { + var id = normalizeId(refPath); + var refVal = this._refs[id]; + if (typeof refVal == 'string') { + return resolveRecursive.call(this, root, refVal, p); + } else if (refVal instanceof SchemaObject) { + if (!refVal.validate) this._compile(refVal); + root = refVal; + } else { + refVal = this._schemas[id]; + if (refVal instanceof SchemaObject) { + if (!refVal.validate) this._compile(refVal); + if (id == normalizeId(ref)) + return { schema: refVal, root: root, baseId: baseId }; + root = refVal; + } else { + return; + } + } + if (!root.schema) return; + baseId = getFullPath(root.schema.id); + } + return getJsonPointer.call(this, p, baseId, root.schema, root); +} + + +/* @this Ajv */ +function resolveRecursive(root, ref, parsedRef) { + /* jshint validthis: true */ + var res = resolveSchema.call(this, root, ref); + if (res) { + var schema = res.schema; + var baseId = res.baseId; + root = res.root; + if (schema.id) baseId = resolveUrl(baseId, schema.id); + return getJsonPointer.call(this, parsedRef, baseId, schema, root); + } +} + + +var PREVENT_SCOPE_CHANGE = util.toHash(['properties', 'patternProperties', 'enum', 'dependencies', 'definitions']); +/* @this Ajv */ +function getJsonPointer(parsedRef, baseId, schema, root) { + /* jshint validthis: true */ + parsedRef.hash = parsedRef.hash || ''; + if (parsedRef.hash.slice(0,2) != '#/') return; + var parts = parsedRef.hash.split('/'); + + for (var i = 1; i < parts.length; i++) { + var part = parts[i]; + if (part) { + part = util.unescapeFragment(part); + schema = schema[part]; + if (!schema) break; + if (schema.id && !PREVENT_SCOPE_CHANGE[part]) baseId = resolveUrl(baseId, schema.id); + if (schema.$ref) { + var $ref = resolveUrl(baseId, schema.$ref); + var res = resolveSchema.call(this, root, $ref); + if (res) { + schema = res.schema; + root = res.root; + baseId = res.baseId; + } + } + } + } + if (schema && schema != root.schema) + return { schema: schema, root: root, baseId: baseId }; +} + + +var SIMPLE_INLINED = util.toHash([ + 'type', 'format', 'pattern', + 'maxLength', 'minLength', + 'maxProperties', 'minProperties', + 'maxItems', 'minItems', + 'maximum', 'minimum', + 'uniqueItems', 'multipleOf', + 'required', 'enum' +]); +function inlineRef(schema, limit) { + if (limit === false) return false; + if (limit === undefined || limit === true) return checkNoRef(schema); + else if (limit) return countKeys(schema) <= limit; +} + + +function checkNoRef(schema) { + var item; + if (Array.isArray(schema)) { + for (var i=0; i<schema.length; i++) { + item = schema[i]; + if (typeof item == 'object' && !checkNoRef(item)) return false; + } + } else { + for (var key in schema) { + if (key == '$ref') return false; + item = schema[key]; + if (typeof item == 'object' && !checkNoRef(item)) return false; + } + } + return true; +} + + +function countKeys(schema) { + var count = 0, item; + if (Array.isArray(schema)) { + for (var i=0; i<schema.length; i++) { + item = schema[i]; + if (typeof item == 'object') count += countKeys(item); + if (count == Infinity) return Infinity; + } + } else { + for (var key in schema) { + if (key == '$ref') return Infinity; + if (SIMPLE_INLINED[key]) { + count++; + } else { + item = schema[key]; + if (typeof item == 'object') count += countKeys(item) + 1; + if (count == Infinity) return Infinity; + } + } + } + return count; +} + + +function getFullPath(id, normalize) { + if (normalize !== false) id = normalizeId(id); + var p = url.parse(id, false, true); + return _getFullPath(p); +} + + +function _getFullPath(p) { + var protocolSeparator = p.protocol || p.href.slice(0,2) == '//' ? '//' : ''; + return (p.protocol||'') + protocolSeparator + (p.host||'') + (p.path||'') + '#'; +} + + +var TRAILING_SLASH_HASH = /#\/?$/; +function normalizeId(id) { + return id ? id.replace(TRAILING_SLASH_HASH, '') : ''; +} + + +function resolveUrl(baseId, id) { + id = normalizeId(id); + return url.resolve(baseId, id); +} + + +/* @this Ajv */ +function resolveIds(schema) { + /* eslint no-shadow: 0 */ + /* jshint validthis: true */ + var id = normalizeId(schema.id); + var localRefs = {}; + _resolveIds.call(this, schema, getFullPath(id, false), id); + return localRefs; + + /* @this Ajv */ + function _resolveIds(schema, fullPath, baseId) { + /* jshint validthis: true */ + if (Array.isArray(schema)) { + for (var i=0; i<schema.length; i++) + _resolveIds.call(this, schema[i], fullPath+'/'+i, baseId); + } else if (schema && typeof schema == 'object') { + if (typeof schema.id == 'string') { + var id = baseId = baseId + ? url.resolve(baseId, schema.id) + : schema.id; + id = normalizeId(id); + + var refVal = this._refs[id]; + if (typeof refVal == 'string') refVal = this._refs[refVal]; + if (refVal && refVal.schema) { + if (!equal(schema, refVal.schema)) + throw new Error('id "' + id + '" resolves to more than one schema'); + } else if (id != normalizeId(fullPath)) { + if (id[0] == '#') { + if (localRefs[id] && !equal(schema, localRefs[id])) + throw new Error('id "' + id + '" resolves to more than one schema'); + localRefs[id] = schema; + } else { + this._refs[id] = fullPath; + } + } + } + for (var key in schema) + _resolveIds.call(this, schema[key], fullPath+'/'+util.escapeFragment(key), baseId); + } + } +} |