Merge branch 'sondrele-fix/resolve-uri'

master
Evgeny Poberezkin 2018-03-24 23:09:06 +00:00
commit 309f9e38b9
3 changed files with 40 additions and 11 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
var url = require('url') var URI = require('uri-js')
, equal = require('fast-deep-equal') , equal = require('fast-deep-equal')
, util = require('./util') , util = require('./util')
, SchemaObject = require('./schema_obj') , SchemaObject = require('./schema_obj')
@ -67,7 +67,7 @@ function resolve(compile, root, ref) {
*/ */
function resolveSchema(root, ref) { function resolveSchema(root, ref) {
/* jshint validthis: true */ /* jshint validthis: true */
var p = url.parse(ref, false, true) var p = URI.parse(ref)
, refPath = _getFullPath(p) , refPath = _getFullPath(p)
, baseId = getFullPath(this._getId(root.schema)); , baseId = getFullPath(this._getId(root.schema));
if (refPath !== baseId) { if (refPath !== baseId) {
@ -115,9 +115,9 @@ var PREVENT_SCOPE_CHANGE = util.toHash(['properties', 'patternProperties', 'enum
/* @this Ajv */ /* @this Ajv */
function getJsonPointer(parsedRef, baseId, schema, root) { function getJsonPointer(parsedRef, baseId, schema, root) {
/* jshint validthis: true */ /* jshint validthis: true */
parsedRef.hash = parsedRef.hash || ''; parsedRef.fragment = parsedRef.fragment || '';
if (parsedRef.hash.slice(0,2) != '#/') return; if (parsedRef.fragment.slice(0,1) != '/') return;
var parts = parsedRef.hash.split('/'); var parts = parsedRef.fragment.split('/');
for (var i = 1; i < parts.length; i++) { for (var i = 1; i < parts.length; i++) {
var part = parts[i]; var part = parts[i];
@ -206,14 +206,13 @@ function countKeys(schema) {
function getFullPath(id, normalize) { function getFullPath(id, normalize) {
if (normalize !== false) id = normalizeId(id); if (normalize !== false) id = normalizeId(id);
var p = url.parse(id, false, true); var p = URI.parse(id);
return _getFullPath(p); return _getFullPath(p);
} }
function _getFullPath(p) { function _getFullPath(p) {
var protocolSeparator = p.protocol || p.href.slice(0,2) == '//' ? '//' : ''; return URI.serialize(p).split('#')[0] + '#';
return (p.protocol||'') + protocolSeparator + (p.host||'') + (p.path||'') + '#';
} }
@ -225,7 +224,7 @@ function normalizeId(id) {
function resolveUrl(baseId, id) { function resolveUrl(baseId, id) {
id = normalizeId(id); id = normalizeId(id);
return url.resolve(baseId, id); return URI.resolve(baseId, id);
} }
@ -246,7 +245,7 @@ function resolveIds(schema) {
fullPath += '/' + (typeof keyIndex == 'number' ? keyIndex : util.escapeFragment(keyIndex)); fullPath += '/' + (typeof keyIndex == 'number' ? keyIndex : util.escapeFragment(keyIndex));
if (typeof id == 'string') { if (typeof id == 'string') {
id = baseId = normalizeId(baseId ? url.resolve(baseId, id) : id); id = baseId = normalizeId(baseId ? URI.resolve(baseId, id) : id);
var refVal = self._refs[id]; var refVal = self._refs[id];
if (typeof refVal == 'string') refVal = self._refs[refVal]; if (typeof refVal == 'string') refVal = self._refs[refVal];

View File

@ -62,7 +62,8 @@
"dependencies": { "dependencies": {
"fast-deep-equal": "^1.0.0", "fast-deep-equal": "^1.0.0",
"fast-json-stable-stringify": "^2.0.0", "fast-json-stable-stringify": "^2.0.0",
"json-schema-traverse": "^0.3.0" "json-schema-traverse": "^0.3.0",
"uri-js": "^3.0.2"
}, },
"devDependencies": { "devDependencies": {
"ajv-async": "^1.0.0", "ajv-async": "^1.0.0",

View File

@ -91,6 +91,35 @@ describe('resolve', function () {
}); });
}); });
}); });
it('should resolve ids defined as urn\'s (issue #423)', function() {
var schema = {
"type": "object",
"properties": {
"ip1": {
"$id": "urn:some:ip:prop",
"type": "string",
"format": "ipv4"
},
"ip2": {
"$ref": "urn:some:ip:prop"
}
},
"required": [
"ip1",
"ip2"
]
};
var data = {
"ip1": "0.0.0.0",
"ip2": "0.0.0.0"
};
instances.forEach(function (ajv) {
var validate = ajv.compile(schema);
validate(data) .should.equal(true);
});
});
}); });