diff --git a/.gitmodules b/.gitmodules index 4e25145..e1a7410 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "test/suite"] path = test/suite - url = git://github.com/json-schema-org/JSON-Schema-Test-Suite.git + url = https://github.com/json-schema-org/JSON-Schema-Test-Suite.git diff --git a/lib/helpers.js b/lib/helpers.js index 58a9f36..239eb19 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,7 +1,5 @@ 'use strict'; -var uri = require('url'); - var ValidationError = exports.ValidationError = function ValidationError (message, instance, schema, path, name, argument) { if(Array.isArray(path)){ this.path = path; @@ -131,13 +129,13 @@ var SchemaContext = exports.SchemaContext = function SchemaContext (schema, opti }; SchemaContext.prototype.resolve = function resolve (target) { - return uri.resolve(this.base, target); + return (() => resolveUrl(this.base,target))(); }; SchemaContext.prototype.makeChild = function makeChild(schema, propertyName){ var path = (propertyName===undefined) ? this.path : this.path.concat([propertyName]); var id = schema.$id || schema.id; - var base = uri.resolve(this.base, id||''); + let base = (() => resolveUrl(this.base,id||''))(); var ctx = new SchemaContext(schema, this.options, path, base, Object.create(this.schemas)); if(id && !ctx.schemas[base]){ ctx.schemas[base] = schema; @@ -390,3 +388,19 @@ exports.isSchema = function isSchema(val){ return (typeof val === 'object' && val) || (typeof val === 'boolean'); }; +/** + * Resolve target URL from a base and relative URL. + * Similar to Node's URL Lib's legacy resolve function. + * Code from example in deprecation note in said library. + * @param string + * @param string + * @returns {string} + */ +var resolveUrl = exports.resolveUrl = function resolveUrl(from, to) { + const resolvedUrl = new URL(to, new URL(from, 'resolve://')); + if (resolvedUrl.protocol === 'resolve:') { + const { pathname, search, hash } = resolvedUrl; + return pathname + search + hash; + } + return resolvedUrl.toString(); +} diff --git a/lib/scan.js b/lib/scan.js index 26f6b33..c33100f 100644 --- a/lib/scan.js +++ b/lib/scan.js @@ -1,6 +1,5 @@ "use strict"; -var urilib = require('url'); var helpers = require('./helpers'); module.exports.SchemaScanResult = SchemaScanResult; @@ -20,12 +19,13 @@ module.exports.scan = function scan(base, schema){ if(!schema || typeof schema!='object') return; // Mark all referenced schemas so we can tell later which schemas are referred to, but never defined if(schema.$ref){ - var resolvedUri = urilib.resolve(baseuri, schema.$ref); + let resolvedUri = helpers.resolveUrl(baseuri,schema.$ref); ref[resolvedUri] = ref[resolvedUri] ? ref[resolvedUri]+1 : 0; return; } var id = schema.$id || schema.id; - var ourBase = id ? urilib.resolve(baseuri, id) : baseuri; + let resolvedBase = helpers.resolveUrl(baseuri,id); + var ourBase = id ? resolvedBase : baseuri; if (ourBase) { // If there's no fragment, append an empty one if(ourBase.indexOf('#')<0) ourBase += '#'; diff --git a/lib/validator.js b/lib/validator.js index cce1694..cca5e9c 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -1,7 +1,5 @@ 'use strict'; -var urilib = require('url'); - var attribute = require('./attribute'); var helpers = require('./helpers'); var scanSchema = require('./scan').scan; @@ -115,7 +113,7 @@ Validator.prototype.validate = function validate (instance, schema, options, ctx // This section indexes subschemas in the provided schema, so they don't need to be added with Validator#addSchema // This will work so long as the function at uri.resolve() will resolve a relative URI to a relative URI var id = schema.$id || schema.id; - var base = urilib.resolve(options.base||anonymousBase, id||''); + let base = helpers.resolveUrl(options.base,id||''); if(!ctx){ ctx = new SchemaContext(schema, options, [], base, Object.create(this.schemas)); if (!ctx.schemas[base]) { @@ -262,8 +260,8 @@ Validator.prototype.resolve = function resolve (schema, switchSchema, ctx) { return {subschema: ctx.schemas[switchSchema], switchSchema: switchSchema}; } // Else try walking the property pointer - var parsed = urilib.parse(switchSchema); - var fragment = parsed && parsed.hash; + let parsed = new URL(switchSchema,'thismessage::/'); + let fragment = parsed.hash; var document = fragment && fragment.length && switchSchema.substr(0, switchSchema.length - fragment.length); if (!document || !ctx.schemas[document]) { throw new SchemaError("no such schema <" + switchSchema + ">", schema);